From 6106d4069dd0b23ee3000cbf58e934ce3ab369d5 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 22 Oct 2021 13:51:52 +0300 Subject: [PATCH] lib/promscrape: do not sort original labels and do not intern label string for the original labels before the sharding code is executed This should reduce CPU and memory usage in shard mode when service discovery finds big number of scrape targets with many long labels. See https://docs.victoriametrics.com/vmagent.html#scraping-big-number-of-targets This is a follow-up after 9882cda8b9b38e697ad3d94ec448ccb08480577d Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1728 --- Makefile | 2 +- lib/promscrape/config.go | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index d6a1f85bca..9dfff4396d 100644 --- a/Makefile +++ b/Makefile @@ -261,7 +261,7 @@ golangci-lint: install-golangci-lint golangci-lint run --exclude '(SA4003|SA1019|SA5011):' -D errcheck -D structcheck --timeout 2m install-golangci-lint: - which golangci-lint || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.40.1 + which golangci-lint || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.42.1 install-wwhrd: which wwhrd || GO111MODULE=off go get github.com/frapposelli/wwhrd diff --git a/lib/promscrape/config.go b/lib/promscrape/config.go index ec1047f913..39da2536b0 100644 --- a/lib/promscrape/config.go +++ b/lib/promscrape/config.go @@ -974,9 +974,6 @@ func (swc *scrapeWorkConfig) getScrapeWork(target string, extraLabels, metaLabel var originalLabels []prompbmarshal.Label if !*dropOriginalLabels { originalLabels = append([]prompbmarshal.Label{}, labels...) - promrelabel.SortLabels(originalLabels) - // Reduce memory usage by interning all the strings in originalLabels. - internLabelStrings(originalLabels) } labels = swc.relabelConfigs.Apply(labels, 0, false) labels = promrelabel.RemoveMetaLabels(labels[:0], labels) @@ -998,6 +995,11 @@ func (swc *scrapeWorkConfig) getScrapeWork(target string, extraLabels, metaLabel return nil, nil } } + if !*dropOriginalLabels { + promrelabel.SortLabels(originalLabels) + // Reduce memory usage by interning all the strings in originalLabels. + internLabelStrings(originalLabels) + } if len(labels) == 0 { // Drop target without labels. droppedTargetsMap.Register(originalLabels)