fix(parsers.json_v2): Handle measurements with multiple objects correctly (#16878)

This commit is contained in:
elv-gilles 2025-05-08 13:07:01 +02:00 committed by GitHub
parent f5404aee67
commit c31d402b72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 59 additions and 6 deletions

View File

@ -151,6 +151,8 @@ func (p *Parser) parseCriticalPath(input []byte) ([]telegraf.Metric, error) {
} }
var metrics []telegraf.Metric var metrics []telegraf.Metric
// timestamp defaults to current time
now := time.Now()
for _, c := range p.Configs { for _, c := range p.Configs {
// Measurement name can either be hardcoded, or parsed from the JSON using a GJSON path expression // Measurement name can either be hardcoded, or parsed from the JSON using a GJSON path expression
@ -162,8 +164,8 @@ func (p *Parser) parseCriticalPath(input []byte) ([]telegraf.Metric, error) {
} }
} }
// timestamp defaults to current time, or can be parsed from the JSON using a GJSON path expression // timestamp can be parsed from the JSON using a GJSON path expression
timestamp := time.Now() timestamp := now
if c.TimestampPath != "" { if c.TimestampPath != "" {
result := gjson.GetBytes(input, c.TimestampPath) result := gjson.GetBytes(input, c.TimestampPath)
@ -200,13 +202,15 @@ func (p *Parser) parseCriticalPath(input []byte) ([]telegraf.Metric, error) {
return nil, err return nil, err
} }
metrics = append(metrics, cartesianProduct(tags, fields)...) cmetrics := cartesianProduct(tags, fields)
if len(objects) != 0 && len(metrics) != 0 { if len(objects) != 0 && len(cmetrics) != 0 {
metrics = cartesianProduct(objects, metrics) cmetrics = cartesianProduct(objects, cmetrics)
} else { } else {
metrics = append(metrics, objects...) cmetrics = append(cmetrics, objects...)
} }
metrics = append(metrics, cmetrics...)
} }
for k, v := range p.DefaultTags { for k, v := range p.DefaultTags {

View File

@ -0,0 +1,5 @@
api_http_status_codes 200=11586,201=16,202=14,204=8,404=43,500=0,503=0 1741532840501119000
api_requests requests=11668 1741532840501225000
api_responses responses=11667 1741532840501263000
api_av sessions_added=0,sessions_removed=0 1741544068286646000

View File

@ -0,0 +1,21 @@
{
"api": {
"http_status_codes": {
"200": 11586,
"201": 16,
"202": 14,
"204": 8,
"404": 43,
"500": 0,
"503": 0
},
"requests": 11668,
"responses": 11667,
"av": {
"sessions": {
"added": 0,
"removed": 0
}
}
}
}

View File

@ -0,0 +1,23 @@
[[inputs.file]]
files = ["./testdata/object_multiple/input.json"]
data_format = "json_v2"
[[inputs.file.json_v2]]
measurement_name = "api_http_status_codes"
[[inputs.file.json_v2.object]]
path = "api.http_status_codes"
[[inputs.file.json_v2]]
measurement_name = "api_requests"
[[inputs.file.json_v2.field]]
path = "api.requests"
[[inputs.file.json_v2]]
measurement_name = "api_responses"
[[inputs.file.json_v2.field]]
path = "api.responses"
[[inputs.file.json_v2]]
measurement_name = "api_av"
[[inputs.file.json_v2.object]]
path = "api.av"