Skip to content

Commit

Permalink
Add raw json field when unmarshaling fails (#6591)
Browse files Browse the repository at this point in the history
Add raw json to `message` field when JSON unmarshaling fails. This ensures that no data loss occurs.

Fixes #6516
  • Loading branch information
crazyvikas23 authored and andrewkroh committed Apr 16, 2018
1 parent 13fca49 commit 88911e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ https://github.com/elastic/beats/compare/v6.0.0-beta2...master[Check the HEAD di

- Fix panic when log prospector configuration fails to load. {issue}6800[6800]
- Fix memory leak in log prospector when files cannot be read. {issue}6797[6797]
- Add raw JSON to message field when JSON parsing fails. {issue}6516[6516]

*Heartbeat*

Expand Down
7 changes: 7 additions & 0 deletions filebeat/harvester/reader/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ func MergeJSONFields(data common.MapStr, jsonFields common.MapStr, text *string,
jsonFields[config.MessageKey] = *text
}

// handle the case in which r.cfg.AddErrorKey is set and len(jsonFields) == 1
// and only thing it contains is `error` key due to error in json decoding
// which results in loss of message key in the main beat event
if len(jsonFields) == 1 && jsonFields["error"] != nil {
data["message"] = *text
}

if config.KeysUnderRoot {
// Delete existing json key
delete(data, "json")
Expand Down

0 comments on commit 88911e7

Please sign in to comment.