From 969a8fb5eeb54c4fc38a587089eaa650b862441d Mon Sep 17 00:00:00 2001 From: shubhindia Date: Tue, 11 Mar 2025 22:41:11 +0530 Subject: [PATCH 1/4] prefix with `_` if name begins with a number Signed-off-by: shubhindia --- pkg/promutil/prometheus.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/promutil/prometheus.go b/pkg/promutil/prometheus.go index 3f62a1588..ff3885748 100644 --- a/pkg/promutil/prometheus.go +++ b/pkg/promutil/prometheus.go @@ -194,6 +194,12 @@ 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 len(text) > 0 && text[0] >= '0' && text[0] <= '9' { + text = "_" + text + } + if strings.ContainsAny(text, "“%") { // fallback to the replacer for complex cases: // - '“' is non-ascii rune From ab349bdf51c50e2aadb6f8da61f0c1c1943f69b4 Mon Sep 17 00:00:00 2001 From: shubhindia Date: Wed, 12 Mar 2025 09:14:47 +0530 Subject: [PATCH 2/4] Use regex to filter metrics starting with digits Signed-off-by: shubhindia --- pkg/promutil/prometheus.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/promutil/prometheus.go b/pkg/promutil/prometheus.go index ff3885748..0d2b86fa5 100644 --- a/pkg/promutil/prometheus.go +++ b/pkg/promutil/prometheus.go @@ -13,6 +13,7 @@ package promutil import ( + "regexp" "strings" "time" @@ -196,7 +197,7 @@ func PromStringTag(text string, labelsSnakeCase bool) (bool, string) { func sanitize(text string) string { // metrics starting with a digit violate the prometheus metric naming convention, so we add an underscore - if len(text) > 0 && text[0] >= '0' && text[0] <= '9' { + if matched, _ := regexp.MatchString(`^\d`, text); matched { text = "_" + text } From 4c7b5f9f4be69f1563e2fc0326b57eed66d0d9b3 Mon Sep 17 00:00:00 2001 From: shubhindia Date: Wed, 12 Mar 2025 13:10:42 +0530 Subject: [PATCH 3/4] Make metricNumberPrefixRE a var Signed-off-by: shubhindia --- pkg/promutil/prometheus.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/promutil/prometheus.go b/pkg/promutil/prometheus.go index 0d2b86fa5..0d2f65ecc 100644 --- a/pkg/promutil/prometheus.go +++ b/pkg/promutil/prometheus.go @@ -106,6 +106,8 @@ var replacer = strings.NewReplacer( "%", "_percent", ) +var metricNumberPrefixRE = regexp.MustCompile(`^\d`) + type PrometheusMetric struct { Name string Labels map[string]string @@ -197,7 +199,7 @@ func PromStringTag(text string, labelsSnakeCase bool) (bool, string) { func sanitize(text string) string { // metrics starting with a digit violate the prometheus metric naming convention, so we add an underscore - if matched, _ := regexp.MatchString(`^\d`, text); matched { + if metricNumberPrefixRE.MatchString(text) { text = "_" + text } From 7595933f97c8aa878cd16f1938e8dfce5d3e054c Mon Sep 17 00:00:00 2001 From: shubhindia Date: Wed, 12 Mar 2025 16:54:35 +0530 Subject: [PATCH 4/4] Add a test for '_' prefix Signed-off-by: shubhindia --- pkg/promutil/prometheus.go | 1 - pkg/promutil/prometheus_test.go | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/promutil/prometheus.go b/pkg/promutil/prometheus.go index 0d2f65ecc..66acfde6f 100644 --- a/pkg/promutil/prometheus.go +++ b/pkg/promutil/prometheus.go @@ -197,7 +197,6 @@ 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 diff --git a/pkg/promutil/prometheus_test.go b/pkg/promutil/prometheus_test.go index 9e461b4a2..006b65e03 100644 --- a/pkg/promutil/prometheus_test.go +++ b/pkg/promutil/prometheus_test.go @@ -63,6 +63,10 @@ func TestSanitize(t *testing.T) { input: "IHaveA%Sign", output: "IHaveA_percentSign", }, + { + input: "5XXError", + output: "_5XXError", + }, } for _, tc := range testCases {