From 4addee2bee406ac7a47480279d2ae34061a645ca Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Wed, 14 Feb 2018 17:03:58 +0000 Subject: [PATCH] Fix honor_labels for empty labels, prune empty labels. The semantics of honor_labels are that if a target exposes and empty label it will override the target labels. This PR fixes that by once again distinguishing between empty labels and missing labels in this one use case. Beyond that empty labels should be pruned and not added to storage, which this also fixes. Fixes #3841 --- pkg/labels/labels.go | 10 ++++++++++ scrape/scrape.go | 8 +++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index 471677dbc2..00afd6aeaa 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -112,6 +112,16 @@ func (ls Labels) Get(name string) string { return "" } +// Has returns if the label with the given name is present. +func (ls Labels) Has(name string) bool { + for _, l := range ls { + if l.Name == name { + return true + } + } + return false +} + // Equal returns whether the two label sets are equal. func Equal(ls, o Labels) bool { if len(ls) != len(o) { diff --git a/scrape/scrape.go b/scrape/scrape.go index a22dcb9a36..2039108551 100644 --- a/scrape/scrape.go +++ b/scrape/scrape.go @@ -336,7 +336,7 @@ func (sp *scrapePool) mutateSampleLabels(lset labels.Labels, target *Target) lab if sp.config.HonorLabels { for _, l := range target.Labels() { - if lv := lset.Get(l.Name); lv == "" { + if !lset.Has(l.Name) { lb.Set(l.Name, l.Value) } } @@ -350,6 +350,12 @@ func (sp *scrapePool) mutateSampleLabels(lset labels.Labels, target *Target) lab } } + for _, l := range lb.Labels() { + if l.Value == "" { + lb.Del(l.Name) + } + } + res := lb.Labels() if mrc := sp.config.MetricRelabelConfigs; len(mrc) > 0 {