From cbcafe28d99e208f906a222b53bedc44b5812c63 Mon Sep 17 00:00:00 2001 From: bLd75 <18489502+bLd75@users.noreply.github.com> Date: Mon, 30 Oct 2023 16:44:18 +0400 Subject: [PATCH 1/2] Add support for hex values Signed-off-by: bLd75 <18489502+bLd75@users.noreply.github.com> --- exporter/collector.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/exporter/collector.go b/exporter/collector.go index 4effc10f..b9a5ab10 100644 --- a/exporter/collector.go +++ b/exporter/collector.go @@ -17,6 +17,8 @@ import ( "bytes" "encoding/json" "time" + "math/big" + "strings" "github.com/go-kit/log" "github.com/go-kit/log/level" @@ -91,6 +93,11 @@ func (mc JSONMetricCollector) Collect(ch chan<- prometheus.Metric) { continue } + // When value starts by '0x', convert hex to decimal + if strings.HasPrefix(value, "0x") { + value = formatHex(value) + } + if floatValue, err := SanitizeValue(value); err == nil { metric := prometheus.MustNewConstMetric( m.Desc, @@ -178,3 +185,11 @@ func timestampMetric(logger log.Logger, m JSONMetric, data []byte, pm prometheus timestamp := time.UnixMilli(epochTime) return prometheus.NewMetricWithTimestamp(timestamp, pm) } + +func formatHex(s string) string { + st := strings.Replace(s, "0x", "", -1) + i := new(big.Int) + i.SetString(st, 16) + str := i.String() + return str +} From 40453e3d0bbca3c64c5a3889ead88fbde07cf792 Mon Sep 17 00:00:00 2001 From: bLd75 <18489502+bLd75@users.noreply.github.com> Date: Mon, 30 Oct 2023 16:50:32 +0400 Subject: [PATCH 2/2] fix indent Signed-off-by: bLd75 <18489502+bLd75@users.noreply.github.com> --- exporter/collector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/collector.go b/exporter/collector.go index b9a5ab10..14125bb2 100644 --- a/exporter/collector.go +++ b/exporter/collector.go @@ -96,7 +96,7 @@ func (mc JSONMetricCollector) Collect(ch chan<- prometheus.Metric) { // When value starts by '0x', convert hex to decimal if strings.HasPrefix(value, "0x") { value = formatHex(value) - } + } if floatValue, err := SanitizeValue(value); err == nil { metric := prometheus.MustNewConstMetric(