From c55a2524332ea1b6742dc977b732c8d37d2ba458 Mon Sep 17 00:00:00 2001 From: Zach Leinweber Date: Thu, 10 Feb 2022 14:39:46 -0600 Subject: [PATCH] Handle digit separators in values Signed-off-by: Zach Leinweber --- exporter/util.go | 5 ++++- exporter/util_test.go | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/exporter/util.go b/exporter/util.go index aa3e8989..fdf7f74d 100644 --- a/exporter/util.go +++ b/exporter/util.go @@ -22,6 +22,7 @@ import ( "math" "net/http" "net/url" + "regexp" "strconv" "strings" "text/template" @@ -34,6 +35,8 @@ import ( pconfig "github.com/prometheus/common/config" ) +var sanitzieValueRE = regexp.MustCompile(`(,|_)`) + func MakeMetricName(parts ...string) string { return strings.Join(parts, "_") } @@ -43,7 +46,7 @@ func SanitizeValue(s string) (float64, error) { var value float64 var resultErr string - if value, err = strconv.ParseFloat(s, 64); err == nil { + if value, err = strconv.ParseFloat(sanitzieValueRE.ReplaceAllString(s, ""), 64); err == nil { return value, nil } resultErr = fmt.Sprintf("%s", err) diff --git a/exporter/util_test.go b/exporter/util_test.go index 90392849..a09e5919 100644 --- a/exporter/util_test.go +++ b/exporter/util_test.go @@ -26,6 +26,9 @@ func TestSanitizeValue(t *testing.T) { }{ {"1234", 1234.0, true}, {"1234.5", 1234.5, true}, + {"123,456.5", 123456.5, true}, + {"123,456,789.5", 123456789.5, true}, + {"123_456_789.5", 123456789.5, true}, {"true", 1.0, true}, {"TRUE", 1.0, true}, {"False", 0.0, true},