From dcc893d4374861bbd7b36ad01c97a7dabc2b053d Mon Sep 17 00:00:00 2001 From: "Alix-Vignola, Nathan" Date: Fri, 9 May 2025 14:09:27 -0400 Subject: [PATCH 1/3] cache prometheusize --- exporter/metrics.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/exporter/metrics.go b/exporter/metrics.go index cd3d27246..5142ed65c 100644 --- a/exporter/metrics.go +++ b/exporter/metrics.go @@ -164,10 +164,17 @@ var ( dollarRe = regexp.MustCompile(`\_$`) ) +var prometheusizeCache = make(map[string]string) + // prometheusize renames metrics by replacing some prefixes with shorter names // replace special chars to follow Prometheus metric naming rules and adds the // exporter name prefix. func prometheusize(s string) string { + if renamed, exists := prometheusizeCache[s]; exists { + return renamed + } + back := strings.Clone(s) + for _, pair := range prefixes { if strings.HasPrefix(s, pair[0]+".") { s = pair[1] + strings.TrimPrefix(s, pair[0]) @@ -180,6 +187,8 @@ func prometheusize(s string) string { s = repeatedUnderscoresRe.ReplaceAllString(s, "_") s = strings.TrimPrefix(s, "_") + prometheusizeCache[back] = strings.Clone(s) + return exporterPrefix + s } From f40a12ce9a3fb3f6140b0fc05ca7ecc5d802bced Mon Sep 17 00:00:00 2001 From: "Alix-Vignola, Nathan" Date: Fri, 9 May 2025 16:55:32 -0400 Subject: [PATCH 2/3] Add prefix --- exporter/metrics.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/exporter/metrics.go b/exporter/metrics.go index 5142ed65c..f6747f2e9 100644 --- a/exporter/metrics.go +++ b/exporter/metrics.go @@ -173,7 +173,7 @@ func prometheusize(s string) string { if renamed, exists := prometheusizeCache[s]; exists { return renamed } - back := strings.Clone(s) + backup := strings.Clone(s) for _, pair := range prefixes { if strings.HasPrefix(s, pair[0]+".") { @@ -186,10 +186,10 @@ func prometheusize(s string) string { s = dollarRe.ReplaceAllString(s, "") s = repeatedUnderscoresRe.ReplaceAllString(s, "_") s = strings.TrimPrefix(s, "_") + s = exporterPrefix + s - prometheusizeCache[back] = strings.Clone(s) - - return exporterPrefix + s + prometheusizeCache[backup] = strings.Clone(s) + return s } // nameAndLabel checks if there are predefined metric name and label for that metric or From 6ec34f299e128d07e85f0db80bec191ab9b92c92 Mon Sep 17 00:00:00 2001 From: "Alix-Vignola, Nathan" Date: Mon, 12 May 2025 08:16:22 -0400 Subject: [PATCH 3/3] thread safe --- exporter/metrics.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/exporter/metrics.go b/exporter/metrics.go index f6747f2e9..6cf9a2f66 100644 --- a/exporter/metrics.go +++ b/exporter/metrics.go @@ -18,6 +18,7 @@ package exporter import ( "regexp" "strings" + "sync" "time" "github.com/pkg/errors" @@ -164,14 +165,14 @@ var ( dollarRe = regexp.MustCompile(`\_$`) ) -var prometheusizeCache = make(map[string]string) +var prometheusizeCache = sync.Map{} // prometheusize renames metrics by replacing some prefixes with shorter names // replace special chars to follow Prometheus metric naming rules and adds the // exporter name prefix. func prometheusize(s string) string { - if renamed, exists := prometheusizeCache[s]; exists { - return renamed + if renamed, exists := prometheusizeCache.Load(s); exists { + return renamed.(string) } backup := strings.Clone(s) @@ -188,7 +189,8 @@ func prometheusize(s string) string { s = strings.TrimPrefix(s, "_") s = exporterPrefix + s - prometheusizeCache[backup] = strings.Clone(s) + prometheusizeCache.Store(backup, strings.Clone(s)) + return s }