2016-09-21 04:17:29 -05:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2017-11-15 04:22:00 -06:00
|
|
|
"time"
|
2016-09-21 04:17:29 -05:00
|
|
|
|
2020-12-04 01:49:39 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2017-11-15 04:22:00 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-03-08 00:02:49 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2016-09-21 04:17:29 -05:00
|
|
|
p "github.com/prometheus/common/model"
|
2020-12-04 01:49:39 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-09-21 04:17:29 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPrometheus(t *testing.T) {
|
2020-12-04 01:49:39 -06:00
|
|
|
dsInfo := &models.DataSource{
|
|
|
|
JsonData: simplejson.New(),
|
|
|
|
}
|
2021-03-08 00:02:49 -06:00
|
|
|
plug, err := NewExecutor(dsInfo)
|
|
|
|
executor := plug.(*PrometheusExecutor)
|
|
|
|
require.NoError(t, err)
|
2020-12-04 01:49:39 -06:00
|
|
|
|
|
|
|
t.Run("converting metric name", func(t *testing.T) {
|
|
|
|
metric := map[p.LabelName]p.LabelValue{
|
|
|
|
p.LabelName("app"): p.LabelValue("backend"),
|
|
|
|
p.LabelName("device"): p.LabelValue("mobile"),
|
2017-11-15 04:22:00 -06:00
|
|
|
}
|
2016-09-21 04:17:29 -05:00
|
|
|
|
2020-12-04 01:49:39 -06:00
|
|
|
query := &PrometheusQuery{
|
|
|
|
LegendFormat: "legend {{app}} {{ device }} {{broken}}",
|
|
|
|
}
|
2016-09-21 04:17:29 -05:00
|
|
|
|
2020-12-04 01:49:39 -06:00
|
|
|
require.Equal(t, "legend backend mobile ", formatLegend(metric, query))
|
|
|
|
})
|
2016-11-03 12:04:09 -05:00
|
|
|
|
2020-12-04 01:49:39 -06:00
|
|
|
t.Run("build full series name", func(t *testing.T) {
|
|
|
|
metric := map[p.LabelName]p.LabelValue{
|
|
|
|
p.LabelName(p.MetricNameLabel): p.LabelValue("http_request_total"),
|
|
|
|
p.LabelName("app"): p.LabelValue("backend"),
|
|
|
|
p.LabelName("device"): p.LabelValue("mobile"),
|
|
|
|
}
|
2016-11-03 12:04:09 -05:00
|
|
|
|
2020-12-04 01:49:39 -06:00
|
|
|
query := &PrometheusQuery{
|
|
|
|
LegendFormat: "",
|
|
|
|
}
|
2016-11-03 12:04:09 -05:00
|
|
|
|
2020-12-04 01:49:39 -06:00
|
|
|
require.Equal(t, `http_request_total{app="backend", device="mobile"}`, formatLegend(metric, query))
|
|
|
|
})
|
2017-11-15 04:22:00 -06:00
|
|
|
|
2020-12-04 01:49:39 -06:00
|
|
|
t.Run("parsing query model with step", func(t *testing.T) {
|
|
|
|
json := `{
|
2017-11-15 04:22:00 -06:00
|
|
|
"expr": "go_goroutines",
|
|
|
|
"format": "time_series",
|
|
|
|
"refId": "A"
|
|
|
|
}`
|
2020-12-04 01:49:39 -06:00
|
|
|
jsonModel, _ := simplejson.NewJson([]byte(json))
|
2021-03-08 00:02:49 -06:00
|
|
|
queryModels := []plugins.DataSubQuery{
|
2020-12-04 01:49:39 -06:00
|
|
|
{Model: jsonModel},
|
|
|
|
}
|
2017-11-15 04:22:00 -06:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
timeRange := plugins.NewDataTimeRange("12h", "now")
|
|
|
|
queryContext := plugins.DataQuery{
|
|
|
|
Queries: queryModels,
|
|
|
|
TimeRange: &timeRange,
|
|
|
|
}
|
2017-12-22 05:22:42 -06:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
models, err := executor.parseQuery(dsInfo, queryContext)
|
2020-12-04 01:49:39 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, time.Second*30, models[0].Step)
|
|
|
|
})
|
2017-11-15 04:22:00 -06:00
|
|
|
|
2020-12-04 01:49:39 -06:00
|
|
|
t.Run("parsing query model without step parameter", func(t *testing.T) {
|
|
|
|
json := `{
|
2017-11-15 04:22:00 -06:00
|
|
|
"expr": "go_goroutines",
|
|
|
|
"format": "time_series",
|
|
|
|
"intervalFactor": 1,
|
|
|
|
"refId": "A"
|
|
|
|
}`
|
2020-12-04 01:49:39 -06:00
|
|
|
jsonModel, _ := simplejson.NewJson([]byte(json))
|
2021-03-08 00:02:49 -06:00
|
|
|
queryModels := []plugins.DataSubQuery{
|
2020-12-04 01:49:39 -06:00
|
|
|
{Model: jsonModel},
|
|
|
|
}
|
2017-11-15 04:22:00 -06:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
timeRange := plugins.NewDataTimeRange("48h", "now")
|
|
|
|
queryContext := plugins.DataQuery{
|
|
|
|
Queries: queryModels,
|
|
|
|
TimeRange: &timeRange,
|
|
|
|
}
|
|
|
|
models, err := executor.parseQuery(dsInfo, queryContext)
|
2020-12-04 01:49:39 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, time.Minute*2, models[0].Step)
|
2017-12-22 05:22:42 -06:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
timeRange = plugins.NewDataTimeRange("1h", "now")
|
|
|
|
queryContext.TimeRange = &timeRange
|
|
|
|
models, err = executor.parseQuery(dsInfo, queryContext)
|
2020-12-04 01:49:39 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, time.Second*15, models[0].Step)
|
|
|
|
})
|
2017-11-15 04:22:00 -06:00
|
|
|
|
2020-12-04 01:49:39 -06:00
|
|
|
t.Run("parsing query model with high intervalFactor", func(t *testing.T) {
|
|
|
|
json := `{
|
2017-11-15 04:22:00 -06:00
|
|
|
"expr": "go_goroutines",
|
|
|
|
"format": "time_series",
|
|
|
|
"intervalFactor": 10,
|
|
|
|
"refId": "A"
|
|
|
|
}`
|
2020-12-04 01:49:39 -06:00
|
|
|
jsonModel, _ := simplejson.NewJson([]byte(json))
|
2021-03-08 00:02:49 -06:00
|
|
|
queryModels := []plugins.DataSubQuery{
|
2020-12-04 01:49:39 -06:00
|
|
|
{Model: jsonModel},
|
|
|
|
}
|
2017-11-15 04:22:00 -06:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
timeRange := plugins.NewDataTimeRange("48h", "now")
|
|
|
|
queryContext := plugins.DataQuery{
|
|
|
|
TimeRange: &timeRange,
|
|
|
|
Queries: queryModels,
|
|
|
|
}
|
2017-12-22 05:22:42 -06:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
models, err := executor.parseQuery(dsInfo, queryContext)
|
2020-12-04 01:49:39 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, time.Minute*20, models[0].Step)
|
|
|
|
})
|
2017-11-15 04:22:00 -06:00
|
|
|
|
2020-12-04 01:49:39 -06:00
|
|
|
t.Run("parsing query model with low intervalFactor", func(t *testing.T) {
|
|
|
|
json := `{
|
2017-11-15 04:22:00 -06:00
|
|
|
"expr": "go_goroutines",
|
|
|
|
"format": "time_series",
|
|
|
|
"intervalFactor": 1,
|
|
|
|
"refId": "A"
|
|
|
|
}`
|
2020-12-04 01:49:39 -06:00
|
|
|
jsonModel, _ := simplejson.NewJson([]byte(json))
|
2021-03-08 00:02:49 -06:00
|
|
|
queryModels := []plugins.DataSubQuery{
|
2020-12-04 01:49:39 -06:00
|
|
|
{Model: jsonModel},
|
|
|
|
}
|
2017-11-15 04:22:00 -06:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
timeRange := plugins.NewDataTimeRange("48h", "now")
|
|
|
|
queryContext := plugins.DataQuery{
|
|
|
|
TimeRange: &timeRange,
|
|
|
|
Queries: queryModels,
|
|
|
|
}
|
2017-12-22 05:22:42 -06:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
models, err := executor.parseQuery(dsInfo, queryContext)
|
2020-12-04 01:49:39 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, time.Minute*2, models[0].Step)
|
2016-09-21 04:17:29 -05:00
|
|
|
})
|
|
|
|
}
|