Skip to content

Commit 56f3fef

Browse files
Giacomo Licarigiacomognosis
Giacomo Licari
authored andcommitted
Fix parseInt, add tests
Signed-off-by: Giacomo Licari <[email protected]>
1 parent 1b73e0b commit 56f3fef

File tree

3 files changed

+53
-19
lines changed

3 files changed

+53
-19
lines changed

exporter/collector.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,21 @@ func (mc JSONMetricCollector) Collect(ch chan<- prometheus.Metric) {
6565
floatValue,
6666
extractLabels(mc.Logger, mc.Data, m.LabelsJSONPaths)...,
6767
)
68+
} else {
69+
intValue, err := SanitizeHexIntValue(value)
70+
if err == nil {
71+
ch <- prometheus.MustNewConstMetric(
72+
m.Desc,
73+
m.ValueType,
74+
float64(intValue),
75+
extractLabels(mc.Logger, mc.Data, m.LabelsJSONPaths)...,
76+
)
77+
} else {
78+
level.Error(mc.Logger).Log("msg", "Failed to convert extracted value to float64", "path", m.KeyJSONPath, "value", value, "err", err, "metric", m.Desc)
79+
continue
80+
}
6881
}
6982

70-
intValue, err := SanitizeHexIntValue(value)
71-
if err == nil {
72-
ch <- prometheus.MustNewConstMetric(
73-
m.Desc,
74-
prometheus.UntypedValue,
75-
float64(intValue),
76-
extractLabels(mc.Logger, mc.Data, m.LabelsJSONPaths)...,
77-
)
78-
}
79-
80-
81-
level.Error(mc.Logger).Log("msg", "Failed to convert extracted value to float64", "path", m.KeyJSONPath, "value", value, "err", err, "metric", m.Desc)
82-
continue
83-
8483
case config.ObjectScrape:
8584
values, err := extractValue(mc.Logger, mc.Data, m.KeyJSONPath, true)
8685
if err != nil {
@@ -102,7 +101,8 @@ func (mc JSONMetricCollector) Collect(ch chan<- prometheus.Metric) {
102101
continue
103102
}
104103

105-
if floatValue, err := SanitizeValue(value); err == nil {
104+
floatValue, err := SanitizeValue(value)
105+
if err == nil {
106106
metric := prometheus.MustNewConstMetric(
107107
m.Desc,
108108
m.ValueType,
@@ -111,8 +111,18 @@ func (mc JSONMetricCollector) Collect(ch chan<- prometheus.Metric) {
111111
)
112112
ch <- timestampMetric(mc.Logger, m, jdata, metric)
113113
} else {
114-
level.Error(mc.Logger).Log("msg", "Failed to convert extracted value to float64", "path", m.ValueJSONPath, "value", value, "err", err, "metric", m.Desc)
115-
continue
114+
intValue, err := SanitizeHexIntValue(value)
115+
if err == nil {
116+
ch <- prometheus.MustNewConstMetric(
117+
m.Desc,
118+
m.ValueType,
119+
float64(intValue),
120+
extractLabels(mc.Logger, mc.Data, m.LabelsJSONPaths)...,
121+
)
122+
} else {
123+
level.Error(mc.Logger).Log("msg", "Failed to convert extracted value to float64", "path", m.ValueJSONPath, "value", value, "err", err, "metric", m.Desc)
124+
continue
125+
}
116126
}
117127
}
118128
} else {

exporter/util.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,11 @@ func SanitizeHexIntValue(s string) (int64, error) {
7979
var value int64
8080
var resultErr string
8181

82-
// Check if value is an hex integer, e.g. 0x1d54c54 => 30755924
83-
if value, err = strconv.ParseInt(s, 16, 64); err == nil {
82+
// remove 0x suffix if found in the input string
83+
cleaned := strings.Replace(s, "0x", "", -1)
84+
cleaned = strings.Replace(cleaned, "\"", "", -1)
85+
86+
if value, err = strconv.ParseInt(cleaned, 16, 64); err == nil {
8487
return value, nil
8588
}
8689
resultErr = fmt.Sprintf("%s", err)

exporter/util_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ func TestSanitizeValue(t *testing.T) {
4848
}
4949
}
5050

51+
func TestSanitizeValueHex(t *testing.T) {
52+
tests := []struct {
53+
Input string
54+
ExpectedOutput int64
55+
ShouldSucceed bool
56+
}{
57+
{"0x1d55195", 30757269, true},
58+
{"\"0x1d55195\"", 30757269, true},
59+
}
60+
61+
for i, test := range tests {
62+
actualOutput, err := SanitizeHexIntValue(test.Input)
63+
if err != nil && test.ShouldSucceed {
64+
t.Fatalf("Value snitization test %d failed with an unexpected error.\nINPUT:\n%q\nERR:\n%s", i, test.Input, err)
65+
}
66+
if test.ShouldSucceed && actualOutput != test.ExpectedOutput {
67+
t.Fatalf("Value sanitization test %d fails unexpectedly.\nGOT:\n%d\nEXPECTED:\n%d", i, actualOutput, test.ExpectedOutput)
68+
}
69+
}
70+
}
71+
5172
func TestSanitizeValueNaN(t *testing.T) {
5273
actualOutput, err := SanitizeValue("<nil>")
5374
if err != nil {

0 commit comments

Comments
 (0)