Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions pkg/promutil/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package promutil

import (
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -105,6 +106,8 @@ var replacer = strings.NewReplacer(
"%", "_percent",
)

var metricNumberPrefixRE = regexp.MustCompile(`^\d`)

type PrometheusMetric struct {
Name string
Labels map[string]string
Expand Down Expand Up @@ -194,6 +197,11 @@ func PromStringTag(text string, labelsSnakeCase bool) (bool, string) {

// sanitize replaces some invalid chars with an underscore
func sanitize(text string) string {
// metrics starting with a digit violate the prometheus metric naming convention, so we add an underscore
if metricNumberPrefixRE.MatchString(text) {
text = "_" + text
}

if strings.ContainsAny(text, "“%") {
// fallback to the replacer for complex cases:
// - '“' is non-ascii rune
Expand Down
4 changes: 4 additions & 0 deletions pkg/promutil/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func TestSanitize(t *testing.T) {
input: "IHaveA%Sign",
output: "IHaveA_percentSign",
},
{
input: "5XXError",
output: "_5XXError",
},
}

for _, tc := range testCases {
Expand Down
Loading