Skip to content

Commit

Permalink
Merge pull request #348 from zalando-incubator/pitr-zmon
Browse files Browse the repository at this point in the history
fixes for ZMON client
  • Loading branch information
jonathanbeber authored Jul 26, 2021
2 parents eefd5ef + 582c942 commit d1969a3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 9 additions & 13 deletions pkg/zmon/zmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{},
Expand Down Expand Up @@ -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)),
},
},
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions pkg/zmon/zmon_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zmon

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d1969a3

Please sign in to comment.