-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
LIBBEAT: Enhancement replace_string processor for replacing strings values of fields. #17342
Merged
urso
merged 23 commits into
elastic:master
from
premendrasingh:processor_replace_string
Apr 28, 2020
Merged
Changes from 3 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0eaa435
Add processor for replacing strings values
premendrasingh 121b558
Update CHANGELOG.next.asciidoc
premendrasingh 0ccce99
Changed signature according to review comments.
premendrasingh 52b4d6f
Merge remote-tracking branch 'beats_upstream/master' into processor_r…
premendrasingh e2ba62a
Changed pattern to *regexp.Regexp as suggested.
premendrasingh cf2608a
Merge remote-tracking branch 'beats_upstream/master' into processor_r…
premendrasingh f80ec20
Merge remote-tracking branch 'beats_upstream/master' into processor_r…
premendrasingh 9ed18dd
Merge remote-tracking branch 'beats_upstream/master' into processor_r…
premendrasingh 1256e55
Fix formatting errors. Add doc for replace processor
premendrasingh a396379
Fix documentation for replace processor
premendrasingh 147c67c
Merge remote-tracking branch 'beats_upstream/master' into processor_r…
premendrasingh 0c0c44f
Merge remote-tracking branch 'beats_upstream/master' into processor_r…
premendrasingh 4d9f433
Merge remote-tracking branch 'beats_upstream/master' into processor_r…
premendrasingh 277978a
Merge branch 'processor_replace_string' of github.com:premendrasingh/…
premendrasingh 851f33a
Merge remote-tracking branch 'beats_upstream/master' into processor_r…
premendrasingh 2bb9cbc
Merge remote-tracking branch 'beats_upstream/master' into processor_r…
premendrasingh dcd2244
Merge remote-tracking branch 'beats_upstream/master' into processor_r…
premendrasingh b4f7d2c
Merge remote-tracking branch 'beats_upstream/master' into processor_r…
premendrasingh 2561c12
Changed to a simpler example. Thanks for the suggestion
premendrasingh 6bc80cb
Fix sentence
premendrasingh 889c0cf
Update imports to use github.com/elastic/beats/v7/libbeat
premendrasingh 2acc815
Merge remote-tracking branch 'beats_upstream/master' into processor_r…
premendrasingh 4a3427d
Merge remote-tracking branch 'beats_upstream/master' into processor_r…
premendrasingh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package actions | ||
|
||
import ( | ||
"fmt" | ||
"github.com/pkg/errors" | ||
"regexp" | ||
|
||
"github.com/elastic/beats/libbeat/beat" | ||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/logp" | ||
"github.com/elastic/beats/libbeat/processors" | ||
"github.com/elastic/beats/libbeat/processors/checks" | ||
jsprocessor "github.com/elastic/beats/libbeat/processors/script/javascript/module/processor" | ||
) | ||
|
||
type replaceString struct { | ||
config replaceStringConfig | ||
} | ||
|
||
type replaceStringConfig struct { | ||
Fields []replaceConfig `config:"fields"` | ||
IgnoreMissing bool `config:"ignore_missing"` | ||
FailOnError bool `config:"fail_on_error"` | ||
} | ||
|
||
type replaceConfig struct { | ||
Field string `config:"field"` | ||
Pattern string `config:"pattern"` | ||
Replacement string `config:"replacement"` | ||
} | ||
|
||
func init() { | ||
processors.RegisterPlugin("replace", | ||
checks.ConfigChecked(NewReplaceString, | ||
checks.RequireFields("fields"))) | ||
|
||
jsprocessor.RegisterPlugin("Replace", NewReplaceString) | ||
} | ||
|
||
// NewReplaceString returns a new replace processor. | ||
func NewReplaceString(c *common.Config) (processors.Processor, error) { | ||
config := replaceStringConfig{ | ||
IgnoreMissing: false, | ||
FailOnError: true, | ||
} | ||
err := c.Unpack(&config) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to unpack the replace configuration: %s", err) | ||
} | ||
|
||
f := &replaceString{ | ||
config: config, | ||
} | ||
return f, nil | ||
} | ||
|
||
func (f *replaceString) Run(event *beat.Event) (*beat.Event, error) { | ||
var backup common.MapStr | ||
// Creates a copy of the event to revert in case of failure | ||
if f.config.FailOnError { | ||
backup = event.Fields.Clone() | ||
} | ||
|
||
for _, field := range f.config.Fields { | ||
err := f.replaceField(field.Field, field.Pattern, field.Replacement, event.Fields) | ||
if err != nil { | ||
errMsg := fmt.Errorf("Failed to replace fields in processor: %s", err) | ||
logp.Debug("replace", errMsg.Error()) | ||
if f.config.FailOnError { | ||
event.Fields = backup | ||
event.PutValue("error.message", errMsg.Error()) | ||
return event, err | ||
} | ||
} | ||
} | ||
|
||
return event, nil | ||
} | ||
|
||
func (f *replaceString) replaceField(field string, pattern string, replacement string, fields common.MapStr) error { | ||
currentValue, err := fields.GetValue(field) | ||
if err != nil { | ||
// Ignore ErrKeyNotFound errors | ||
if f.config.IgnoreMissing && errors.Cause(err) == common.ErrKeyNotFound { | ||
return nil | ||
} | ||
return fmt.Errorf("could not fetch value for key: %s, Error: %s", field, err) | ||
} | ||
|
||
re := regexp.MustCompile(pattern) | ||
updatedString := re.ReplaceAllString(currentValue.(string), replacement) | ||
_, err = fields.Put(field, updatedString) | ||
if err != nil { | ||
return fmt.Errorf("could not put value: %s: %v, %v", replacement, currentValue, err) | ||
} | ||
return nil | ||
} | ||
|
||
func (f *replaceString) String() string { | ||
return "replace=" + fmt.Sprintf("%+v", f.config.Fields) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package actions | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/beats/libbeat/beat" | ||
"github.com/elastic/beats/libbeat/common" | ||
) | ||
|
||
func TestReplaceRun(t *testing.T) { | ||
var tests = []struct { | ||
description string | ||
Fields []replaceConfig | ||
IgnoreMissing bool | ||
FailOnError bool | ||
Input common.MapStr | ||
Output common.MapStr | ||
error bool | ||
}{ | ||
{ | ||
description: "simple field replacing", | ||
Fields: []replaceConfig{ | ||
{ | ||
Field: "f", | ||
Pattern: "a", | ||
Replacement: "b", | ||
}, | ||
}, | ||
Input: common.MapStr{ | ||
"f": "abc", | ||
}, | ||
Output: common.MapStr{ | ||
"f": "bbc", | ||
}, | ||
error: false, | ||
IgnoreMissing: false, | ||
FailOnError: true, | ||
}, | ||
{ | ||
description: "Add one more hierarchy to event", | ||
Fields: []replaceConfig{ | ||
{ | ||
Field: "f.b", | ||
Pattern: "a", | ||
Replacement: "b", | ||
}, | ||
}, | ||
Input: common.MapStr{ | ||
"f": common.MapStr{ | ||
"b": "abc", | ||
}, | ||
}, | ||
Output: common.MapStr{ | ||
"f": common.MapStr{ | ||
"b": "bbc", | ||
}, | ||
}, | ||
error: false, | ||
IgnoreMissing: false, | ||
FailOnError: true, | ||
}, | ||
{ | ||
description: "replace two fields at the same time.", | ||
Fields: []replaceConfig{ | ||
{ | ||
Field: "f", | ||
Pattern: "a.*c", | ||
Replacement: "cab", | ||
}, | ||
{ | ||
Field: "g", | ||
Pattern: "ef", | ||
Replacement: "oor", | ||
}, | ||
}, | ||
Input: common.MapStr{ | ||
"f": "abbbc", | ||
"g": "def", | ||
}, | ||
Output: common.MapStr{ | ||
"f": "cab", | ||
"g": "door", | ||
}, | ||
error: false, | ||
IgnoreMissing: false, | ||
FailOnError: true, | ||
}, | ||
{ | ||
description: "test missing fields", | ||
Fields: []replaceConfig{ | ||
{ | ||
Field: "f", | ||
Pattern: "abc", | ||
Replacement: "xyz", | ||
}, | ||
{ | ||
Field: "g", | ||
Pattern: "def", | ||
Replacement: "", | ||
}, | ||
}, | ||
Input: common.MapStr{ | ||
"m": "abc", | ||
"n": "def", | ||
}, | ||
Output: common.MapStr{ | ||
"m": "abc", | ||
"n": "def", | ||
"error": common.MapStr{ | ||
"message": "Failed to replace fields in processor: could not fetch value for key: f, Error: key not found", | ||
}, | ||
}, | ||
error: true, | ||
IgnoreMissing: false, | ||
FailOnError: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.description, func(t *testing.T) { | ||
f := &replaceString{ | ||
config: replaceStringConfig{ | ||
Fields: test.Fields, | ||
IgnoreMissing: test.IgnoreMissing, | ||
FailOnError: test.FailOnError, | ||
}, | ||
} | ||
event := &beat.Event{ | ||
Fields: test.Input, | ||
} | ||
|
||
newEvent, err := f.Run(event) | ||
if !test.error { | ||
assert.Nil(t, err) | ||
} else { | ||
assert.NotNil(t, err) | ||
} | ||
|
||
assert.True(t, reflect.DeepEqual(newEvent.Fields, test.Output)) | ||
}) | ||
} | ||
} | ||
|
||
func TestReplaceField(t *testing.T) { | ||
var tests = []struct { | ||
Field string | ||
Pattern string | ||
Replacement string | ||
ignoreMissing bool | ||
failOnError bool | ||
Input common.MapStr | ||
Output common.MapStr | ||
error bool | ||
description string | ||
}{ | ||
{ | ||
description: "replace part of field value with another string", | ||
Field: "f", | ||
Pattern: "a", | ||
Replacement: "b", | ||
Input: common.MapStr{ | ||
"f": "abc", | ||
}, | ||
Output: common.MapStr{ | ||
"f": "bbc", | ||
}, | ||
error: false, | ||
failOnError: true, | ||
ignoreMissing: false, | ||
}, | ||
{ | ||
description: "Add hierarchy to event and replace", | ||
Field: "f.b", | ||
Pattern: "a", | ||
Replacement: "b", | ||
Input: common.MapStr{ | ||
"f": common.MapStr{ | ||
"b": "abc", | ||
}, | ||
}, | ||
Output: common.MapStr{ | ||
"f": common.MapStr{ | ||
"b": "bbc", | ||
}, | ||
}, | ||
error: false, | ||
ignoreMissing: false, | ||
failOnError: true, | ||
}, | ||
{ | ||
description: "try replacing value of missing fields in event", | ||
Field: "f", | ||
Pattern: "abc", | ||
Replacement: "xyz", | ||
Input: common.MapStr{ | ||
"m": "abc", | ||
"n": "def", | ||
}, | ||
Output: common.MapStr{ | ||
"m": "abc", | ||
"n": "def", | ||
}, | ||
error: true, | ||
ignoreMissing: false, | ||
failOnError: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.description, func(t *testing.T) { | ||
|
||
f := &replaceString{ | ||
config: replaceStringConfig{ | ||
IgnoreMissing: test.ignoreMissing, | ||
FailOnError: test.failOnError, | ||
}, | ||
} | ||
|
||
err := f.replaceField(test.Field, test.Pattern, test.Replacement, test.Input) | ||
if err != nil { | ||
assert.Equal(t, test.error, true) | ||
} | ||
|
||
assert.True(t, reflect.DeepEqual(test.Input, test.Output)) | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@urso @exekias should https://github.com/elastic/beats/blob/master/libbeat/common/match/matchers.go be enhanced to do replacing strings and just use
matcher.Match
here?using plain old regex could be slower as compared to the optimized one in libbeat.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for Pattern in the config use
Patter *regexp.Regexp
. Config unpacking will automatically try to compile the regex and fail with error + setting name if this has failed.matcher.Match
is an optimization for some custom cases only, but falls back toregexp
if the case becomes more 'complicated'. Given the optimizations we have in matcher, I only see the case for a constant string match being helpful (which would become a sub-string search, or in some cases string-prefix/suffix comparison).The
matcher
package also replaces capturing-group-matches with non-capturing-groups (greatly reduces allocations). Having patterns and replacement likegsub
, do we want to allow users to use capturing group in the replacement in the future? E.g.For now I would not enhance the
matcher
package. Only if we figure this is indeed a common problem. When doing so we might have to remove some of the optimizations. In case we find we really need to optimize another type (e.g. matcher.Replacer) might give us better flexibility in applying the kind of optimizations we need for the use-case, while not un-optimizingmatcher.Matcher
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@urso Changed to pattern, as suggested.