From 5cb7987314c9e06e1d6931422c3fef3fb8bdc61e Mon Sep 17 00:00:00 2001 From: Geoffrey Beausire Date: Wed, 20 Nov 2019 16:50:05 +0100 Subject: [PATCH] Fix relabaling collision when using exported label When using both a label and the suffix+label in the relabel config. It's possible that Prometheus remove the suffx+label for no obvious reason. It's due to a collision when merging labels from target and from the sample. Signed-off-by: Geoffrey Beausire --- scrape/scrape.go | 6 +++--- scrape/scrape_test.go | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/scrape/scrape.go b/scrape/scrape.go index 7905f2644..3c6ecf356 100644 --- a/scrape/scrape.go +++ b/scrape/scrape.go @@ -451,9 +451,9 @@ func mutateSampleLabels(lset labels.Labels, target *Target, honor bool, rc []*re for _, l := range target.Labels() { // existingValue will be empty if l.Name doesn't exist. existingValue := lset.Get(l.Name) - // Because setting a label with an empty value is a no-op, - // this will only create the prefixed label if necessary. - lb.Set(model.ExportedLabelPrefix+l.Name, existingValue) + if existingValue != "" { + lb.Set(model.ExportedLabelPrefix+l.Name, existingValue) + } // It is now safe to set the target label. lb.Set(l.Name, l.Value) } diff --git a/scrape/scrape_test.go b/scrape/scrape_test.go index 02ddca84d..406dd8c4c 100644 --- a/scrape/scrape_test.go +++ b/scrape/scrape_test.go @@ -935,6 +935,15 @@ func TestScrapeLoopAppend(t *testing.T) { discoveryLabels: []string{"n", "2"}, expLset: labels.FromStrings("__name__", "metric", "exported_n", "1", "n", "2"), expValue: 0, + }, { + // When "honor_labels" is not set + // exported label from discovery don't get overwritten + title: "Label name collision", + honorLabels: false, + scrapeLabels: `metric 0`, + discoveryLabels: []string{"n", "2", "exported_n", "2"}, + expLset: labels.FromStrings("__name__", "metric", "n", "2", "exported_n", "2"), + expValue: 0, }, { // Labels with no value need to be removed as these should not be ingested. title: "Delete Empty labels",