Skip to content

Commit

Permalink
Add support for hex values
Browse files Browse the repository at this point in the history
  • Loading branch information
bLd75 committed Oct 30, 2023
1 parent 661af08 commit 943d2a4
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions exporter/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"bytes"
"encoding/json"
"time"
"math/big"
"strings"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}

0 comments on commit 943d2a4

Please sign in to comment.