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
This commit is contained in:
Aliaksandr Valialkin 2020-11-04 11:08:30 +02:00
parent ca8b5745b5
commit 5a9743211f
2 changed files with 8 additions and 3 deletions

View File

@ -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 {

View File

@ -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++