Skip to content

Commit e5a61ba

Browse files
belimawrmergify-bot
authored andcommitted
Fix decode_json_fields processor to always add error key (#29107)
When the `decode_json_fields` processor encountered an error while decoding the JSON it was not always respecting the `add_error_key` configuration. This commit fixes it. (cherry picked from commit 37c6229)
1 parent d490c1c commit e5a61ba

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
129129
- Fix `fingerprint` processor to give it access to the `@timestamp` field. {issue}28683[28683]
130130
- Fix the wrong beat name on monitoring and state endpoint {issue}27755[27755]
131131
- Skip configuration checks in autodiscover for configurations that are already running {pull}29048[29048]
132+
- Fix `decode_json_processor` to always respect `add_error_key` {pull}29107[29107]
132133

133134
*Auditbeat*
134135

libbeat/processors/actions/decode_json_fields.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ func (f *decodeJSONFields) Run(event *beat.Event) (*beat.Event, error) {
122122
if err != nil {
123123
f.logger.Debugf("Error trying to unmarshal %s", text)
124124
errs = append(errs, err.Error())
125+
event.SetErrorWithOption(common.MapStr{
126+
"message": "parsing input as JSON: " + err.Error(),
127+
"data": text,
128+
"field": field,
129+
}, f.addErrorKey)
125130
continue
126131
}
127132

libbeat/processors/actions/decode_json_fields_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,27 @@ func TestOverwriteMetadata(t *testing.T) {
491491
assert.Equal(t, expected, actual)
492492
}
493493

494+
func TestAddErrorToEventOnUnmarshalError(t *testing.T) {
495+
testConfig := common.MustNewConfigFrom(map[string]interface{}{
496+
"fields": "message",
497+
"add_error_key": true,
498+
})
499+
500+
input := common.MapStr{
501+
"message": "Broken JSON [[",
502+
}
503+
504+
actual := getActualValue(t, testConfig, input)
505+
506+
errObj, ok := actual["error"].(common.MapStr)
507+
require.True(t, ok, "'error' field not present or of invalid type")
508+
require.NotNil(t, actual["error"])
509+
510+
assert.Equal(t, "message", errObj["field"])
511+
assert.NotNil(t, errObj["data"])
512+
assert.NotNil(t, errObj["message"])
513+
}
514+
494515
func getActualValue(t *testing.T, config *common.Config, input common.MapStr) common.MapStr {
495516
log := logp.NewLogger("decode_json_fields_test")
496517

0 commit comments

Comments
 (0)