Merge pull request #348 from zalando-incubator/pitr-zmon

fixes for ZMON client
This commit is contained in:
Jonathan Juares Beber
2021-07-26 15:57:11 +02:00
committed by GitHub
3 changed files with 43 additions and 15 deletions

View File

@@ -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 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 entities. This would be possible by using the `avg` aggregator. The default
aggregator is `last` which returns only the latest metric point from the aggregator is `last` which returns only the latest metric point from the
query. The supported aggregation functions are `avg`, `dev`, `count`, query. The supported aggregation functions are `avg`, `count`,
`first`, `last`, `max`, `min`, `sum`, `diff`. See the [KariosDB docs](https://kairosdb.github.io/docs/build/html/restapi/Aggregators.html) for `last`, `max`, `min`, `sum`, `diff`. See the [KariosDB docs](https://kairosdb.github.io/docs/build/html/restapi/Aggregators.html) for
details. details.
The `duration` defines the duration used for the timeseries query. E.g. if you The `duration` defines the duration used for the timeseries query. E.g. if you

View File

@@ -15,9 +15,7 @@ var (
// https://kairosdb.github.io/docs/build/html/restapi/Aggregators.html // https://kairosdb.github.io/docs/build/html/restapi/Aggregators.html
validAggregators = map[string]struct{}{ validAggregators = map[string]struct{}{
"avg": struct{}{}, "avg": struct{}{},
"dev": struct{}{},
"count": struct{}{}, "count": struct{}{},
"first": struct{}{},
"last": struct{}{}, "last": struct{}{},
"max": struct{}{}, "max": struct{}{},
"min": struct{}{}, "min": struct{}{},
@@ -112,17 +110,10 @@ func (c *Client) Query(checkID int, key string, tags map[string]string, aggregat
StartRelative: durationToSampling(duration), StartRelative: durationToSampling(duration),
Metrics: []metric{ Metrics: []metric{
{ {
Name: fmt.Sprintf("zmon.check.%d", checkID), Name: fmt.Sprintf("zmon.check.%d", checkID),
Limit: 10000, // maximum limit of ZMON Limit: 10000, // maximum limit of ZMON
Tags: tagsSlice, Tags: tagsSlice,
GroupBy: []tagGroup{ GroupBy: []tagGroup{},
{
Name: "tag",
Tags: []string{
"key",
},
},
},
Aggregators: make([]aggregator, 0, len(aggregators)), 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 // add key to query if defined
if key != "" { if key != "" {
query.Metrics[0].Tags["key"] = []string{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) 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("Content-Type", "application/json")
req.Header.Set("Accept", "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) resp, err := c.http.Do(req)
if err != nil { if err != nil {

View File

@@ -1,6 +1,7 @@
package zmon package zmon
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@@ -130,6 +131,37 @@ func TestQuery(tt *testing.T) {
tt.Run(ti.msg, func(t *testing.T) { tt.Run(ti.msg, func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc( ts := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) { 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) w.WriteHeader(ti.status)
_, err := w.Write([]byte(ti.body)) _, err := w.Write([]byte(ti.body))
assert.NoError(t, err) assert.NoError(t, err)