mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
TestData: attach labels to series results (#18653)
This commit is contained in:
44
pkg/tsdb/testdata/scenarios.go
vendored
44
pkg/tsdb/testdata/scenarios.go
vendored
@@ -423,6 +423,7 @@ func getPredictablePulse(query *tsdb.Query, context *tsdb.TsdbQuery) *tsdb.Query
|
||||
series := newSeriesForQuery(query)
|
||||
series.Points = *points
|
||||
queryRes.Series = append(queryRes.Series, series)
|
||||
attachLabels(query, queryRes)
|
||||
return queryRes
|
||||
}
|
||||
|
||||
@@ -472,6 +473,7 @@ func getPredictableCSVWave(query *tsdb.Query, context *tsdb.TsdbQuery) *tsdb.Que
|
||||
series := newSeriesForQuery(query)
|
||||
series.Points = *points
|
||||
queryRes.Series = append(queryRes.Series, series)
|
||||
attachLabels(query, queryRes)
|
||||
return queryRes
|
||||
}
|
||||
|
||||
@@ -517,9 +519,51 @@ func getRandomWalk(query *tsdb.Query, tsdbQuery *tsdb.TsdbQuery) *tsdb.QueryResu
|
||||
|
||||
queryRes := tsdb.NewQueryResult()
|
||||
queryRes.Series = append(queryRes.Series, series)
|
||||
attachLabels(query, queryRes)
|
||||
return queryRes
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks for a labels request and adds them as tags
|
||||
*
|
||||
* '{job="foo", instance="bar"} => {job: "foo", instance: "bar"}`
|
||||
*/
|
||||
func attachLabels(query *tsdb.Query, queryRes *tsdb.QueryResult) {
|
||||
labelText := query.Model.Get("labels").MustString("")
|
||||
if labelText == "" {
|
||||
return
|
||||
}
|
||||
|
||||
tags := parseLabels(labelText)
|
||||
for _, series := range queryRes.Series {
|
||||
series.Tags = tags
|
||||
}
|
||||
}
|
||||
|
||||
// generous parser:
|
||||
// {job="foo", instance="bar"}
|
||||
// job="foo", instance="bar"
|
||||
// job=foo, instance=bar
|
||||
// should all equal {job=foo, instance=bar}
|
||||
|
||||
func parseLabels(text string) map[string]string {
|
||||
var tags map[string]string
|
||||
text = strings.Trim(text, `{}`)
|
||||
if len(text) < 2 {
|
||||
return tags
|
||||
}
|
||||
tags = make(map[string]string)
|
||||
|
||||
for _, keyval := range strings.Split(text, ",") {
|
||||
idx := strings.Index(keyval, "=")
|
||||
key := strings.TrimSpace(keyval[:idx])
|
||||
val := strings.TrimSpace(keyval[idx+1:])
|
||||
tags[key] = val
|
||||
}
|
||||
|
||||
return tags
|
||||
}
|
||||
|
||||
func getRandomWalkTable(query *tsdb.Query, tsdbQuery *tsdb.TsdbQuery) *tsdb.QueryResult {
|
||||
timeWalkerMs := tsdbQuery.TimeRange.GetFromAsMsEpoch()
|
||||
to := tsdbQuery.TimeRange.GetToAsMsEpoch()
|
||||
|
||||
13
pkg/tsdb/testdata/scenarios_test.go
vendored
13
pkg/tsdb/testdata/scenarios_test.go
vendored
@@ -92,3 +92,16 @@ func TestTestdataScenarios(t *testing.T) {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestToLabels(t *testing.T) {
|
||||
Convey("read labels", t, func() {
|
||||
tags := make(map[string]string)
|
||||
tags["job"] = "foo"
|
||||
tags["instance"] = "bar"
|
||||
|
||||
So(parseLabels(`{job="foo", instance="bar"}`), ShouldEqual, tags)
|
||||
So(parseLabels(`job="foo", instance="bar"`), ShouldEqual, tags)
|
||||
So(parseLabels(`job=foo, instance=bar`), ShouldEqual, tags)
|
||||
So(parseLabels(`job = foo,instance = bar`), ShouldEqual, tags)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user