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

[7.17](backport #35945) Decrease Clones #36593

Merged
merged 4 commits into from
Sep 18, 2023
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
8 changes: 1 addition & 7 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

*Affecting all Beats*

*Filebeat*
- Eliminate cloning of event in deepUpdate {pull}35945[35945]

*Auditbeat*

Expand Down Expand Up @@ -141,9 +141,3 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
*Functionbeat*

==== Known Issue






77 changes: 47 additions & 30 deletions libbeat/beat/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,45 +98,62 @@ func (e *Event) deepUpdate(d common.MapStr, overwrite bool) {
if len(d) == 0 {
return
}
fieldsUpdate := d.Clone() // so we can delete redundant keys

var metaUpdate common.MapStr
// It's supported to update the timestamp using this function.
// However, we must handle it separately since it's a separate field of the event.
timestampValue, timestampExists := d[timestampFieldKey]
if timestampExists {
if overwrite {
_ = e.setTimestamp(timestampValue)
}

for fieldKey, value := range d {
switch fieldKey {
// Temporary delete it from the update map,
// so we can do `e.Fields.DeepUpdate(d)` or
// `e.Fields.DeepUpdateNoOverwrite(d)` later
delete(d, timestampFieldKey)
}

// one of the updates is the timestamp which is not a part of the event fields
case timestampFieldKey:
if overwrite {
_ = e.setTimestamp(value)
}
delete(fieldsUpdate, fieldKey)
// It's supported to update the metadata using this function.
// However, we must handle it separately since it's a separate field of the event.
metaValue, metaExists := d[metadataFieldKey]
if metaExists {
var metaUpdate common.MapStr

switch meta := metaValue.(type) {
case common.MapStr:
metaUpdate = meta
case map[string]interface{}:
metaUpdate = common.MapStr(meta)
}

// some updates are addressed for the metadata not the fields
case metadataFieldKey:
switch meta := value.(type) {
case common.MapStr:
metaUpdate = meta
case map[string]interface{}:
metaUpdate = common.MapStr(meta)
if metaUpdate != nil {
if e.Meta == nil {
e.Meta = common.MapStr{}
}
if overwrite {
e.Meta.DeepUpdate(metaUpdate)
} else {
e.Meta.DeepUpdateNoOverwrite(metaUpdate)
}

delete(fieldsUpdate, fieldKey)
}

// Temporary delete it from the update map,
// so we can do `e.Fields.DeepUpdate(d)` or
// `e.Fields.DeepUpdateNoOverwrite(d)` later
delete(d, metadataFieldKey)
}

if metaUpdate != nil {
if e.Meta == nil {
e.Meta = common.MapStr{}
// At the end we revert all changes we made to the update map
defer func() {
if timestampExists {
d[timestampFieldKey] = timestampValue
}
if overwrite {
e.Meta.DeepUpdate(metaUpdate)
} else {
e.Meta.DeepUpdateNoOverwrite(metaUpdate)
if metaExists {
d[metadataFieldKey] = metaValue
}
}
}()

if len(fieldsUpdate) == 0 {
if len(d) == 0 {
return
}

Expand All @@ -145,9 +162,9 @@ func (e *Event) deepUpdate(d common.MapStr, overwrite bool) {
}

if overwrite {
e.Fields.DeepUpdate(fieldsUpdate)
e.Fields.DeepUpdate(d)
} else {
e.Fields.DeepUpdateNoOverwrite(fieldsUpdate)
e.Fields.DeepUpdateNoOverwrite(d)
}
}

Expand Down
Loading
Loading