diff --git a/pkg/tsdb/cloudmonitoring/annotation_query.go b/pkg/tsdb/cloudmonitoring/annotation_query.go index 603d478c71a..16ac1fc7352 100644 --- a/pkg/tsdb/cloudmonitoring/annotation_query.go +++ b/pkg/tsdb/cloudmonitoring/annotation_query.go @@ -3,6 +3,7 @@ package cloudmonitoring import ( "context" "encoding/json" + "strconv" "strings" "time" @@ -45,24 +46,46 @@ func (s *Service) executeAnnotationQuery(ctx context.Context, logger log.Logger, if err != nil { return resp, nil } - err = queries[0].parseToAnnotations(queryRes, dr, mq.MetricQuery.Title, mq.MetricQuery.Text) + err = parseToAnnotations(req.Queries[0].RefID, queryRes, dr, mq.MetricQuery.Title, mq.MetricQuery.Text) resp.Responses[firstQuery.RefID] = *queryRes return resp, err } -func (timeSeriesQuery cloudMonitoringTimeSeriesQuery) transformAnnotationToFrame(annotations []*annotationEvent, result *backend.DataResponse) { - frame := data.NewFrame(timeSeriesQuery.RefID, +func parseToAnnotations(refID string, dr *backend.DataResponse, + response cloudMonitoringResponse, title, text string) error { + frame := data.NewFrame(refID, data.NewField("time", nil, []time.Time{}), data.NewField("title", nil, []string{}), data.NewField("tags", nil, []string{}), data.NewField("text", nil, []string{}), ) - for _, a := range annotations { - frame.AppendRow(a.Time, a.Title, a.Tags, a.Text) + + for _, series := range response.TimeSeries { + if len(series.Points) == 0 { + continue + } + + for i := len(series.Points) - 1; i >= 0; i-- { + point := series.Points[i] + value := strconv.FormatFloat(point.Value.DoubleValue, 'f', 6, 64) + if series.ValueType == "STRING" { + value = point.Value.StringValue + } + annotation := &annotationEvent{ + Time: point.Interval.EndTime, + Title: formatAnnotationText(title, value, series.Metric.Type, + series.Metric.Labels, series.Resource.Labels), + Tags: "", + Text: formatAnnotationText(text, value, series.Metric.Type, + series.Metric.Labels, series.Resource.Labels), + } + frame.AppendRow(annotation.Time, annotation.Title, annotation.Tags, annotation.Text) + } } - result.Frames = append(result.Frames, frame) - timeSeriesQuery.logger.Info("anno", "len", len(annotations)) + dr.Frames = append(dr.Frames, frame) + + return nil } func formatAnnotationText(annotationText string, pointValue string, metricType string, metricLabels map[string]string, resourceLabels map[string]string) string { diff --git a/pkg/tsdb/cloudmonitoring/annotation_query_test.go b/pkg/tsdb/cloudmonitoring/annotation_query_test.go index e502067ce10..6e2c4cdb45c 100644 --- a/pkg/tsdb/cloudmonitoring/annotation_query_test.go +++ b/pkg/tsdb/cloudmonitoring/annotation_query_test.go @@ -14,9 +14,8 @@ func TestExecutor_parseToAnnotations(t *testing.T) { require.Len(t, d.TimeSeries, 3) res := &backend.DataResponse{} - query := &cloudMonitoringTimeSeriesFilter{} - err = query.parseToAnnotations(res, d, "atitle {{metric.label.instance_name}} {{metric.value}}", + err = parseToAnnotations("anno", res, d, "atitle {{metric.label.instance_name}} {{metric.value}}", "atext {{resource.label.zone}}") require.NoError(t, err) @@ -33,13 +32,12 @@ func TestExecutor_parseToAnnotations(t *testing.T) { func TestCloudMonitoringExecutor_parseToAnnotations_emptyTimeSeries(t *testing.T) { res := &backend.DataResponse{} - query := &cloudMonitoringTimeSeriesFilter{} response := cloudMonitoringResponse{ TimeSeries: []timeSeries{}, } - err := query.parseToAnnotations(res, response, "atitle", "atext") + err := parseToAnnotations("anno", res, response, "atitle", "atext") require.NoError(t, err) require.Len(t, res.Frames, 1) @@ -55,7 +53,6 @@ func TestCloudMonitoringExecutor_parseToAnnotations_emptyTimeSeries(t *testing.T func TestCloudMonitoringExecutor_parseToAnnotations_noPointsInSeries(t *testing.T) { res := &backend.DataResponse{} - query := &cloudMonitoringTimeSeriesFilter{} response := cloudMonitoringResponse{ TimeSeries: []timeSeries{ @@ -63,7 +60,7 @@ func TestCloudMonitoringExecutor_parseToAnnotations_noPointsInSeries(t *testing. }, } - err := query.parseToAnnotations(res, response, "atitle", "atext") + err := parseToAnnotations("anno", res, response, "atitle", "atext") require.NoError(t, err) require.Len(t, res.Frames, 1) diff --git a/pkg/tsdb/cloudmonitoring/time_series_filter.go b/pkg/tsdb/cloudmonitoring/time_series_filter.go index 38fdd2c53a6..011d9b1a3f4 100644 --- a/pkg/tsdb/cloudmonitoring/time_series_filter.go +++ b/pkg/tsdb/cloudmonitoring/time_series_filter.go @@ -265,42 +265,6 @@ func (timeSeriesFilter *cloudMonitoringTimeSeriesFilter) handleNonDistributionSe setDisplayNameAsFieldName(dataField) } -func (timeSeriesFilter *cloudMonitoringTimeSeriesFilter) parseToAnnotations(dr *backend.DataResponse, - response cloudMonitoringResponse, title, text string) error { - frame := data.NewFrame(timeSeriesFilter.RefID, - data.NewField("time", nil, []time.Time{}), - data.NewField("title", nil, []string{}), - data.NewField("tags", nil, []string{}), - data.NewField("text", nil, []string{}), - ) - - for _, series := range response.TimeSeries { - if len(series.Points) == 0 { - continue - } - - for i := len(series.Points) - 1; i >= 0; i-- { - point := series.Points[i] - value := strconv.FormatFloat(point.Value.DoubleValue, 'f', 6, 64) - if series.ValueType == "STRING" { - value = point.Value.StringValue - } - annotation := &annotationEvent{ - Time: point.Interval.EndTime, - Title: formatAnnotationText(title, value, series.Metric.Type, - series.Metric.Labels, series.Resource.Labels), - Tags: "", - Text: formatAnnotationText(text, value, series.Metric.Type, - series.Metric.Labels, series.Resource.Labels), - } - frame.AppendRow(annotation.Time, annotation.Title, annotation.Tags, annotation.Text) - } - } - dr.Frames = append(dr.Frames, frame) - - return nil -} - func (timeSeriesFilter *cloudMonitoringTimeSeriesFilter) buildDeepLink() string { if timeSeriesFilter.Slo != "" { return "" diff --git a/pkg/tsdb/cloudmonitoring/time_series_query.go b/pkg/tsdb/cloudmonitoring/time_series_query.go index af0dbe4b6ed..05ca9505f51 100644 --- a/pkg/tsdb/cloudmonitoring/time_series_query.go +++ b/pkg/tsdb/cloudmonitoring/time_series_query.go @@ -286,58 +286,6 @@ func (timeSeriesQuery *cloudMonitoringTimeSeriesQuery) parseResponse(queryRes *b return nil } -func (timeSeriesQuery *cloudMonitoringTimeSeriesQuery) parseToAnnotations(queryRes *backend.DataResponse, - data cloudMonitoringResponse, title, text string) error { - annotations := make([]*annotationEvent, 0) - - for _, series := range data.TimeSeriesData { - metricLabels := make(map[string]string) - resourceLabels := make(map[string]string) - - for n, d := range data.TimeSeriesDescriptor.LabelDescriptors { - key := toSnakeCase(d.Key) - labelValue := series.LabelValues[n] - value := "" - switch d.ValueType { - case "BOOL": - strVal := strconv.FormatBool(labelValue.BoolValue) - value = strVal - case "INT64": - value = labelValue.Int64Value - default: - value = labelValue.StringValue - } - if strings.Index(key, "metric.") == 0 { - key = key[len("metric."):] - metricLabels[key] = value - } else if strings.Index(key, "resource.") == 0 { - key = key[len("resource."):] - resourceLabels[key] = value - } - } - - for n, d := range data.TimeSeriesDescriptor.PointDescriptors { - // reverse the order to be ascending - for i := len(series.PointData) - 1; i >= 0; i-- { - point := series.PointData[i] - value := strconv.FormatFloat(point.Values[n].DoubleValue, 'f', 6, 64) - if d.ValueType == "STRING" { - value = point.Values[n].StringValue - } - annotations = append(annotations, &annotationEvent{ - Time: point.TimeInterval.EndTime, - Title: formatAnnotationText(title, value, d.MetricKind, metricLabels, resourceLabels), - Tags: "", - Text: formatAnnotationText(text, value, d.MetricKind, metricLabels, resourceLabels), - }) - } - } - } - - timeSeriesQuery.transformAnnotationToFrame(annotations, queryRes) - return nil -} - func (timeSeriesQuery *cloudMonitoringTimeSeriesQuery) buildDeepLink() string { u, err := url.Parse("https://console.cloud.google.com/monitoring/metrics-explorer") if err != nil { diff --git a/pkg/tsdb/cloudmonitoring/types.go b/pkg/tsdb/cloudmonitoring/types.go index 0b09c652bea..02afc94d630 100644 --- a/pkg/tsdb/cloudmonitoring/types.go +++ b/pkg/tsdb/cloudmonitoring/types.go @@ -16,7 +16,6 @@ type ( run(ctx context.Context, req *backend.QueryDataRequest, s *Service, dsInfo datasourceInfo, tracer tracing.Tracer) ( *backend.DataResponse, cloudMonitoringResponse, string, error) parseResponse(dr *backend.DataResponse, data cloudMonitoringResponse, executedQueryString string) error - parseToAnnotations(dr *backend.DataResponse, data cloudMonitoringResponse, title, text string) error buildDeepLink() string getRefID() string }