Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update json_v2 parser to handle null types #9368

Merged
merged 3 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions plugins/parsers/json_v2/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
timestamp, err := internal.ParseTimestamp(p.currentSettings.TimestampFormat, result.Value(), p.currentSettings.TimestampTimezone)
if err != nil {
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"
}
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)
}
} else if !result.IsObject() {
v, err := p.convertType(result.Value(), "string", result.SetName)
if err != nil {
return nil, err
if result.Tag {
result.Metric.AddTag(result.OutputName, v.(string))
} else {
result.Metric.AddField(result.OutputName, v)
}
}
result.Metric.AddTag(result.OutputName, v.(string))
}

results = append(results, result)
}

Expand Down
4 changes: 4 additions & 0 deletions plugins/parsers/json_v2/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions plugins/parsers/json_v2/testdata/null/expected.out
Original file line number Diff line number Diff line change
@@ -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"
40 changes: 40 additions & 0 deletions plugins/parsers/json_v2/testdata/null/input.json
Original file line number Diff line number Diff line change
@@ -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
]
}

8 changes: 8 additions & 0 deletions plugins/parsers/json_v2/testdata/null/telegraf.conf
Original file line number Diff line number Diff line change
@@ -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