CloudMonitoring: Allow to set a custom value or disable graph_period (#48646)
This commit is contained in:
@@ -338,6 +338,7 @@ func (s *Service) buildQueryExecutors(req *backend.QueryDataRequest) ([]cloudMon
|
||||
IntervalMS: query.Interval.Milliseconds(),
|
||||
AliasBy: q.MetricQuery.AliasBy,
|
||||
timeRange: req.Queries[0].TimeRange,
|
||||
GraphPeriod: q.MetricQuery.GraphPeriod,
|
||||
}
|
||||
} else {
|
||||
cmtsf.AliasBy = q.MetricQuery.AliasBy
|
||||
|
||||
@@ -20,6 +20,21 @@ import (
|
||||
"github.com/grafana/grafana/pkg/tsdb/intervalv2"
|
||||
)
|
||||
|
||||
func (timeSeriesQuery cloudMonitoringTimeSeriesQuery) appendGraphPeriod(req *backend.QueryDataRequest) string {
|
||||
// GraphPeriod needs to be explicitly disabled.
|
||||
// If not set, the default behavior is to set an automatic value
|
||||
if timeSeriesQuery.GraphPeriod != "disabled" {
|
||||
graphPeriod := timeSeriesQuery.GraphPeriod
|
||||
if graphPeriod == "auto" || graphPeriod == "" {
|
||||
intervalCalculator := intervalv2.NewCalculator(intervalv2.CalculatorOptions{})
|
||||
interval := intervalCalculator.Calculate(req.Queries[0].TimeRange, time.Duration(timeSeriesQuery.IntervalMS/1000)*time.Second, req.Queries[0].MaxDataPoints)
|
||||
graphPeriod = interval.Text
|
||||
}
|
||||
return fmt.Sprintf(" | graph_period %s", graphPeriod)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (timeSeriesQuery cloudMonitoringTimeSeriesQuery) run(ctx context.Context, req *backend.QueryDataRequest,
|
||||
s *Service, dsInfo datasourceInfo, tracer tracing.Tracer) (*backend.DataResponse, cloudMonitoringResponse, string, error) {
|
||||
dr := &backend.DataResponse{}
|
||||
@@ -35,13 +50,11 @@ func (timeSeriesQuery cloudMonitoringTimeSeriesQuery) run(ctx context.Context, r
|
||||
slog.Info("No project name set on query, using project name from datasource", "projectName", projectName)
|
||||
}
|
||||
|
||||
intervalCalculator := intervalv2.NewCalculator(intervalv2.CalculatorOptions{})
|
||||
interval := intervalCalculator.Calculate(req.Queries[0].TimeRange, time.Duration(timeSeriesQuery.IntervalMS/1000)*time.Second, req.Queries[0].MaxDataPoints)
|
||||
|
||||
timeSeriesQuery.Query += timeSeriesQuery.appendGraphPeriod(req)
|
||||
from := req.Queries[0].TimeRange.From
|
||||
to := req.Queries[0].TimeRange.To
|
||||
timeFormat := "2006/01/02-15:04:05"
|
||||
timeSeriesQuery.Query += fmt.Sprintf(" | graph_period %s | within d'%s', d'%s'", interval.Text, from.UTC().Format(timeFormat), to.UTC().Format(timeFormat))
|
||||
timeSeriesQuery.Query += fmt.Sprintf(" | within d'%s', d'%s'", from.UTC().Format(timeFormat), to.UTC().Format(timeFormat))
|
||||
|
||||
buf, err := json.Marshal(map[string]interface{}{
|
||||
"query": timeSeriesQuery.Query,
|
||||
|
||||
@@ -101,4 +101,14 @@ func TestTimeSeriesQuery(t *testing.T) {
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, "6724404429462225363", labels["resource.label.instance_id"])
|
||||
})
|
||||
|
||||
t.Run("appends graph_period to the query", func(t *testing.T) {
|
||||
query := &cloudMonitoringTimeSeriesQuery{}
|
||||
assert.Equal(t, query.appendGraphPeriod(&backend.QueryDataRequest{Queries: []backend.DataQuery{{}}}), " | graph_period 10ms")
|
||||
})
|
||||
|
||||
t.Run("skips graph_period if disabled", func(t *testing.T) {
|
||||
query := &cloudMonitoringTimeSeriesQuery{GraphPeriod: "disabled"}
|
||||
assert.Equal(t, query.appendGraphPeriod(&backend.QueryDataRequest{Queries: []backend.DataQuery{{}}}), "")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ type (
|
||||
IntervalMS int64
|
||||
AliasBy string
|
||||
timeRange backend.TimeRange
|
||||
GraphPeriod string
|
||||
}
|
||||
|
||||
metricQuery struct {
|
||||
@@ -56,6 +57,7 @@ type (
|
||||
Query string
|
||||
Preprocessor string
|
||||
PreprocessorType preprocessorType
|
||||
GraphPeriod string
|
||||
}
|
||||
|
||||
sloQuery struct {
|
||||
|
||||
Reference in New Issue
Block a user