Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

processors/actions/add_fields: Do not panic if event.Fields is nil map #28219

Merged
merged 4 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-developer.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ The list below covers the major changes between 7.0.0-rc2 and master only.
- 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
adriansr marked this conversation as resolved.
Show resolved Hide resolved
} 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