From f7ac56372fa8ab3ae915e385a9def46c544a499f Mon Sep 17 00:00:00 2001 From: "Jonas L." Date: Tue, 16 Jul 2024 13:12:02 +0200 Subject: [PATCH] refactor: move regexp compilation outside function (#485) This improves the performances a bit. --- hcloud/internal/instrumentation/metrics.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/hcloud/internal/instrumentation/metrics.go b/hcloud/internal/instrumentation/metrics.go index e52bd606..f57e92b7 100644 --- a/hcloud/internal/instrumentation/metrics.go +++ b/hcloud/internal/instrumentation/metrics.go @@ -102,12 +102,13 @@ func registerOrReuse[C prometheus.Collector](registry prometheus.Registerer, col return collector } +var pathLabelRegexp = regexp.MustCompile("[^a-z/_]+") + func preparePathForLabel(path string) string { path = strings.ToLower(path) // replace all numbers and chars that are not a-z, / or _ - reg := regexp.MustCompile("[^a-z/_]+") - path = reg.ReplaceAllString(path, "") + path = pathLabelRegexp.ReplaceAllString(path, "") // replace all artifacts of number replacement (//) path = strings.ReplaceAll(path, "//", "/")