From 6bc10f06236b4b33da8db5d17fdd5fd79bf72301 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 c3c18dd382..84ef7a1e76 100644 --- a/Makefile +++ b/Makefile @@ -154,7 +154,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)