diff --git a/pkg/tsdb/mqe/types.go b/pkg/tsdb/mqe/types.go index 34e643adc8f..ba50b02ba04 100644 --- a/pkg/tsdb/mqe/types.go +++ b/pkg/tsdb/mqe/types.go @@ -33,14 +33,10 @@ var ( ) func (q *MQEQuery) Build(availableSeries []string) ([]string, error) { - var queries []string - where := q.buildWhereClause() - - var metrics []string - + var metrics []MQEMetric for _, v := range q.Metrics { if !containsWildcardPattern.Match([]byte(v.Metric)) { - metrics = append(metrics, v.Metric) + metrics = append(metrics, v) continue } @@ -55,16 +51,28 @@ func (q *MQEQuery) Build(availableSeries []string) ([]string, error) { //TODO: this lookup should be cached for _, a := range availableSeries { if mp.Match([]byte(a)) { - metrics = append(metrics, a) + metrics = append(metrics, MQEMetric{ + Metric: a, + Alias: v.Alias, + }) } } } + var queries []string + where := q.buildWhereClause() + for _, metric := range metrics { + alias := "" + if metric.Alias != "" { + alias = fmt.Sprintf(" {%s}", metric.Alias) + } + queries = append(queries, fmt.Sprintf( - "`%s` %s from %v to %v", - metric, + "`%s`%s %s from %v to %v", + metric.Metric, + alias, where, q.TimeRange.GetFromAsMsEpoch(), q.TimeRange.GetToAsMsEpoch())) diff --git a/pkg/tsdb/mqe/types_test.go b/pkg/tsdb/mqe/types_test.go index c136599cb93..c01b229e72e 100644 --- a/pkg/tsdb/mqe/types_test.go +++ b/pkg/tsdb/mqe/types_test.go @@ -30,11 +30,15 @@ func TestWildcardExpansion(t *testing.T) { Metrics: []MQEMetric{ MQEMetric{ Metric: "os.cpu.3.idle", - Alias: "cpu on core 3", + Alias: "", }, MQEMetric{ Metric: "os.cpu.2.idle", - Alias: "cpu on core 2", + Alias: "", + }, + MQEMetric{ + Metric: "os.cpu.1.idle", + Alias: "cpu", }, }, Hosts: []string{"staples-lab-1", "staples-lab-2"}, @@ -46,9 +50,10 @@ func TestWildcardExpansion(t *testing.T) { expandeQueries, err := query.Build(availableMetrics) So(err, ShouldBeNil) - So(len(expandeQueries), ShouldEqual, 2) + So(len(expandeQueries), ShouldEqual, 3) So(expandeQueries[0], ShouldEqual, fmt.Sprintf("`os.cpu.3.idle` where app in ('demoapp-1', 'demoapp-2') and host in ('staples-lab-1', 'staples-lab-2') from %v to %v", from, to)) So(expandeQueries[1], ShouldEqual, fmt.Sprintf("`os.cpu.2.idle` where app in ('demoapp-1', 'demoapp-2') and host in ('staples-lab-1', 'staples-lab-2') from %v to %v", from, to)) + So(expandeQueries[2], ShouldEqual, fmt.Sprintf("`os.cpu.1.idle` {cpu} where app in ('demoapp-1', 'demoapp-2') and host in ('staples-lab-1', 'staples-lab-2') from %v to %v", from, to)) }) Convey("Containg wildcard series", func() {