Skip to content

Commit

Permalink
Add additional step to scrape hex data
Browse files Browse the repository at this point in the history
Signed-off-by: Giacomo Licari <[email protected]>
  • Loading branch information
Giacomo Licari authored and giacomognosis committed Nov 2, 2023
1 parent 2a036ee commit 1b73e0b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
23 changes: 17 additions & 6 deletions exporter/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,30 @@ func (mc JSONMetricCollector) Collect(ch chan<- prometheus.Metric) {
continue
}

if floatValue, err := SanitizeValue(value); err == nil {
metric := prometheus.MustNewConstMetric(
floatValue, err := SanitizeValue(value)
if err == nil {
ch <- prometheus.MustNewConstMetric(
m.Desc,
m.ValueType,
floatValue,
extractLabels(mc.Logger, mc.Data, m.LabelsJSONPaths)...,
)
ch <- timestampMetric(mc.Logger, m, mc.Data, metric)
} 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 Down
21 changes: 14 additions & 7 deletions exporter/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,13 @@ func MakeMetricName(parts ...string) string {
func SanitizeValue(s string) (float64, error) {
var err error
var value float64
var hexValue int64
var resultErr string

if value, err = strconv.ParseFloat(s, 64); err == nil {
return value, nil
}
resultErr = fmt.Sprintf("%s", err)

// Check if value is an hex integer, e.g. 0x1d54c54 => 30755924
if hexValue, err = strconv.ParseInt(s, 16, 64); err == nil {
return float64(hexValue), nil
}
resultErr = resultErr + "; " + fmt.Sprintf("%s", err)

if boolValue, err := strconv.ParseBool(s); err == nil {
if boolValue {
return 1.0, nil
Expand Down Expand Up @@ -81,6 +74,20 @@ func SanitizeIntValue(s string) (int64, error) {
return value, fmt.Errorf(resultErr)
}

func SanitizeHexIntValue(s string) (int64, error) {
var err 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 {
return value, nil
}
resultErr = fmt.Sprintf("%s", err)

return value, fmt.Errorf(resultErr)
}

func CreateMetricsList(c config.Module) ([]JSONMetric, error) {
var (
metrics []JSONMetric
Expand Down

0 comments on commit 1b73e0b

Please sign in to comment.