Browse Source

stackdriver: pattern formatting for annotations

Daniel Lee 7 years ago
parent
commit
a63877bd4f

+ 41 - 20
pkg/tsdb/stackdriver/annotation_query.go

@@ -2,6 +2,8 @@ package stackdriver
 
 import (
 	"context"
+	"fmt"
+	"strings"
 	"time"
 
 	"github.com/grafana/grafana/pkg/tsdb"
@@ -42,9 +44,9 @@ func (e *StackdriverExecutor) parseToAnnotations(queryRes *tsdb.QueryResult, dat
 
 			annotation := make(map[string]string)
 			annotation["time"] = point.Interval.EndTime.UTC().Format(time.RFC3339)
-			annotation["title"] = title
+			annotation["title"] = formatAnnotationText(title, point.Value.DoubleValue, series.Metric.Type, series.Metric.Labels, series.Resource.Labels)
 			annotation["tags"] = tags
-			annotation["text"] = text
+			annotation["text"] = formatAnnotationText(text, point.Value.DoubleValue, series.Metric.Type, series.Metric.Labels, series.Resource.Labels)
 			annotations = append(annotations, annotation)
 		}
 	}
@@ -76,21 +78,40 @@ func transformAnnotationToTable(data []map[string]string, result *tsdb.QueryResu
 	slog.Info("anno", "len", len(data))
 }
 
-// func (e *StackdriverExecutor) buildAnnotationQuery(tsdbQuery *tsdb.TsdbQuery) (*StackdriverQuery, error) {
-// 	firstQuery := queryContext.Queries[0]
-
-// 	metricType := query.Model.Get("metricType").MustString()
-// 	filterParts := query.Model.Get("filters").MustArray()
-// 	filterString := buildFilterString(metricType, filterParts)
-// 	params := url.Values{}
-// 	params.Add("interval.startTime", startTime.UTC().Format(time.RFC3339))
-// 	params.Add("interval.endTime", endTime.UTC().Format(time.RFC3339))
-// 	params.Add("filter", buildFilterString(metricType, filterParts))
-// 	params.Add("view", "FULL")
-
-// 	return &StackdriverQuery{
-// 		RefID:  firstQuery.RefID,
-// 		Params: params,
-// 		Target: "",
-// 	}, nil
-// }
+func formatAnnotationText(annotationText string, pointValue float64, metricType string, metricLabels map[string]string, resourceLabels map[string]string) string {
+	result := legendKeyFormat.ReplaceAllFunc([]byte(annotationText), func(in []byte) []byte {
+		metaPartName := strings.Replace(string(in), "{{", "", 1)
+		metaPartName = strings.Replace(metaPartName, "}}", "", 1)
+		metaPartName = strings.TrimSpace(metaPartName)
+
+		if metaPartName == "metric.type" {
+			return []byte(metricType)
+		}
+
+		metricPart := replaceWithMetricPart(metaPartName, metricType)
+
+		if metricPart != nil {
+			return metricPart
+		}
+
+		if metaPartName == "value" {
+			return []byte(fmt.Sprintf("%f", pointValue))
+		}
+
+		metaPartName = strings.Replace(metaPartName, "metric.label.", "", 1)
+
+		if val, exists := metricLabels[metaPartName]; exists {
+			return []byte(val)
+		}
+
+		metaPartName = strings.Replace(metaPartName, "resource.label.", "", 1)
+
+		if val, exists := resourceLabels[metaPartName]; exists {
+			return []byte(val)
+		}
+
+		return in
+	})
+
+	return string(result)
+}

+ 3 - 3
pkg/tsdb/stackdriver/annotation_query_test.go

@@ -19,14 +19,14 @@ func TestStackdriverAnnotationQuery(t *testing.T) {
 
 			res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "annotationQuery"}
 			query := &StackdriverQuery{}
-			err = executor.parseToAnnotations(res, data, query, "atitle", "atext", "atag")
+			err = executor.parseToAnnotations(res, data, query, "atitle {{metric.label.instance_name}} {{value}}", "atext {{resource.label.zone}}", "atag")
 			So(err, ShouldBeNil)
 
 			Convey("Should return annotations table", func() {
 				So(len(res.Tables), ShouldEqual, 1)
 				So(len(res.Tables[0].Rows), ShouldEqual, 9)
-				So(res.Tables[0].Rows[0][1], ShouldEqual, "atitle")
-				So(res.Tables[0].Rows[0][3], ShouldEqual, "atext")
+				So(res.Tables[0].Rows[0][1], ShouldEqual, "atitle collector-asia-east-1 9.856650")
+				So(res.Tables[0].Rows[0][3], ShouldEqual, "atext asia-east1-a")
 			})
 		})
 	})