2021-11-05 07:09:22 -05:00
|
|
|
package cloudmonitoring
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
2022-12-07 09:32:54 -06:00
|
|
|
gdata "github.com/grafana/grafana-plugin-sdk-go/data"
|
2023-07-31 12:14:27 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/cloud-monitoring/kinds/dataquery"
|
2022-12-07 09:32:54 -06:00
|
|
|
|
2021-11-05 07:09:22 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTimeSeriesQuery(t *testing.T) {
|
2024-01-29 10:24:23 -06:00
|
|
|
service := &Service{}
|
2021-11-05 07:09:22 -05:00
|
|
|
t.Run("multiple point descriptor is returned", func(t *testing.T) {
|
|
|
|
data, err := loadTestFile("./test-data/8-series-response-mql-multiple-point-descriptors.json")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, len(data.TimeSeries))
|
|
|
|
assert.Equal(t, 1, len(data.TimeSeriesData))
|
|
|
|
assert.Equal(t, 3, len(data.TimeSeriesDescriptor.PointDescriptors))
|
|
|
|
fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC).In(time.Local)
|
|
|
|
|
|
|
|
t.Run("and alias template is not specified", func(t *testing.T) {
|
|
|
|
res := &backend.DataResponse{}
|
|
|
|
query := &cloudMonitoringTimeSeriesQuery{
|
2023-07-31 12:14:27 -05:00
|
|
|
parameters: &dataquery.TimeSeriesQuery{
|
2022-11-28 02:17:01 -06:00
|
|
|
ProjectName: "test-proj",
|
|
|
|
Query: "test-query",
|
|
|
|
},
|
2021-11-05 07:09:22 -05:00
|
|
|
timeRange: backend.TimeRange{
|
|
|
|
From: fromStart,
|
|
|
|
To: fromStart.Add(34 * time.Minute),
|
|
|
|
},
|
|
|
|
}
|
2024-01-29 10:24:23 -06:00
|
|
|
err = query.parseResponse(res, data, "", service.logger)
|
2021-11-05 07:09:22 -05:00
|
|
|
frames := res.Frames
|
2022-12-07 09:32:54 -06:00
|
|
|
assert.Equal(t, "grafana-prod asia-northeast1-c 6724404429462225363 200", frames[0].Fields[1].Name)
|
2021-11-05 07:09:22 -05:00
|
|
|
assert.Equal(t, 843302441.9, frames[0].Fields[1].At(0))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("and alias template is specified", func(t *testing.T) {
|
|
|
|
res := &backend.DataResponse{}
|
|
|
|
query := &cloudMonitoringTimeSeriesQuery{
|
2023-07-31 12:14:27 -05:00
|
|
|
parameters: &dataquery.TimeSeriesQuery{
|
2022-11-28 02:17:01 -06:00
|
|
|
ProjectName: "test-proj",
|
|
|
|
Query: "test-query",
|
|
|
|
},
|
|
|
|
aliasBy: "{{project}} - {{resource.label.zone}} - {{resource.label.instance_id}} - {{metric.label.response_code_class}}",
|
2021-11-05 07:09:22 -05:00
|
|
|
timeRange: backend.TimeRange{
|
|
|
|
From: fromStart,
|
|
|
|
To: fromStart.Add(34 * time.Minute),
|
|
|
|
},
|
|
|
|
}
|
2024-01-29 10:24:23 -06:00
|
|
|
err = query.parseResponse(res, data, "", service.logger)
|
2021-11-05 07:09:22 -05:00
|
|
|
frames := res.Frames
|
|
|
|
assert.Equal(t, "test-proj - asia-northeast1-c - 6724404429462225363 - 200", frames[0].Fields[1].Name)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("single point descriptor is returned", func(t *testing.T) {
|
|
|
|
data, err := loadTestFile("./test-data/7-series-response-mql.json")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, len(data.TimeSeries))
|
|
|
|
assert.Equal(t, 1, len(data.TimeSeriesData))
|
|
|
|
assert.Equal(t, 1, len(data.TimeSeriesDescriptor.PointDescriptors))
|
|
|
|
|
|
|
|
t.Run("and alias by is expanded", func(t *testing.T) {
|
|
|
|
fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC).In(time.Local)
|
|
|
|
|
|
|
|
res := &backend.DataResponse{}
|
|
|
|
query := &cloudMonitoringTimeSeriesQuery{
|
2023-07-31 12:14:27 -05:00
|
|
|
parameters: &dataquery.TimeSeriesQuery{
|
2022-11-28 02:17:01 -06:00
|
|
|
ProjectName: "test-proj",
|
|
|
|
Query: "test-query",
|
|
|
|
},
|
|
|
|
aliasBy: "{{project}} - {{resource.label.zone}} - {{resource.label.instance_id}} - {{metric.label.response_code_class}}",
|
2021-11-05 07:09:22 -05:00
|
|
|
timeRange: backend.TimeRange{
|
|
|
|
From: fromStart,
|
|
|
|
To: fromStart.Add(34 * time.Minute),
|
|
|
|
},
|
|
|
|
}
|
2024-01-29 10:24:23 -06:00
|
|
|
err = query.parseResponse(res, data, "", service.logger)
|
2021-11-05 07:09:22 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
frames := res.Frames
|
|
|
|
assert.Equal(t, 1, len(res.Frames))
|
|
|
|
assert.Equal(t, "test-proj - asia-northeast1-c - 6724404429462225363 - 200", frames[0].Fields[1].Name)
|
|
|
|
})
|
|
|
|
})
|
2022-01-26 05:02:42 -06:00
|
|
|
|
|
|
|
t.Run("Parse labels", func(t *testing.T) {
|
|
|
|
data, err := loadTestFile("./test-data/7-series-response-mql.json")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC).In(time.Local)
|
|
|
|
res := &backend.DataResponse{}
|
|
|
|
query := &cloudMonitoringTimeSeriesQuery{
|
2023-07-31 12:14:27 -05:00
|
|
|
parameters: &dataquery.TimeSeriesQuery{
|
2022-11-28 02:17:01 -06:00
|
|
|
ProjectName: "test-proj",
|
|
|
|
Query: "test-query",
|
|
|
|
},
|
2022-01-26 05:02:42 -06:00
|
|
|
timeRange: backend.TimeRange{
|
|
|
|
From: fromStart,
|
|
|
|
To: fromStart.Add(34 * time.Minute),
|
|
|
|
},
|
|
|
|
}
|
2024-01-29 10:24:23 -06:00
|
|
|
err = query.parseResponse(res, data, "", service.logger)
|
2022-01-26 05:02:42 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
frames := res.Frames
|
2023-08-30 10:46:47 -05:00
|
|
|
custom, ok := frames[0].Meta.Custom.(map[string]any)
|
2022-01-26 05:02:42 -06:00
|
|
|
require.True(t, ok)
|
2022-12-07 09:32:54 -06:00
|
|
|
labels, ok := custom["labels"].(gdata.Labels)
|
2022-01-26 05:02:42 -06:00
|
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, "6724404429462225363", labels["resource.label.instance_id"])
|
2023-06-08 11:30:22 -05:00
|
|
|
assert.Equal(t, "test-app", labels["metadata.label.app"])
|
2022-01-26 05:02:42 -06:00
|
|
|
})
|
2022-05-09 06:43:10 -05:00
|
|
|
|
2022-10-31 08:48:32 -05:00
|
|
|
t.Run("includes time interval", func(t *testing.T) {
|
|
|
|
data, err := loadTestFile("./test-data/7-series-response-mql.json")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC).In(time.Local)
|
|
|
|
res := &backend.DataResponse{}
|
|
|
|
query := &cloudMonitoringTimeSeriesQuery{
|
2023-07-31 12:14:27 -05:00
|
|
|
parameters: &dataquery.TimeSeriesQuery{
|
2022-11-28 02:17:01 -06:00
|
|
|
ProjectName: "test-proj",
|
|
|
|
Query: "test-query",
|
2023-07-31 12:14:27 -05:00
|
|
|
GraphPeriod: strPtr("60s"),
|
2022-11-28 02:17:01 -06:00
|
|
|
},
|
2022-10-31 08:48:32 -05:00
|
|
|
timeRange: backend.TimeRange{
|
|
|
|
From: fromStart,
|
|
|
|
To: fromStart.Add(34 * time.Minute),
|
|
|
|
},
|
|
|
|
}
|
2024-01-29 10:24:23 -06:00
|
|
|
err = query.parseResponse(res, data, "", service.logger)
|
2022-10-31 08:48:32 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
frames := res.Frames
|
|
|
|
timeField := frames[0].Fields[0]
|
|
|
|
assert.Equal(t, float64(60*1000), timeField.Config.Interval)
|
|
|
|
})
|
|
|
|
|
2022-05-09 06:43:10 -05:00
|
|
|
t.Run("appends graph_period to the query", func(t *testing.T) {
|
2023-07-31 12:14:27 -05:00
|
|
|
query := &cloudMonitoringTimeSeriesQuery{parameters: &dataquery.TimeSeriesQuery{}}
|
2022-06-29 00:39:50 -05:00
|
|
|
assert.Equal(t, query.appendGraphPeriod(&backend.QueryDataRequest{Queries: []backend.DataQuery{{}}}), " | graph_period 1ms")
|
2022-05-09 06:43:10 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("skips graph_period if disabled", func(t *testing.T) {
|
2023-07-31 12:14:27 -05:00
|
|
|
query := &cloudMonitoringTimeSeriesQuery{parameters: &dataquery.TimeSeriesQuery{GraphPeriod: strPtr("disabled")}}
|
2022-05-09 06:43:10 -05:00
|
|
|
assert.Equal(t, query.appendGraphPeriod(&backend.QueryDataRequest{Queries: []backend.DataQuery{{}}}), "")
|
|
|
|
})
|
2024-10-11 05:31:37 -05:00
|
|
|
|
|
|
|
t.Run("time field is appropriately named", func(t *testing.T) {
|
|
|
|
res := &backend.DataResponse{}
|
|
|
|
data, err := loadTestFile("./test-data/7-series-response-mql.json")
|
|
|
|
require.NoError(t, err)
|
|
|
|
fromStart := time.Date(2018, 3, 15, 13, 0, 0, 0, time.UTC).In(time.Local)
|
|
|
|
query := &cloudMonitoringTimeSeriesQuery{
|
|
|
|
parameters: &dataquery.TimeSeriesQuery{
|
|
|
|
ProjectName: "test-proj",
|
|
|
|
Query: "test-query",
|
|
|
|
},
|
|
|
|
aliasBy: "",
|
|
|
|
timeRange: backend.TimeRange{
|
|
|
|
From: fromStart,
|
|
|
|
To: fromStart.Add(34 * time.Minute),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = query.parseResponse(res, data, "", service.logger)
|
|
|
|
require.NoError(t, err)
|
|
|
|
frames := res.Frames
|
|
|
|
assert.Equal(t, gdata.TimeSeriesTimeFieldName, frames[0].Fields[0].Name)
|
|
|
|
})
|
2021-11-05 07:09:22 -05:00
|
|
|
}
|