diff --git a/exporter/collector.go b/exporter/collector.go index 4effc10f..316c7c6d 100644 --- a/exporter/collector.go +++ b/exporter/collector.go @@ -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 { diff --git a/exporter/util.go b/exporter/util.go index eb492e27..2bb42f4e 100644 --- a/exporter/util.go +++ b/exporter/util.go @@ -40,7 +40,6 @@ 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 { @@ -48,12 +47,6 @@ func SanitizeValue(s string) (float64, error) { } 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 @@ -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