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

Fix ndjson parser to store JSON fields correctly under target #36089

Merged
merged 6 commits into from
Jul 20, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
- Fix recovering from invalid output configuration when running under Elastic-Agent {pull}36016[36016]
- Improve StreamBuf append to improve performance when reading long lines from files. {pull}35928[35928]
- Eliminate cloning of event in deepUpdate {pull}35945[35945]
- Fix ndjson parser to store JSON fields correctly under `target` {issue}29395[29395]
- Support build of projects outside of beats directory {pull}36126[36126]

*Auditbeat*
Expand Down
48 changes: 48 additions & 0 deletions libbeat/reader/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,54 @@ func TestJSONParsersWithFields(t *testing.T) {
},
},
},
"JSON post processor with dotted target key": {
message: reader.Message{
Content: []byte("{\"key\":\"value\"}"),
Fields: mapstr.M{},
},
config: map[string]interface{}{
"parsers": []map[string]interface{}{
map[string]interface{}{
"ndjson": map[string]interface{}{
"target": "kubernetes.audit",
},
},
},
},
expectedMessage: reader.Message{
Content: []byte(""),
Fields: mapstr.M{
"kubernetes": mapstr.M{
"audit": mapstr.M{
"key": "value",
},
},
},
},
},
"JSON post processor with non-dotted target key": {
message: reader.Message{
Content: []byte("{\"key\":\"value\"}"),
Fields: mapstr.M{},
},
config: map[string]interface{}{
"parsers": []map[string]interface{}{
map[string]interface{}{
"ndjson": map[string]interface{}{
"target": "kubernetes",
},
},
},
},
expectedMessage: reader.Message{
Content: []byte(""),
Fields: mapstr.M{
"kubernetes": mapstr.M{
"key": "value",
},
},
},
},
"JSON post processor with document ID": {
message: reader.Message{
Content: []byte("{\"key\":\"value\", \"my-id-field\":\"my-id\"}"),
Expand Down
4 changes: 3 additions & 1 deletion libbeat/reader/readjson/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@
message.Fields = event.Fields
message.Meta = event.Meta
} else {
message.AddFields(mapstr.M{p.target: jsonFields})
fields := mapstr.M{}
fields.Put(p.target, jsonFields)
message.AddFields(fields)
}

return message, err
Expand Down Expand Up @@ -238,7 +240,7 @@
case time.Time:
ts = t
case common.Time:
ts = time.Time(ts)

Check failure on line 243 in libbeat/reader/readjson/json.go

View workflow job for this annotation

GitHub Actions / lint (linux)

unnecessary conversion (unconvert)
}
delete(data, "@timestamp")
}
Expand Down
Loading