From a9803e9ecb18d9d5fd10c38c7a0d104bdd8b6fdc Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Thu, 8 Nov 2018 09:11:38 -0500 Subject: [PATCH] Correctly skip mismatching targets Signed-off-by: Fabian Reinartz --- web/api/v1/api.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/web/api/v1/api.go b/web/api/v1/api.go index d1d40e078..bb883eefa 100644 --- a/web/api/v1/api.go +++ b/web/api/v1/api.go @@ -573,6 +573,15 @@ func (api *API) targets(r *http.Request) (interface{}, *apiError, func()) { return res, nil, nil } +func matchLabels(lset labels.Labels, matchers []*labels.Matcher) bool { + for _, m := range matchers { + if !m.Matches(lset.Get(m.Name)) { + return false + } + } + return true +} + func (api *API) targetMetadata(r *http.Request) (interface{}, *apiError, func()) { limit := -1 if s := r.FormValue("limit"); s != "" { @@ -590,17 +599,14 @@ func (api *API) targetMetadata(r *http.Request) (interface{}, *apiError, func()) metric := r.FormValue("metric") var res []metricMetadata -Outer: for _, tt := range api.targetRetriever.TargetsActive() { for _, t := range tt { if limit >= 0 && len(res) >= limit { break } - for _, m := range matchers { - // Filter targets that don't satisfy the label matchers. - if !m.Matches(t.Labels().Get(m.Name)) { - continue Outer - } + // Filter targets that don't satisfy the label matchers. + if !matchLabels(t.Labels(), matchers) { + continue } // If no metric is specified, get the full list for the target. if metric == "" {