From b08c6f5144bf654cd46c39776edbd02122d9c8cc Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 4 Nov 2020 11:08:30 +0200 Subject: [PATCH] lib/promscrape: add `-promscrape.dropOriginalLabels` command-line flag for reducing memory usage when discovering big number of scrape targets Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/878 --- lib/promscrape/config.go | 9 +++++++-- lib/promscrape/scraper.go | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/promscrape/config.go b/lib/promscrape/config.go index d8ca914699..dc7128608b 100644 --- a/lib/promscrape/config.go +++ b/lib/promscrape/config.go @@ -32,6 +32,8 @@ var ( dryRun = flag.Bool("promscrape.config.dryRun", false, "Checks -promscrape.config file for errors and unsupported fields and then exits. "+ "Returns non-zero exit code on parsing errors and emits these errors to stderr. "+ "Pass -loggerLevel=ERROR if you don't need to see info messages in the output") + dropOriginalLabels = flag.Bool("promscrape.dropOriginalLabels", false, "Whether to drop original labels for scrape targets at /targets and /api/v1/targets pages. "+ + "This may be needed for reducing memory usage if original labels occupy big amounts of memory") ) // Config represents essential parts from Prometheus config defined at https://prometheus.io/docs/prometheus/latest/configuration/configuration/ @@ -639,8 +641,11 @@ func (stc *StaticConfig) appendScrapeWork(dst []ScrapeWork, swc *scrapeWorkConfi func appendScrapeWork(dst []ScrapeWork, swc *scrapeWorkConfig, target string, extraLabels, metaLabels map[string]string) ([]ScrapeWork, error) { labels := mergeLabels(swc.jobName, swc.scheme, target, swc.metricsPath, extraLabels, swc.externalLabels, metaLabels, swc.params) - originalLabels := append([]prompbmarshal.Label{}, labels...) - promrelabel.SortLabels(originalLabels) + var originalLabels []prompbmarshal.Label + if !*dropOriginalLabels { + originalLabels = append([]prompbmarshal.Label{}, labels...) + promrelabel.SortLabels(originalLabels) + } labels = promrelabel.ApplyRelabelConfigs(labels, 0, swc.relabelConfigs, false) labels = promrelabel.RemoveMetaLabels(labels[:0], labels) if len(labels) == 0 { diff --git a/lib/promscrape/scraper.go b/lib/promscrape/scraper.go index 341e66462a..a6683b5fe0 100644 --- a/lib/promscrape/scraper.go +++ b/lib/promscrape/scraper.go @@ -308,7 +308,7 @@ func (sg *scraperGroup) update(sws []ScrapeWork) { // Stop deleted scrapers, which are missing in sws. for key, sc := range sg.m { - if swsMap[key] == nil { + if _, ok := swsMap[key]; !ok { close(sc.stopCh) delete(sg.m, key) deletionsCount++