Update json_v2 parser to handle null types (#9368)
This commit is contained in:
parent
ee0a86c4ae
commit
daec1040c6
|
|
@ -306,31 +306,35 @@ func (p *Parser) expandArray(result MetricNode) ([]MetricNode, error) {
|
|||
return nil, err
|
||||
}
|
||||
} else {
|
||||
if !result.Tag && !result.IsObject() {
|
||||
if result.SetName == p.currentSettings.TimestampKey {
|
||||
if p.currentSettings.TimestampFormat == "" {
|
||||
err := fmt.Errorf("use of 'timestamp_query' requires 'timestamp_format'")
|
||||
return nil, err
|
||||
if result.SetName == p.currentSettings.TimestampKey {
|
||||
if p.currentSettings.TimestampFormat == "" {
|
||||
err := fmt.Errorf("use of 'timestamp_query' requires 'timestamp_format'")
|
||||
return nil, err
|
||||
}
|
||||
timestamp, err := internal.ParseTimestamp(p.currentSettings.TimestampFormat, result.Value(), p.currentSettings.TimestampTimezone)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result.Metric.SetTime(timestamp)
|
||||
} else {
|
||||
switch result.Value().(type) {
|
||||
case nil: // Ignore JSON values that are set as null
|
||||
default:
|
||||
if result.Tag {
|
||||
result.DesiredType = "string"
|
||||
}
|
||||
timestamp, err := internal.ParseTimestamp(p.currentSettings.TimestampFormat, result.Value(), p.currentSettings.TimestampTimezone)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result.Metric.SetTime(timestamp)
|
||||
} else {
|
||||
v, err := p.convertType(result.Value(), result.DesiredType, result.SetName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result.Metric.AddField(result.OutputName, v)
|
||||
if result.Tag {
|
||||
result.Metric.AddTag(result.OutputName, v.(string))
|
||||
} else {
|
||||
result.Metric.AddField(result.OutputName, v)
|
||||
}
|
||||
}
|
||||
} else if !result.IsObject() {
|
||||
v, err := p.convertType(result.Value(), "string", result.SetName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result.Metric.AddTag(result.OutputName, v.(string))
|
||||
}
|
||||
|
||||
results = append(results, result)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -69,6 +69,10 @@ func TestData(t *testing.T) {
|
|||
name: "Test multiple timestamps",
|
||||
test: "multiple_timestamps",
|
||||
},
|
||||
{
|
||||
name: "Test field with null",
|
||||
test: "null",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
file,id=ak0217l8ue0x,type=Feature detail="https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/ak0217l8ue0x.geojson",mag=1.5,place="63 km N of Petersville, Alaska",status="automatic",time=1623708726566,updated=1623709998223,url="https://earthquake.usgs.gov/earthquakes/eventpage/ak0217l8ue0x"
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
{
|
||||
"type": "FeatureCollection",
|
||||
"metadata": {
|
||||
"generated": 1623710450000,
|
||||
"url": "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson",
|
||||
"title": "USGS All Earthquakes, Past Hour",
|
||||
"status": 200,
|
||||
"api": "1.10.3",
|
||||
"count": 10
|
||||
},
|
||||
"features": [
|
||||
{
|
||||
"type": "Feature",
|
||||
"properties": {
|
||||
"mag": 1.5,
|
||||
"place": "63 km N of Petersville, Alaska",
|
||||
"time": 1623708726566,
|
||||
"updated": 1623709998223,
|
||||
"tz": null,
|
||||
"url": "https://earthquake.usgs.gov/earthquakes/eventpage/ak0217l8ue0x",
|
||||
"detail": "https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/ak0217l8ue0x.geojson",
|
||||
"felt": null,
|
||||
"cdi": null,
|
||||
"mmi": null,
|
||||
"alert": null,
|
||||
"status": "automatic"
|
||||
},
|
||||
"id": "ak0217l8ue0x"
|
||||
}
|
||||
],
|
||||
"bbox": [
|
||||
-157.5749,
|
||||
32.9001667,
|
||||
0.25,
|
||||
-115.6211667,
|
||||
66.331,
|
||||
132.5
|
||||
]
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
[[inputs.file]]
|
||||
files = ["./testdata/null/input.json"]
|
||||
data_format = "json_v2"
|
||||
[[inputs.file.json_v2]]
|
||||
[[inputs.file.json_v2.object]]
|
||||
path = "features"
|
||||
tags = ["type", "id"]
|
||||
disable_prepend_keys = true
|
||||
Loading…
Reference in New Issue