Skip to content

Commit 174e7fb

Browse files
vigno88vigno99idoqo
authored
Cache prometheusize rename between scrapes (#1080)
* cache prometheusize * Add prefix * thread safe --------- Co-authored-by: Alix-Vignola, Nathan <nalixvignola@rbbn.com> Co-authored-by: Michael Okoko <10512379+idoqo@users.noreply.github.com>
1 parent 21abf08 commit 174e7fb

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

exporter/metrics.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package exporter
1818
import (
1919
"regexp"
2020
"strings"
21+
"sync"
2122
"time"
2223

2324
"github.com/pkg/errors"
@@ -164,10 +165,17 @@ var (
164165
dollarRe = regexp.MustCompile(`\_$`)
165166
)
166167

168+
var prometheusizeCache = sync.Map{}
169+
167170
// prometheusize renames metrics by replacing some prefixes with shorter names
168171
// replace special chars to follow Prometheus metric naming rules and adds the
169172
// exporter name prefix.
170173
func prometheusize(s string) string {
174+
if renamed, exists := prometheusizeCache.Load(s); exists {
175+
return renamed.(string)
176+
}
177+
backup := strings.Clone(s)
178+
171179
for _, pair := range prefixes {
172180
if strings.HasPrefix(s, pair[0]+".") {
173181
s = pair[1] + strings.TrimPrefix(s, pair[0])
@@ -179,8 +187,11 @@ func prometheusize(s string) string {
179187
s = dollarRe.ReplaceAllString(s, "")
180188
s = repeatedUnderscoresRe.ReplaceAllString(s, "_")
181189
s = strings.TrimPrefix(s, "_")
190+
s = exporterPrefix + s
191+
192+
prometheusizeCache.Store(backup, strings.Clone(s))
182193

183-
return exporterPrefix + s
194+
return s
184195
}
185196

186197
// nameAndLabel checks if there are predefined metric name and label for that metric or

0 commit comments

Comments
 (0)