Skip to content

Cache prometheusize rename between scrapes #1080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion exporter/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import (
"regexp"
"strings"
"sync"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -164,10 +165,17 @@
dollarRe = regexp.MustCompile(`\_$`)
)

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.Load(s); exists {
return renamed.(string)

Check failure on line 175 in exporter/metrics.go

View workflow job for this annotation

GitHub Actions / Lint Check

type assertion must be checked (forcetypeassert)
}
backup := strings.Clone(s)

for _, pair := range prefixes {
if strings.HasPrefix(s, pair[0]+".") {
s = pair[1] + strings.TrimPrefix(s, pair[0])
Expand All @@ -179,8 +187,11 @@
s = dollarRe.ReplaceAllString(s, "")
s = repeatedUnderscoresRe.ReplaceAllString(s, "_")
s = strings.TrimPrefix(s, "_")
s = exporterPrefix + s

prometheusizeCache.Store(backup, strings.Clone(s))

return exporterPrefix + s
return s
}

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