Skip to content

Commit

Permalink
Fix ndjson parser to store JSON fields correctly under target (#36089)
Browse files Browse the repository at this point in the history
* Fix ndjson parser to store JSON fields correctly under target

* Update CHANGELOG.next.asciidoc

* Move the change log entry to the right section

---------

Co-authored-by: Shaunak Kashyap <[email protected]>
  • Loading branch information
zipperle and ycombinator authored Jul 20, 2023
1 parent 8a9d6f6 commit 2aba00a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
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 @@ func (p *JSONParser) Next() (reader.Message, error) {
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

0 comments on commit 2aba00a

Please sign in to comment.