mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
stackdriver: pattern formatting for annotations
This commit is contained in:
parent
9351b56e57
commit
a63877bd4f
@ -2,6 +2,8 @@ package stackdriver
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/tsdb"
|
"github.com/grafana/grafana/pkg/tsdb"
|
||||||
@ -42,9 +44,9 @@ func (e *StackdriverExecutor) parseToAnnotations(queryRes *tsdb.QueryResult, dat
|
|||||||
|
|
||||||
annotation := make(map[string]string)
|
annotation := make(map[string]string)
|
||||||
annotation["time"] = point.Interval.EndTime.UTC().Format(time.RFC3339)
|
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["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)
|
annotations = append(annotations, annotation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,21 +78,40 @@ func transformAnnotationToTable(data []map[string]string, result *tsdb.QueryResu
|
|||||||
slog.Info("anno", "len", len(data))
|
slog.Info("anno", "len", len(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (e *StackdriverExecutor) buildAnnotationQuery(tsdbQuery *tsdb.TsdbQuery) (*StackdriverQuery, error) {
|
func formatAnnotationText(annotationText string, pointValue float64, metricType string, metricLabels map[string]string, resourceLabels map[string]string) string {
|
||||||
// firstQuery := queryContext.Queries[0]
|
result := legendKeyFormat.ReplaceAllFunc([]byte(annotationText), func(in []byte) []byte {
|
||||||
|
metaPartName := strings.Replace(string(in), "{{", "", 1)
|
||||||
|
metaPartName = strings.Replace(metaPartName, "}}", "", 1)
|
||||||
|
metaPartName = strings.TrimSpace(metaPartName)
|
||||||
|
|
||||||
// metricType := query.Model.Get("metricType").MustString()
|
if metaPartName == "metric.type" {
|
||||||
// filterParts := query.Model.Get("filters").MustArray()
|
return []byte(metricType)
|
||||||
// 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{
|
metricPart := replaceWithMetricPart(metaPartName, metricType)
|
||||||
// RefID: firstQuery.RefID,
|
|
||||||
// Params: params,
|
if metricPart != nil {
|
||||||
// Target: "",
|
return metricPart
|
||||||
// }, nil
|
}
|
||||||
// }
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
@ -19,14 +19,14 @@ func TestStackdriverAnnotationQuery(t *testing.T) {
|
|||||||
|
|
||||||
res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "annotationQuery"}
|
res := &tsdb.QueryResult{Meta: simplejson.New(), RefId: "annotationQuery"}
|
||||||
query := &StackdriverQuery{}
|
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)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
Convey("Should return annotations table", func() {
|
Convey("Should return annotations table", func() {
|
||||||
So(len(res.Tables), ShouldEqual, 1)
|
So(len(res.Tables), ShouldEqual, 1)
|
||||||
So(len(res.Tables[0].Rows), ShouldEqual, 9)
|
So(len(res.Tables[0].Rows), ShouldEqual, 9)
|
||||||
So(res.Tables[0].Rows[0][1], ShouldEqual, "atitle")
|
So(res.Tables[0].Rows[0][1], ShouldEqual, "atitle collector-asia-east-1 9.856650")
|
||||||
So(res.Tables[0].Rows[0][3], ShouldEqual, "atext")
|
So(res.Tables[0].Rows[0][3], ShouldEqual, "atext asia-east1-a")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user