Ver Fonte

feat(mqe): add support for app and host alias

bergquist há 9 anos atrás
pai
commit
1b0005a9e5

+ 8 - 3
pkg/tsdb/mqe/mqe.go

@@ -50,6 +50,11 @@ func init() {
 	HttpClient = tsdb.GetDefaultClient()
 }
 
+type QueryToSend struct {
+	RawQuery string
+	QueryRef *MQEQuery
+}
+
 func (e *MQEExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, queryContext *tsdb.QueryContext) *tsdb.BatchResult {
 	result := &tsdb.BatchResult{}
 
@@ -67,7 +72,7 @@ func (e *MQEExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, quer
 		mqeQueries = append(mqeQueries, q)
 	}
 
-	var rawQueries []string
+	var rawQueries []QueryToSend
 	for _, v := range mqeQueries {
 		queries, err := v.Build(availableSeries.Metrics)
 		if err != nil {
@@ -81,14 +86,14 @@ func (e *MQEExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, quer
 	for _, v := range rawQueries {
 		glog.Info("Mqe executor", "query", v)
 
-		req, err := e.createRequest(v)
+		req, err := e.createRequest(v.RawQuery)
 
 		resp, err := ctxhttp.Do(ctx, HttpClient, req)
 		if err != nil {
 			return result.WithError(err)
 		}
 
-		series, err := e.ResponseParser.Parse(resp)
+		series, err := e.ResponseParser.Parse(resp, v.QueryRef)
 		if err != nil {
 			return result.WithError(err)
 		}

+ 13 - 2
pkg/tsdb/mqe/response_parser.go

@@ -48,7 +48,7 @@ type MQEResponseParser struct {
 	log log.Logger
 }
 
-func (parser *MQEResponseParser) Parse(res *http.Response) (*tsdb.QueryResult, error) {
+func (parser *MQEResponseParser) Parse(res *http.Response, queryRef *MQEQuery) (*tsdb.QueryResult, error) {
 	body, err := ioutil.ReadAll(res.Body)
 	defer res.Body.Close()
 	if err != nil {
@@ -74,7 +74,18 @@ func (parser *MQEResponseParser) Parse(res *http.Response) (*tsdb.QueryResult, e
 	var series tsdb.TimeSeriesSlice
 	for _, body := range data.Body {
 		for _, mqeSerie := range body.Series {
-			serie := &tsdb.TimeSeries{Name: body.Name}
+			namePrefix := ""
+
+			for key, value := range mqeSerie.Tagset {
+				if key == "app" && queryRef.AddAppToAlias {
+					namePrefix += value + " "
+				}
+				if key == "host" && queryRef.AddHostToAlias {
+					namePrefix += value + " "
+				}
+			}
+
+			serie := &tsdb.TimeSeries{Name: namePrefix + body.Name}
 
 			for i, value := range mqeSerie.Values {
 				timestamp := body.TimeRange.Start + int64(i)*body.TimeRange.Resolution

+ 7 - 2
pkg/tsdb/mqe/response_parser_test.go

@@ -20,15 +20,20 @@ func TestMQEResponseParser(t *testing.T) {
 		parser := NewResponseParser()
 
 		Convey("Can parse response", func() {
+			queryRef := &MQEQuery{
+				AddAppToAlias:  true,
+				AddHostToAlias: true,
+			}
+
 			response := &http.Response{
 				StatusCode: 200,
 				Body:       ioutil.NopCloser(strings.NewReader(dummieJson)),
 			}
-			res, err := parser.Parse(response)
+			res, err := parser.Parse(response, queryRef)
 			So(err, ShouldBeNil)
 			So(len(res.Series), ShouldEqual, 2)
 			So(len(res.Series[0].Points), ShouldEqual, 14)
-
+			So(res.Series[0].Name, ShouldEqual, "demoapp staples-lab-1 os.disk.sda3.weighted_io_time")
 			startTime := 1479287280000
 			for i := 0; i < 11; i++ {
 				So(res.Series[0].Points[i][0].Float64, ShouldEqual, i+1)

+ 36 - 27
pkg/tsdb/mqe/types.go

@@ -32,11 +32,27 @@ var (
 	containsWildcardPattern *regexp.Regexp = regexp.MustCompile(`\*`)
 )
 
-func (q *MQEQuery) Build(availableSeries []string) ([]string, error) {
-	var metrics []MQEMetric
+func (q *MQEQuery) Build(availableSeries []string) ([]QueryToSend, error) {
+	var queriesToSend []QueryToSend
+	where := q.buildWhereClause()
+
 	for _, v := range q.Metrics {
 		if !containsWildcardPattern.Match([]byte(v.Metric)) {
-			metrics = append(metrics, v)
+			alias := ""
+			if v.Alias != "" {
+				alias = fmt.Sprintf(" {%s}", v.Alias)
+			}
+			rawQuery := fmt.Sprintf(
+				"`%s`%s %s from %v to %v",
+				v.Metric,
+				alias,
+				where,
+				q.TimeRange.GetFromAsMsEpoch(),
+				q.TimeRange.GetToAsMsEpoch())
+			queriesToSend = append(queriesToSend, QueryToSend{
+				RawQuery: rawQuery,
+				QueryRef: q,
+			})
 			continue
 		}
 
@@ -51,34 +67,27 @@ 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, MQEMetric{
-					Metric: a,
-					Alias:  v.Alias,
+				alias := ""
+				if v.Alias != "" {
+					alias = fmt.Sprintf(" {%s}", v.Alias)
+				}
+
+				rawQuery := fmt.Sprintf(
+					"`%s`%s %s from %v to %v",
+					a,
+					alias,
+					where,
+					q.TimeRange.GetFromAsMsEpoch(),
+					q.TimeRange.GetToAsMsEpoch())
+
+				queriesToSend = append(queriesToSend, QueryToSend{
+					RawQuery: rawQuery,
+					QueryRef: q,
 				})
 			}
 		}
 	}
-
-	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 %s from %v to %v",
-				metric.Metric,
-				alias,
-				where,
-				q.TimeRange.GetFromAsMsEpoch(),
-				q.TimeRange.GetToAsMsEpoch()))
-	}
-
-	return queries, nil
+	return queriesToSend, nil
 }
 
 func (q *MQEQuery) buildWhereClause() string {

+ 7 - 7
pkg/tsdb/mqe/types_test.go

@@ -41,9 +41,9 @@ func TestWildcardExpansion(t *testing.T) {
 			expandeQueries, err := query.Build(availableMetrics)
 			So(err, ShouldBeNil)
 			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))
+			So(expandeQueries[0].RawQuery, 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].RawQuery, 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].RawQuery, 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() {
@@ -61,10 +61,10 @@ func TestWildcardExpansion(t *testing.T) {
 			So(err, ShouldBeNil)
 			So(len(expandeQueries), ShouldEqual, 4)
 
-			So(expandeQueries[0], ShouldEqual, fmt.Sprintf("`os.cpu.all.idle` where host in ('staples-lab-1') from %v to %v", from, to))
-			So(expandeQueries[1], ShouldEqual, fmt.Sprintf("`os.cpu.1.idle` where host in ('staples-lab-1') from %v to %v", from, to))
-			So(expandeQueries[2], ShouldEqual, fmt.Sprintf("`os.cpu.2.idle` where host in ('staples-lab-1') from %v to %v", from, to))
-			So(expandeQueries[3], ShouldEqual, fmt.Sprintf("`os.cpu.3.idle` where host in ('staples-lab-1') from %v to %v", from, to))
+			So(expandeQueries[0].RawQuery, ShouldEqual, fmt.Sprintf("`os.cpu.all.idle` where host in ('staples-lab-1') from %v to %v", from, to))
+			So(expandeQueries[1].RawQuery, ShouldEqual, fmt.Sprintf("`os.cpu.1.idle` where host in ('staples-lab-1') from %v to %v", from, to))
+			So(expandeQueries[2].RawQuery, ShouldEqual, fmt.Sprintf("`os.cpu.2.idle` where host in ('staples-lab-1') from %v to %v", from, to))
+			So(expandeQueries[3].RawQuery, ShouldEqual, fmt.Sprintf("`os.cpu.3.idle` where host in ('staples-lab-1') from %v to %v", from, to))
 		})
 	})
 }