Skip to content

Commit

Permalink
Fix parseInt, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Giacomo Licari committed Nov 2, 2023
1 parent 077d972 commit 5e0b809
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
44 changes: 27 additions & 17 deletions exporter/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,21 @@ func (mc JSONMetricCollector) Collect(ch chan<- prometheus.Metric) {
floatValue,
extractLabels(mc.Logger, mc.Data, m.LabelsJSONPaths)...,
)
} else {
intValue, err := SanitizeHexIntValue(value)
if err == nil {
ch <- prometheus.MustNewConstMetric(
m.Desc,
m.ValueType,
float64(intValue),
extractLabels(mc.Logger, mc.Data, m.LabelsJSONPaths)...,
)
} else {
level.Error(mc.Logger).Log("msg", "Failed to convert extracted value to float64", "path", m.KeyJSONPath, "value", value, "err", err, "metric", m.Desc)
continue
}
}

intValue, err := SanitizeHexIntValue(value)
if err == nil {
ch <- prometheus.MustNewConstMetric(
m.Desc,
prometheus.UntypedValue,
float64(intValue),
extractLabels(mc.Logger, mc.Data, m.LabelsJSONPaths)...,
)
}


level.Error(mc.Logger).Log("msg", "Failed to convert extracted value to float64", "path", m.KeyJSONPath, "value", value, "err", err, "metric", m.Desc)
continue

case config.ObjectScrape:
values, err := extractValue(mc.Logger, mc.Data, m.KeyJSONPath, true)
if err != nil {
Expand All @@ -102,7 +101,8 @@ func (mc JSONMetricCollector) Collect(ch chan<- prometheus.Metric) {
continue
}

if floatValue, err := SanitizeValue(value); err == nil {
floatValue, err := SanitizeValue(value)
if err == nil {
metric := prometheus.MustNewConstMetric(
m.Desc,
m.ValueType,
Expand All @@ -111,8 +111,18 @@ func (mc JSONMetricCollector) Collect(ch chan<- prometheus.Metric) {
)
ch <- timestampMetric(mc.Logger, m, jdata, metric)
} else {
level.Error(mc.Logger).Log("msg", "Failed to convert extracted value to float64", "path", m.ValueJSONPath, "value", value, "err", err, "metric", m.Desc)
continue
intValue, err := SanitizeHexIntValue(value)
if err == nil {
ch <- prometheus.MustNewConstMetric(
m.Desc,
m.ValueType,
float64(intValue),
extractLabels(mc.Logger, mc.Data, m.LabelsJSONPaths)...,
)
} else {
level.Error(mc.Logger).Log("msg", "Failed to convert extracted value to float64", "path", m.ValueJSONPath, "value", value, "err", err, "metric", m.Desc)
continue
}
}
}
} else {
Expand Down
7 changes: 5 additions & 2 deletions exporter/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ func SanitizeHexIntValue(s string) (int64, error) {
var value int64
var resultErr string

// Check if value is an hex integer, e.g. 0x1d54c54 => 30755924
if value, err = strconv.ParseInt(s, 16, 64); err == nil {
// remove 0x suffix if found in the input string
cleaned := strings.Replace(s, "0x", "", -1)
cleaned = strings.Replace(cleaned, "\"", "", -1)

if value, err = strconv.ParseInt(cleaned, 16, 64); err == nil {
return value, nil
}
resultErr = fmt.Sprintf("%s", err)
Expand Down
21 changes: 21 additions & 0 deletions exporter/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ func TestSanitizeValue(t *testing.T) {
}
}

func TestSanitizeValueHex(t *testing.T) {
tests := []struct {
Input string
ExpectedOutput int64
ShouldSucceed bool
}{
{"0x1d55195", 30757269, true},
{"\"0x1d55195\"", 30757269, true},
}

for i, test := range tests {
actualOutput, err := SanitizeHexIntValue(test.Input)
if err != nil && test.ShouldSucceed {
t.Fatalf("Value snitization test %d failed with an unexpected error.\nINPUT:\n%q\nERR:\n%s", i, test.Input, err)
}
if test.ShouldSucceed && actualOutput != test.ExpectedOutput {
t.Fatalf("Value sanitization test %d fails unexpectedly.\nGOT:\n%d\nEXPECTED:\n%d", i, actualOutput, test.ExpectedOutput)
}
}
}

func TestSanitizeValueNaN(t *testing.T) {
actualOutput, err := SanitizeValue("<nil>")
if err != nil {
Expand Down

0 comments on commit 5e0b809

Please sign in to comment.