Skip to content

Commit

Permalink
Handle digit separators in values
Browse files Browse the repository at this point in the history
  • Loading branch information
zleinweber committed Feb 10, 2022
1 parent 621139e commit e392410
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 4 additions & 1 deletion exporter/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"math"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"text/template"
Expand All @@ -34,6 +35,8 @@ import (
pconfig "github.com/prometheus/common/config"
)

var sanitzieValueRE = regexp.MustCompile(`(,|_)`)

func MakeMetricName(parts ...string) string {
return strings.Join(parts, "_")
}
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions exporter/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit e392410

Please sign in to comment.