diff --git a/README.md b/README.md index a918dfd..ff6b6f0 100644 --- a/README.md +++ b/README.md @@ -587,8 +587,8 @@ For instance if you define the entity filter then you might want to get an average over the metrics for those three entities. This would be possible by using the `avg` aggregator. The default aggregator is `last` which returns only the latest metric point from the -query. The supported aggregation functions are `avg`, `dev`, `count`, -`first`, `last`, `max`, `min`, `sum`, `diff`. See the [KariosDB docs](https://kairosdb.github.io/docs/build/html/restapi/Aggregators.html) for +query. The supported aggregation functions are `avg`, `count`, +`last`, `max`, `min`, `sum`, `diff`. See the [KariosDB docs](https://kairosdb.github.io/docs/build/html/restapi/Aggregators.html) for details. The `duration` defines the duration used for the timeseries query. E.g. if you diff --git a/pkg/zmon/zmon.go b/pkg/zmon/zmon.go index 1501e16..6942d21 100644 --- a/pkg/zmon/zmon.go +++ b/pkg/zmon/zmon.go @@ -15,9 +15,7 @@ var ( // https://kairosdb.github.io/docs/build/html/restapi/Aggregators.html validAggregators = map[string]struct{}{ "avg": struct{}{}, - "dev": struct{}{}, "count": struct{}{}, - "first": struct{}{}, "last": struct{}{}, "max": struct{}{}, "min": struct{}{}, @@ -112,17 +110,10 @@ func (c *Client) Query(checkID int, key string, tags map[string]string, aggregat StartRelative: durationToSampling(duration), Metrics: []metric{ { - Name: fmt.Sprintf("zmon.check.%d", checkID), - Limit: 10000, // maximum limit of ZMON - Tags: tagsSlice, - GroupBy: []tagGroup{ - { - Name: "tag", - Tags: []string{ - "key", - }, - }, - }, + Name: fmt.Sprintf("zmon.check.%d", checkID), + Limit: 10000, // maximum limit of ZMON + Tags: tagsSlice, + GroupBy: []tagGroup{}, Aggregators: make([]aggregator, 0, len(aggregators)), }, }, @@ -142,6 +133,10 @@ func (c *Client) Query(checkID int, key string, tags map[string]string, aggregat // add key to query if defined if key != "" { query.Metrics[0].Tags["key"] = []string{key} + query.Metrics[0].GroupBy = append(query.Metrics[0].GroupBy, tagGroup{ + Name: "tag", + Tags: []string{"key"}, + }) } body, err := json.Marshal(&query) @@ -158,6 +153,7 @@ func (c *Client) Query(checkID int, key string, tags map[string]string, aggregat req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json") + req.Header.Set("X-Attribution", fmt.Sprintf("kube-metrics-adapter/%d", checkID)) resp, err := c.http.Do(req) if err != nil { diff --git a/pkg/zmon/zmon_test.go b/pkg/zmon/zmon_test.go index f3193b2..938bfe0 100644 --- a/pkg/zmon/zmon_test.go +++ b/pkg/zmon/zmon_test.go @@ -1,6 +1,7 @@ package zmon import ( + "encoding/json" "fmt" "net/http" "net/http/httptest" @@ -130,6 +131,37 @@ func TestQuery(tt *testing.T) { tt.Run(ti.msg, func(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { + if ti.status == http.StatusOK { + q := metricQuery{} + decoder := json.NewDecoder(r.Body) + err := decoder.Decode(&q) + assert.NoError(t, err) + + numberOfMetrics := len(q.Metrics) + assert.Equal(t, 1, numberOfMetrics, "expected 1 metrics, got %d", numberOfMetrics) + metric := q.Metrics[0] + if ti.key != "" { + numberOfTags := len(metric.Tags) + assert.Equal(t, 1, numberOfTags, "expected 1 metric, got %d", numberOfTags) + tag := metric.Tags["key"][0] + assert.Equal(t, ti.key, tag, "expected key '%s' as tag, got '%s'", ti.key, tag) + + numberOfTagGroups := len(metric.GroupBy) + assert.Equal(t, 1, numberOfTagGroups, "expected 1 GroupBy tag, got %d", numberOfTagGroups) + tagGroups := metric.GroupBy[0] + + numberOfTagGroupTags := len(tagGroups.Tags) + assert.Equal(t, 1, numberOfTagGroupTags, "expected 1 GroupBy tag, got %d", numberOfTagGroupTags) + + expectedGroupByTag := "key" + groupByTag := tagGroups.Tags[0] + assert.Equal(t, expectedGroupByTag, groupByTag, "expected GroupBy tag '%s', got '%s'", expectedGroupByTag, groupByTag) + } else { + _, ok := metric.Tags["key"] + assert.Equal(t, false, ok) + assert.Equal(t, 0, len(metric.GroupBy)) + } + } w.WriteHeader(ti.status) _, err := w.Write([]byte(ti.body)) assert.NoError(t, err)