diff --git a/exporter/util.go b/exporter/util.go index 2bb42f4e..32571a56 100644 --- a/exporter/util.go +++ b/exporter/util.go @@ -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) diff --git a/exporter/util_test.go b/exporter/util_test.go index 90392849..3688d193 100644 --- a/exporter/util_test.go +++ b/exporter/util_test.go @@ -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("") if err != nil {