瀏覽代碼

feat(mqe): support basic aliases

bergquist 9 年之前
父節點
當前提交
113020aabe
共有 2 個文件被更改,包括 25 次插入12 次删除
  1. 17 9
      pkg/tsdb/mqe/types.go
  2. 8 3
      pkg/tsdb/mqe/types_test.go

+ 17 - 9
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()))

+ 8 - 3
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() {