Merge pull request #197 from bakins/escapeMetricName-performance

Increase escapeMetricName performance
This commit is contained in:
Matthias Rampke 2019-04-10 08:47:49 +00:00 committed by GitHub
commit ece9385f22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 6 deletions

View file

@ -21,7 +21,6 @@ import (
"hash/fnv"
"io"
"net"
"regexp"
"sort"
"strconv"
"strings"
@ -42,8 +41,6 @@ const (
)
var (
illegalCharsRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
hash = fnv.New64a()
strBuf bytes.Buffer // Used for hashing.
intBuf = make([]byte, 8)
@ -311,15 +308,35 @@ type Exporter struct {
labelValues map[string]map[uint64]*LabelValues
}
// Replace invalid characters in the metric name with "_"
// Valid characters are a-z, A-Z, 0-9, and _
func escapeMetricName(metricName string) string {
// If a metric starts with a digit, prepend an underscore.
if len(metricName) > 0 && metricName[0] >= '0' && metricName[0] <= '9' {
metricName = "_" + metricName
}
// Replace all illegal metric chars with underscores.
metricName = illegalCharsRE.ReplaceAllString(metricName, "_")
return metricName
// this is an character replacement method optimized for this limited
// use case. It is much faster than using a regex.
out := make([]byte, len(metricName))
j := 0
for _, c := range metricName {
// check if the rune is valid for a metric name
// and replace it if it is not.
// As only certain ASCII characters are valid in metric names,
// we can use a byte.
if (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') {
out[j] = byte(c)
} else {
out[j] = byte('_')
}
j++
}
return string(out[:j])
}
// Listen handles all events sent to the given channel sequentially. It

View file

@ -506,3 +506,24 @@ func getTelemetryCounterValue(counter prometheus.Counter) float64 {
}
return metric.Counter.GetValue()
}
func BenchmarkEscapeMetricName(b *testing.B) {
scenarios := []string{
"clean",
"0starts_with_digit",
"with_underscore",
"with.dot",
"with😱emoji",
"with.*.multiple",
"test.web-server.foo.bar",
"",
}
for _, s := range scenarios {
b.Run(s, func(b *testing.B) {
for n := 0; n < b.N; n++ {
escapeMetricName(s)
}
})
}
}