From 30b8d91727acee4de2eb32830a246b10fbc3c511 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sat, 27 Aug 2022 00:28:36 +0300 Subject: [PATCH] lib/promscrape/discoveryutils: always store just allocated string to sanitized label names cache This is a follow-up for c06e7a142c8240202a5f4bec350f9022d9494090 --- lib/promscrape/discoveryutils/utils.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/promscrape/discoveryutils/utils.go b/lib/promscrape/discoveryutils/utils.go index 274933cf72..9ee10d0cc3 100644 --- a/lib/promscrape/discoveryutils/utils.go +++ b/lib/promscrape/discoveryutils/utils.go @@ -29,6 +29,11 @@ func SanitizeLabelName(name string) string { // Make a copy of name in order to limit memory usage to the name length, // since the name may point to bigger string. s := string(append([]byte{}, name...)) + if sanitizedName == name { + // point sanitizedName to just allocated s, since it may point to name, + // which, in turn, can point to bigger string. + sanitizedName = s + } sp := &sanitizedName m.Store(s, sp) n := atomic.AddUint64(&sanitizedLabelNamesLen, 1)