tsweb/varz: flesh out munging of expvar keys into valid Prometheus metrics
From a problem we hit with how badger registers expvars; it broke trunkd's exported metrics. Updates tailscale/corp#1297 Change-Id: I42e1552e25f734c6f521b6e993d57a82849464b2 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
committed by
Brad Fitzpatrick
parent
b104688e04
commit
4992aca6ec
+24
-1
@@ -17,6 +17,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"tailscale.com/metrics"
|
||||
"tailscale.com/version"
|
||||
@@ -85,8 +87,29 @@ func prometheusMetric(prefix string, key string) (string, string, string) {
|
||||
label, key = a, b
|
||||
}
|
||||
}
|
||||
|
||||
// Convert the metric to a valid Prometheus metric name.
|
||||
// "Metric names may contain ASCII letters, digits, underscores, and colons.
|
||||
// It must match the regex [a-zA-Z_:][a-zA-Z0-9_:]*"
|
||||
mapInvalidMetricRunes := func(r rune) rune {
|
||||
if r >= 'a' && r <= 'z' ||
|
||||
r >= 'A' && r <= 'Z' ||
|
||||
r >= '0' && r <= '9' ||
|
||||
r == '_' || r == ':' {
|
||||
return r
|
||||
}
|
||||
if r < utf8.RuneSelf && unicode.IsPrint(r) {
|
||||
return '_'
|
||||
}
|
||||
return -1
|
||||
}
|
||||
metricName := strings.Map(mapInvalidMetricRunes, prefix+key)
|
||||
if metricName == "" || unicode.IsDigit(rune(metricName[0])) {
|
||||
metricName = "_" + metricName
|
||||
}
|
||||
|
||||
d := &prometheusMetricDetails{
|
||||
Name: strings.ReplaceAll(prefix+key, "-", "_"),
|
||||
Name: metricName,
|
||||
Type: typ,
|
||||
Label: label,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user