mirror of
https://github.com/prometheus-community/json_exporter
synced 2025-02-17 03:56:50 +00:00
Add support for parsing additional types (string, null) (#5)
* Add support for parsing additional types (string, null) With this patch we now support pulling number out of strings (its fairly common for metrics to use quoted numbers as values) as well as "null" -- which we represent as a NaN. * Bump minor version
This commit is contained in:
parent
d45e5ebdb0
commit
51e3dc02a3
@ -2,11 +2,13 @@ package jsonexporter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/kawamuray/jsonpath" // Originally: "github.com/NickSardo/jsonpath"
|
||||
"github.com/kawamuray/prometheus-exporter-harness/harness"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type JsonScraper interface {
|
||||
@ -64,13 +66,21 @@ func (vs *ValueScraper) Scrape(data []byte, reg *harness.MetricRegistry) error {
|
||||
}
|
||||
isFirst = false
|
||||
|
||||
if result.Type != jsonpath.JsonNumber {
|
||||
var value float64
|
||||
var err error
|
||||
switch result.Type {
|
||||
case jsonpath.JsonNumber:
|
||||
value, err = vs.parseValue(result.Value)
|
||||
case jsonpath.JsonString:
|
||||
// If it is a string, lets pull off the quotes and attempt to parse it as a number
|
||||
value, err = vs.parseValue(result.Value[1 : len(result.Value)-1])
|
||||
case jsonpath.JsonNull:
|
||||
value = math.NaN()
|
||||
default:
|
||||
log.Warnf("skipping not numerical result;path:<%s>,value:<%s>",
|
||||
vs.valueJsonPath, result.Value)
|
||||
return
|
||||
}
|
||||
|
||||
value, err := vs.parseValue(result.Value)
|
||||
if err != nil {
|
||||
// Should never happen.
|
||||
log.Errorf("could not parse numerical value as float;path:<%s>,value:<%s>",
|
||||
@ -178,13 +188,20 @@ func (obsc *ObjectScraper) Scrape(data []byte, reg *harness.MetricRegistry) erro
|
||||
continue
|
||||
}
|
||||
|
||||
if firstResult.Type != jsonpath.JsonNumber {
|
||||
var value float64
|
||||
switch firstResult.Type {
|
||||
case jsonpath.JsonNumber:
|
||||
value, err = obsc.parseValue(firstResult.Value)
|
||||
case jsonpath.JsonString:
|
||||
// If it is a string, lets pull off the quotes and attempt to parse it as a number
|
||||
value, err = obsc.parseValue(firstResult.Value[1 : len(firstResult.Value)-1])
|
||||
case jsonpath.JsonNull:
|
||||
value = math.NaN()
|
||||
default:
|
||||
log.Warnf("skipping not numerical result;path:<%s>,value:<%s>",
|
||||
obsc.valueJsonPath, result.Value)
|
||||
continue
|
||||
}
|
||||
|
||||
value, err := obsc.parseValue(firstResult.Value)
|
||||
if err != nil {
|
||||
// Should never happen.
|
||||
log.Errorf("could not parse numerical value as float;path:<%s>,value:<%s>",
|
||||
|
@ -1,3 +1,3 @@
|
||||
package jsonexporter
|
||||
|
||||
const Version = "0.0.1"
|
||||
const Version = "0.0.2"
|
||||
|
Loading…
Reference in New Issue
Block a user