Skip to content

Commit

Permalink
processors/actions/add_fields: Do not panic if event.Fields is nil map (
Browse files Browse the repository at this point in the history
#28219)

(cherry picked from commit bef0411)
  • Loading branch information
bmoylan authored and mergify-bot committed Oct 7, 2021
1 parent f7e2366 commit 60b5585
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-developer.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ The list below covers the major changes between 7.0.0-rc2 and master only.
- Metricbeat module builders call host parser only once when instantiating light modules. {pull}20149[20149]
- Fix export dashboard command when running against Elastic Cloud hosted Kibana. {pull}22746[22746]
- Remove `event.dataset` (ECS) annotion from `libbeat.logp`. {issue}27404[27404]
- Errors should be thrown as errors. Metricsets inside Metricbeat will now throw errors as the `error` log level. {pull}27804[27804]
- Avoid panicking in `add_fields` processor when input event.Fields is a nil map. {pull}28219[28219]

==== Added

Expand Down
6 changes: 4 additions & 2 deletions libbeat/processors/actions/add_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ func NewAddFields(fields common.MapStr, shared bool, overwrite bool) processors.

func (af *addFields) Run(event *beat.Event) (*beat.Event, error) {
fields := af.fields
if af.shared {
if af.shared || event.Fields == nil {
fields = fields.Clone()
}

if af.overwrite {
if event.Fields == nil {
event.Fields = fields
} else if af.overwrite {
event.Fields.DeepUpdate(fields)
} else {
event.Fields.DeepUpdateNoOverwrite(fields)
Expand Down
7 changes: 7 additions & 0 deletions libbeat/processors/actions/add_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,12 @@ func TestAddFields(t *testing.T) {
`{add_fields: {target: "", fields: {a.change: b}}}`,
),
},
"add fields to nil event": {
event: nil,
want: common.MapStr{
"fields": common.MapStr{"field": "test"},
},
cfg: single(`{add_fields: {fields: {field: test}}}`),
},
})
}
5 changes: 4 additions & 1 deletion libbeat/processors/actions/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ func testProcessors(t *testing.T, cases map[string]testCase) {
}
}

current := &beat.Event{Fields: test.event.Clone()}
current := &beat.Event{}
if test.event != nil {
current.Fields = test.event.Clone()
}
for i, processor := range ps {
var err error
current, err = processor.Run(current)
Expand Down

0 comments on commit 60b5585

Please sign in to comment.