@@ -18,6 +18,7 @@ package exporter
18
18
import (
19
19
"regexp"
20
20
"strings"
21
+ "sync"
21
22
"time"
22
23
23
24
"github.com/pkg/errors"
@@ -164,10 +165,17 @@ var (
164
165
dollarRe = regexp .MustCompile (`\_$` )
165
166
)
166
167
168
+ var prometheusizeCache = sync.Map {}
169
+
167
170
// prometheusize renames metrics by replacing some prefixes with shorter names
168
171
// replace special chars to follow Prometheus metric naming rules and adds the
169
172
// exporter name prefix.
170
173
func prometheusize (s string ) string {
174
+ if renamed , exists := prometheusizeCache .Load (s ); exists {
175
+ return renamed .(string )
176
+ }
177
+ backup := strings .Clone (s )
178
+
171
179
for _ , pair := range prefixes {
172
180
if strings .HasPrefix (s , pair [0 ]+ "." ) {
173
181
s = pair [1 ] + strings .TrimPrefix (s , pair [0 ])
@@ -179,8 +187,11 @@ func prometheusize(s string) string {
179
187
s = dollarRe .ReplaceAllString (s , "" )
180
188
s = repeatedUnderscoresRe .ReplaceAllString (s , "_" )
181
189
s = strings .TrimPrefix (s , "_" )
190
+ s = exporterPrefix + s
191
+
192
+ prometheusizeCache .Store (backup , strings .Clone (s ))
182
193
183
- return exporterPrefix + s
194
+ return s
184
195
}
185
196
186
197
// nameAndLabel checks if there are predefined metric name and label for that metric or
0 commit comments