Skip to content

Commit

Permalink
Replace regex in escapeMetricName with loop over runes
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Akins <[email protected]>
  • Loading branch information
bakins committed Apr 2, 2019
1 parent 71df5a3 commit 95fe616
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"hash/fnv"
"io"
"net"
"regexp"
"sort"
"strconv"
"strings"
Expand All @@ -44,8 +43,6 @@ const (
)

var (
illegalCharsRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)

hash = fnv.New64a()
strBuf bytes.Buffer // Used for hashing.
intBuf = make([]byte, 8)
Expand Down Expand Up @@ -284,9 +281,21 @@ func escapeMetricName(metricName string) string {
metricName = "_" + metricName
}

// Replace all illegal metric chars with underscores.
metricName = illegalCharsRE.ReplaceAllString(metricName, "_")
return metricName
out := make([]byte, len(metricName))
j := 0
for _, c := range metricName {
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
Expand Down

0 comments on commit 95fe616

Please sign in to comment.