Skip to content

Commit e917e41

Browse files
bmoylanmergify-bot
authored andcommitted
processors/actions/add_fields: Do not panic if event.Fields is nil map (#28219)
(cherry picked from commit bef0411)
1 parent 200a789 commit e917e41

4 files changed

Lines changed: 16 additions & 3 deletions

File tree

CHANGELOG-developer.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ The list below covers the major changes between 7.0.0-rc2 and master only.
6060
- Fix export dashboard command when running against Elastic Cloud hosted Kibana. {pull}22746[22746]
6161
- Remove `event.dataset` (ECS) annotion from `libbeat.logp`. {issue}27404[27404]
6262
- Errors should be thrown as errors. Metricsets inside Metricbeat will now throw errors as the `error` log level. {pull}27804[27804]
63+
- Avoid panicking in `add_fields` processor when input event.Fields is a nil map. {pull}28219[28219]
6364

6465
==== Added
6566

libbeat/processors/actions/add_fields.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ func NewAddFields(fields common.MapStr, shared bool, overwrite bool) processors.
7373

7474
func (af *addFields) Run(event *beat.Event) (*beat.Event, error) {
7575
fields := af.fields
76-
if af.shared {
76+
if af.shared || event.Fields == nil {
7777
fields = fields.Clone()
7878
}
7979

80-
if af.overwrite {
80+
if event.Fields == nil {
81+
event.Fields = fields
82+
} else if af.overwrite {
8183
event.Fields.DeepUpdate(fields)
8284
} else {
8385
event.Fields.DeepUpdateNoOverwrite(fields)

libbeat/processors/actions/add_fields_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,12 @@ func TestAddFields(t *testing.T) {
113113
`{add_fields: {target: "", fields: {a.change: b}}}`,
114114
),
115115
},
116+
"add fields to nil event": {
117+
event: nil,
118+
want: common.MapStr{
119+
"fields": common.MapStr{"field": "test"},
120+
},
121+
cfg: single(`{add_fields: {fields: {field: test}}}`),
122+
},
116123
})
117124
}

libbeat/processors/actions/common_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ func testProcessors(t *testing.T, cases map[string]testCase) {
5050
}
5151
}
5252

53-
current := &beat.Event{Fields: test.event.Clone()}
53+
current := &beat.Event{}
54+
if test.event != nil {
55+
current.Fields = test.event.Clone()
56+
}
5457
for i, processor := range ps {
5558
var err error
5659
current, err = processor.Run(current)

0 commit comments

Comments
 (0)