From b42cf33c4de258529819888a4dd9054595007b0f Mon Sep 17 00:00:00 2001 From: Ween Date: Tue, 30 Jun 2020 03:29:29 +0800 Subject: [PATCH] Fix Auto metrics relabeled errors (#593) * Fix Auto metrics relabeled errors * Finalize auto-genenated Labels * Fix Test Errors Co-authored-by: xinyulong --- lib/promscrape/scrapework.go | 12 ++++++++---- lib/promscrape/scrapework_test.go | 9 +++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/promscrape/scrapework.go b/lib/promscrape/scrapework.go index 85bc1cc76..526270d0f 100644 --- a/lib/promscrape/scrapework.go +++ b/lib/promscrape/scrapework.go @@ -207,7 +207,7 @@ func (sw *scrapeWork) scrapeInternal(timestamp int64) error { samplesScraped := len(srcRows) scrapedSamples.Update(float64(samplesScraped)) for i := range srcRows { - sw.addRowToTimeseries(&srcRows[i], timestamp) + sw.addRowToTimeseries(&srcRows[i], timestamp, true) } sw.rows.Reset() if sw.Config.SampleLimit > 0 && len(sw.writeRequest.Timeseries) > sw.Config.SampleLimit { @@ -238,13 +238,17 @@ func (sw *scrapeWork) addAutoTimeseries(name string, value float64, timestamp in sw.tmpRow.Tags = nil sw.tmpRow.Value = value sw.tmpRow.Timestamp = timestamp - sw.addRowToTimeseries(&sw.tmpRow, timestamp) + sw.addRowToTimeseries(&sw.tmpRow, timestamp, false) } -func (sw *scrapeWork) addRowToTimeseries(r *parser.Row, timestamp int64) { +func (sw *scrapeWork) addRowToTimeseries(r *parser.Row, timestamp int64, needRelabel bool) { labelsLen := len(sw.labels) sw.labels = appendLabels(sw.labels, r.Metric, r.Tags, sw.Config.Labels, sw.Config.HonorLabels) - sw.labels = promrelabel.ApplyRelabelConfigs(sw.labels, labelsLen, sw.Config.MetricRelabelConfigs, true) + if needRelabel { + sw.labels = promrelabel.ApplyRelabelConfigs(sw.labels, labelsLen, sw.Config.MetricRelabelConfigs, true) + } else { + sw.labels = promrelabel.FinalizeLabels(sw.labels[:labelsLen], sw.labels[labelsLen:]) + } if len(sw.labels) == labelsLen { // Skip row without labels. return diff --git a/lib/promscrape/scrapework_test.go b/lib/promscrape/scrapework_test.go index d3b4294f8..bdc273303 100644 --- a/lib/promscrape/scrapework_test.go +++ b/lib/promscrape/scrapework_test.go @@ -245,10 +245,10 @@ func TestScrapeWorkScrapeInternalSuccess(t *testing.T) { }, ` foo{bar="baz",job="xx",instance="foo.com/xx"} 34.44 123 bar{a="b",job="xx",instance="foo.com/xx"} -3e4 123 - up{job="xx",instance="foo.com/xx"} 1 123 - scrape_samples_scraped{job="xx",instance="foo.com/xx"} 2 123 - scrape_duration_seconds{job="xx",instance="foo.com/xx"} 0 123 - scrape_samples_post_metric_relabeling{job="xx",instance="foo.com/xx"} 2 123 + up{job="xx"} 1 123 + scrape_samples_scraped{job="xx"} 2 123 + scrape_duration_seconds{job="xx"} 0 123 + scrape_samples_post_metric_relabeling{job="xx"} 2 123 `) f(` foo{bar="baz"} 34.44 @@ -281,6 +281,7 @@ func TestScrapeWorkScrapeInternalSuccess(t *testing.T) { }, }, ` foo{bar="baz",job="xx",instance="foo.com"} 34.44 123 + up{job="xx",instance="foo.com"} 1 123 scrape_samples_scraped{job="xx",instance="foo.com"} 4 123 scrape_duration_seconds{job="xx",instance="foo.com"} 0 123 scrape_samples_post_metric_relabeling{job="xx",instance="foo.com"} 1 123