Skip to content

Commit 1b0005a

Browse files
committed
feat(mqe): add support for app and host alias
1 parent 5c153f0 commit 1b0005a

File tree

5 files changed

+71
-41
lines changed

5 files changed

+71
-41
lines changed

pkg/tsdb/mqe/mqe.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ func init() {
5050
HttpClient = tsdb.GetDefaultClient()
5151
}
5252

53+
type QueryToSend struct {
54+
RawQuery string
55+
QueryRef *MQEQuery
56+
}
57+
5358
func (e *MQEExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, queryContext *tsdb.QueryContext) *tsdb.BatchResult {
5459
result := &tsdb.BatchResult{}
5560

@@ -67,7 +72,7 @@ func (e *MQEExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, quer
6772
mqeQueries = append(mqeQueries, q)
6873
}
6974

70-
var rawQueries []string
75+
var rawQueries []QueryToSend
7176
for _, v := range mqeQueries {
7277
queries, err := v.Build(availableSeries.Metrics)
7378
if err != nil {
@@ -81,14 +86,14 @@ func (e *MQEExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, quer
8186
for _, v := range rawQueries {
8287
glog.Info("Mqe executor", "query", v)
8388

84-
req, err := e.createRequest(v)
89+
req, err := e.createRequest(v.RawQuery)
8590

8691
resp, err := ctxhttp.Do(ctx, HttpClient, req)
8792
if err != nil {
8893
return result.WithError(err)
8994
}
9095

91-
series, err := e.ResponseParser.Parse(resp)
96+
series, err := e.ResponseParser.Parse(resp, v.QueryRef)
9297
if err != nil {
9398
return result.WithError(err)
9499
}

pkg/tsdb/mqe/response_parser.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type MQEResponseParser struct {
4848
log log.Logger
4949
}
5050

51-
func (parser *MQEResponseParser) Parse(res *http.Response) (*tsdb.QueryResult, error) {
51+
func (parser *MQEResponseParser) Parse(res *http.Response, queryRef *MQEQuery) (*tsdb.QueryResult, error) {
5252
body, err := ioutil.ReadAll(res.Body)
5353
defer res.Body.Close()
5454
if err != nil {
@@ -74,7 +74,18 @@ func (parser *MQEResponseParser) Parse(res *http.Response) (*tsdb.QueryResult, e
7474
var series tsdb.TimeSeriesSlice
7575
for _, body := range data.Body {
7676
for _, mqeSerie := range body.Series {
77-
serie := &tsdb.TimeSeries{Name: body.Name}
77+
namePrefix := ""
78+
79+
for key, value := range mqeSerie.Tagset {
80+
if key == "app" && queryRef.AddAppToAlias {
81+
namePrefix += value + " "
82+
}
83+
if key == "host" && queryRef.AddHostToAlias {
84+
namePrefix += value + " "
85+
}
86+
}
87+
88+
serie := &tsdb.TimeSeries{Name: namePrefix + body.Name}
7889

7990
for i, value := range mqeSerie.Values {
8091
timestamp := body.TimeRange.Start + int64(i)*body.TimeRange.Resolution

pkg/tsdb/mqe/response_parser_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@ func TestMQEResponseParser(t *testing.T) {
2020
parser := NewResponseParser()
2121

2222
Convey("Can parse response", func() {
23+
queryRef := &MQEQuery{
24+
AddAppToAlias: true,
25+
AddHostToAlias: true,
26+
}
27+
2328
response := &http.Response{
2429
StatusCode: 200,
2530
Body: ioutil.NopCloser(strings.NewReader(dummieJson)),
2631
}
27-
res, err := parser.Parse(response)
32+
res, err := parser.Parse(response, queryRef)
2833
So(err, ShouldBeNil)
2934
So(len(res.Series), ShouldEqual, 2)
3035
So(len(res.Series[0].Points), ShouldEqual, 14)
31-
36+
So(res.Series[0].Name, ShouldEqual, "demoapp staples-lab-1 os.disk.sda3.weighted_io_time")
3237
startTime := 1479287280000
3338
for i := 0; i < 11; i++ {
3439
So(res.Series[0].Points[i][0].Float64, ShouldEqual, i+1)

pkg/tsdb/mqe/types.go

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,27 @@ var (
3232
containsWildcardPattern *regexp.Regexp = regexp.MustCompile(`\*`)
3333
)
3434

35-
func (q *MQEQuery) Build(availableSeries []string) ([]string, error) {
36-
var metrics []MQEMetric
35+
func (q *MQEQuery) Build(availableSeries []string) ([]QueryToSend, error) {
36+
var queriesToSend []QueryToSend
37+
where := q.buildWhereClause()
38+
3739
for _, v := range q.Metrics {
3840
if !containsWildcardPattern.Match([]byte(v.Metric)) {
39-
metrics = append(metrics, v)
41+
alias := ""
42+
if v.Alias != "" {
43+
alias = fmt.Sprintf(" {%s}", v.Alias)
44+
}
45+
rawQuery := fmt.Sprintf(
46+
"`%s`%s %s from %v to %v",
47+
v.Metric,
48+
alias,
49+
where,
50+
q.TimeRange.GetFromAsMsEpoch(),
51+
q.TimeRange.GetToAsMsEpoch())
52+
queriesToSend = append(queriesToSend, QueryToSend{
53+
RawQuery: rawQuery,
54+
QueryRef: q,
55+
})
4056
continue
4157
}
4258

@@ -51,34 +67,27 @@ func (q *MQEQuery) Build(availableSeries []string) ([]string, error) {
5167
//TODO: this lookup should be cached
5268
for _, a := range availableSeries {
5369
if mp.Match([]byte(a)) {
54-
metrics = append(metrics, MQEMetric{
55-
Metric: a,
56-
Alias: v.Alias,
70+
alias := ""
71+
if v.Alias != "" {
72+
alias = fmt.Sprintf(" {%s}", v.Alias)
73+
}
74+
75+
rawQuery := fmt.Sprintf(
76+
"`%s`%s %s from %v to %v",
77+
a,
78+
alias,
79+
where,
80+
q.TimeRange.GetFromAsMsEpoch(),
81+
q.TimeRange.GetToAsMsEpoch())
82+
83+
queriesToSend = append(queriesToSend, QueryToSend{
84+
RawQuery: rawQuery,
85+
QueryRef: q,
5786
})
5887
}
5988
}
6089
}
61-
62-
var queries []string
63-
where := q.buildWhereClause()
64-
65-
for _, metric := range metrics {
66-
alias := ""
67-
if metric.Alias != "" {
68-
alias = fmt.Sprintf(" {%s}", metric.Alias)
69-
}
70-
71-
queries = append(queries,
72-
fmt.Sprintf(
73-
"`%s`%s %s from %v to %v",
74-
metric.Metric,
75-
alias,
76-
where,
77-
q.TimeRange.GetFromAsMsEpoch(),
78-
q.TimeRange.GetToAsMsEpoch()))
79-
}
80-
81-
return queries, nil
90+
return queriesToSend, nil
8291
}
8392

8493
func (q *MQEQuery) buildWhereClause() string {

pkg/tsdb/mqe/types_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ func TestWildcardExpansion(t *testing.T) {
4141
expandeQueries, err := query.Build(availableMetrics)
4242
So(err, ShouldBeNil)
4343
So(len(expandeQueries), ShouldEqual, 3)
44-
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))
45-
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))
46-
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))
44+
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))
45+
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))
46+
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))
4747
})
4848

4949
Convey("Containg wildcard series", func() {
@@ -61,10 +61,10 @@ func TestWildcardExpansion(t *testing.T) {
6161
So(err, ShouldBeNil)
6262
So(len(expandeQueries), ShouldEqual, 4)
6363

64-
So(expandeQueries[0], ShouldEqual, fmt.Sprintf("`os.cpu.all.idle` where host in ('staples-lab-1') from %v to %v", from, to))
65-
So(expandeQueries[1], ShouldEqual, fmt.Sprintf("`os.cpu.1.idle` where host in ('staples-lab-1') from %v to %v", from, to))
66-
So(expandeQueries[2], ShouldEqual, fmt.Sprintf("`os.cpu.2.idle` where host in ('staples-lab-1') from %v to %v", from, to))
67-
So(expandeQueries[3], ShouldEqual, fmt.Sprintf("`os.cpu.3.idle` where host in ('staples-lab-1') from %v to %v", from, to))
64+
So(expandeQueries[0].RawQuery, ShouldEqual, fmt.Sprintf("`os.cpu.all.idle` where host in ('staples-lab-1') from %v to %v", from, to))
65+
So(expandeQueries[1].RawQuery, ShouldEqual, fmt.Sprintf("`os.cpu.1.idle` where host in ('staples-lab-1') from %v to %v", from, to))
66+
So(expandeQueries[2].RawQuery, ShouldEqual, fmt.Sprintf("`os.cpu.2.idle` where host in ('staples-lab-1') from %v to %v", from, to))
67+
So(expandeQueries[3].RawQuery, ShouldEqual, fmt.Sprintf("`os.cpu.3.idle` where host in ('staples-lab-1') from %v to %v", from, to))
6868
})
6969
})
7070
}

0 commit comments

Comments
 (0)