mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* Move TraceQL metrics queries to backend * Add tests for TransformMetricsResponse * Add tests for createMetricsQuery * Refactor, detect metric queries and throw error for search queries * Remove unneeded types * Don't add start and end if they are 0. Return on err * lint * Fix pre-alloc of frames * Address PR comments
134 lines
3.7 KiB
Go
134 lines
3.7 KiB
Go
package tempo
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
"github.com/grafana/grafana/pkg/tsdb/tempo/kinds/dataquery"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestCreateMetricsQuery_Success(t *testing.T) {
|
|
logger := backend.NewLoggerWith("logger", "tsdb.tempo.test")
|
|
service := &Service{
|
|
logger: logger,
|
|
}
|
|
dsInfo := &Datasource{
|
|
URL: "http://tempo:3100",
|
|
}
|
|
queryVal := "{attribute=\"value\"}"
|
|
stepVal := "14"
|
|
query := &dataquery.TempoQuery{
|
|
Query: &queryVal,
|
|
Step: &stepVal,
|
|
}
|
|
start := int64(1625097600)
|
|
end := int64(1625184000)
|
|
|
|
req, err := service.createMetricsQuery(context.Background(), dsInfo, query, start, end)
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, req)
|
|
assert.Equal(t, "http://tempo:3100/api/metrics/query_range?end=1625184000&q=%7Battribute%3D%22value%22%7D&start=1625097600&step=14", req.URL.String())
|
|
assert.Equal(t, "application/json", req.Header.Get("Accept"))
|
|
}
|
|
func TestCreateMetricsQuery_OnlyQuery(t *testing.T) {
|
|
logger := backend.NewLoggerWith("logger", "tsdb.tempo.test")
|
|
service := &Service{
|
|
logger: logger,
|
|
}
|
|
dsInfo := &Datasource{
|
|
URL: "http://tempo:3100",
|
|
}
|
|
queryVal := "{attribute=\"value\"}"
|
|
query := &dataquery.TempoQuery{
|
|
Query: &queryVal,
|
|
}
|
|
|
|
req, err := service.createMetricsQuery(context.Background(), dsInfo, query, 0, 0)
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, req)
|
|
assert.Equal(t, "http://tempo:3100/api/metrics/query_range?q=%7Battribute%3D%22value%22%7D", req.URL.String())
|
|
assert.Equal(t, "application/json", req.Header.Get("Accept"))
|
|
}
|
|
|
|
func TestCreateMetricsQuery_URLParseError(t *testing.T) {
|
|
logger := backend.NewLoggerWith("logger", "tsdb.tempo.test")
|
|
service := &Service{
|
|
logger: logger,
|
|
}
|
|
dsInfo := &Datasource{
|
|
URL: "http://[::1]:namedport",
|
|
}
|
|
queryVal := "{attribute=\"value\"}"
|
|
query := &dataquery.TempoQuery{
|
|
Query: &queryVal,
|
|
}
|
|
start := int64(1625097600)
|
|
end := int64(1625184000)
|
|
|
|
req, err := service.createMetricsQuery(context.Background(), dsInfo, query, start, end)
|
|
assert.Error(t, err)
|
|
assert.Nil(t, req)
|
|
}
|
|
|
|
func TestEmptyQueryString_ReturnsFalse(t *testing.T) {
|
|
result := isMetricsQuery("")
|
|
assert.False(t, result)
|
|
}
|
|
|
|
func TestQueryWithoutMetricsFunction_ReturnsFalse(t *testing.T) {
|
|
result := isMetricsQuery("{.some = \"random query\"} && {} >> {}")
|
|
assert.False(t, result)
|
|
}
|
|
|
|
func TestQueryWithRateFunction_ReturnsTrue(t *testing.T) {
|
|
result := isMetricsQuery("{} | rate(foo)")
|
|
assert.True(t, result)
|
|
}
|
|
|
|
func TestQueryWithAvgOverTimeFunction_ReturnsTrue(t *testing.T) {
|
|
result := isMetricsQuery("{} | avg_over_time(foo)")
|
|
assert.True(t, result)
|
|
}
|
|
|
|
func TestQueryWithCountOverTimeFunction_ReturnsTrue(t *testing.T) {
|
|
result := isMetricsQuery("{} | count_over_time(foo)")
|
|
assert.True(t, result)
|
|
}
|
|
|
|
func TestQueryWithMaxOverTimeFunction_ReturnsTrue(t *testing.T) {
|
|
result := isMetricsQuery("{} | max_over_time(foo)")
|
|
assert.True(t, result)
|
|
}
|
|
|
|
func TestQueryWithMinOverTimeFunction_ReturnsTrue(t *testing.T) {
|
|
result := isMetricsQuery("{} | min_over_time(foo)")
|
|
assert.True(t, result)
|
|
}
|
|
|
|
func TestQueryWithQuantileOverTimeFunction_ReturnsTrue(t *testing.T) {
|
|
result := isMetricsQuery("{} | quantile_over_time(foo)")
|
|
assert.True(t, result)
|
|
}
|
|
|
|
func TestQueryWithHistogramOverTimeFunction_ReturnsTrue(t *testing.T) {
|
|
result := isMetricsQuery("{} | histogram_over_time(foo)")
|
|
assert.True(t, result)
|
|
}
|
|
|
|
func TestQueryWithCompareFunction_ReturnsTrue(t *testing.T) {
|
|
result := isMetricsQuery("{} | compare(foo)")
|
|
assert.True(t, result)
|
|
}
|
|
|
|
func TestQueryWithMultipleFunctions_ReturnsTrue(t *testing.T) {
|
|
result := isMetricsQuery("{} | rate(foo) | avg_over_time(bar)")
|
|
assert.True(t, result)
|
|
}
|
|
|
|
func TestQueryWithInvalidFunction_ReturnsFalse(t *testing.T) {
|
|
result := isMetricsQuery("{} | invalid_function(foo)")
|
|
assert.False(t, result)
|
|
}
|