From 99f9b022836f49e2421f348f31ed301630a3fc71 Mon Sep 17 00:00:00 2001 From: Zakhar Bessarab Date: Thu, 5 Jan 2023 15:34:25 +0400 Subject: [PATCH] lib/promscrape/discovery/dockerswarm: fix query encoding of filters (#3586) Co-authored-by: Aliaksandr Valialkin --- docs/CHANGELOG.md | 1 + lib/promscrape/discovery/dockerswarm/api.go | 14 +++++--------- lib/promscrape/discovery/dockerswarm/api_test.go | 8 +++++++- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 2e0bbb241..844814b13 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -29,6 +29,7 @@ The following tip changes can be tested by building VictoriaMetrics components f * BUGFIX: properly parse floating-point numbers without integer or fractional parts such as `.123` and `20.` during [data import](https://docs.victoriametrics.com/#how-to-import-time-series-data). See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3544). * BUGFIX: [MetricsQL](https://docs.victoriametrics.com/MetricsQL.html): properly parse durations with uppercase suffixes such as `10S`, `5MS`, `1W`, etc. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3589). * BUGFIX: [vmagent](https://docs.victoriametrics.com/vmagent.html): fix a panic during target discovery when `vmagent` runs with `-promscrape.dropOriginalLabels` command-line flag. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3580). The bug has been introduced in [v1.85.0](https://docs.victoriametrics.com/CHANGELOG.html#v1850). +* BUGFIX: [vmagent](https://docs.victoriametrics.com/vmagent.html): [dockerswarm_sd_configs](https://docs.victoriametrics.com/sd_configs.html#dockerswarm_sd_configs): properly encode `filters` field. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3579) ## [v1.85.3](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.85.3) diff --git a/lib/promscrape/discovery/dockerswarm/api.go b/lib/promscrape/discovery/dockerswarm/api.go index d0bb79cef..35d2ab974 100644 --- a/lib/promscrape/discovery/dockerswarm/api.go +++ b/lib/promscrape/discovery/dockerswarm/api.go @@ -63,20 +63,16 @@ func (cfg *apiConfig) getAPIResponse(path string) ([]byte, error) { return cfg.client.GetAPIResponse(path) } +// Encodes filters as `map[string][]string` and then marshals it to JSON. +// Reference: https://docs.docker.com/engine/api/v1.41/#tag/Task func getFiltersQueryArg(filters []Filter) string { if len(filters) == 0 { return "" } - m := make(map[string]map[string]bool) + + m := make(map[string][]string) for _, f := range filters { - x := m[f.Name] - if x == nil { - x = make(map[string]bool) - m[f.Name] = x - } - for _, value := range f.Values { - x[value] = true - } + m[f.Name] = f.Values } buf, err := json.Marshal(m) if err != nil { diff --git a/lib/promscrape/discovery/dockerswarm/api_test.go b/lib/promscrape/discovery/dockerswarm/api_test.go index fc8888ee5..114ec43a4 100644 --- a/lib/promscrape/discovery/dockerswarm/api_test.go +++ b/lib/promscrape/discovery/dockerswarm/api_test.go @@ -22,5 +22,11 @@ func TestGetFiltersQueryArg(t *testing.T) { Name: "xxx", Values: []string{"aa"}, }, - }, "%7B%22name%22%3A%7B%22bar%22%3Atrue%2C%22foo%22%3Atrue%7D%2C%22xxx%22%3A%7B%22aa%22%3Atrue%7D%7D") + }, "%7B%22name%22%3A%5B%22foo%22%2C%22bar%22%5D%2C%22xxx%22%3A%5B%22aa%22%5D%7D") + f([]Filter{ + { + Name: "desired-state", + Values: []string{"running", "shutdown"}, + }, + }, "%7B%22desired-state%22%3A%5B%22running%22%2C%22shutdown%22%5D%7D") }