From 33e16ba0a6cae43d092b2211d549dda1c66163dc Mon Sep 17 00:00:00 2001 From: Karim Radhouani Date: Thu, 25 Jan 2024 08:08:17 -0800 Subject: [PATCH 1/2] add a `deletes` field to event-add-tag processor --- pkg/formatters/event_add_tag/event_add_tag.go | 70 +++++++++++-------- .../event_add_tag/event_add_tag_test.go | 55 +++++++++++++++ 2 files changed, 95 insertions(+), 30 deletions(-) diff --git a/pkg/formatters/event_add_tag/event_add_tag.go b/pkg/formatters/event_add_tag/event_add_tag.go index 6ab42496..98e7e254 100644 --- a/pkg/formatters/event_add_tag/event_add_tag.go +++ b/pkg/formatters/event_add_tag/event_add_tag.go @@ -28,13 +28,14 @@ const ( loggingPrefix = "[" + processorType + "] " ) -// addTag adds a set of tags to the event message if tag +// addTag adds a set of tags to the event message if certain criteria's are met. type addTag struct { Condition string `mapstructure:"condition,omitempty"` Tags []string `mapstructure:"tags,omitempty" json:"tags,omitempty"` Values []string `mapstructure:"values,omitempty" json:"values,omitempty"` TagNames []string `mapstructure:"tag-names,omitempty" json:"tag-names,omitempty"` ValueNames []string `mapstructure:"value-names,omitempty" json:"value-names,omitempty"` + Deletes []string `mapstructure:"deletes,omitempty" json:"deletes,omitempty"` Overwrite bool `mapstructure:"overwrite,omitempty" json:"overwrite,omitempty"` Add map[string]string `mapstructure:"add,omitempty" json:"add,omitempty"` Debug bool `mapstructure:"debug,omitempty" json:"debug,omitempty"` @@ -43,6 +44,7 @@ type addTag struct { values []*regexp.Regexp tagNames []*regexp.Regexp valueNames []*regexp.Regexp + deletes []*regexp.Regexp code *gojq.Code logger *log.Logger } @@ -75,42 +77,30 @@ func (p *addTag) Init(cfg interface{}, opts ...formatters.Option) error { } } // init tags regex - p.tags = make([]*regexp.Regexp, 0, len(p.Tags)) - for _, reg := range p.Tags { - re, err := regexp.Compile(reg) - if err != nil { - return err - } - p.tags = append(p.tags, re) + p.tags, err = compileRegex(p.Tags) + if err != nil { + return err } // init tag names regex - p.tagNames = make([]*regexp.Regexp, 0, len(p.TagNames)) - for _, reg := range p.TagNames { - re, err := regexp.Compile(reg) - if err != nil { - return err - } - p.tagNames = append(p.tagNames, re) + p.tagNames, err = compileRegex(p.TagNames) + if err != nil { + return err } // init values regex - p.values = make([]*regexp.Regexp, 0, len(p.Values)) - for _, reg := range p.Values { - re, err := regexp.Compile(reg) - if err != nil { - return err - } - p.values = append(p.values, re) + p.values, err = compileRegex(p.Values) + if err != nil { + return err } // init value names regex - p.valueNames = make([]*regexp.Regexp, 0, len(p.ValueNames)) - for _, reg := range p.ValueNames { - re, err := regexp.Compile(reg) - if err != nil { - return err - } - p.valueNames = append(p.valueNames, re) + p.valueNames, err = compileRegex(p.ValueNames) + if err != nil { + return err + } + // init deletes regex + p.deletes, err = compileRegex(p.Deletes) + if err != nil { + return err } - if p.logger.Writer() != io.Discard { b, err := json.Marshal(p) if err != nil { @@ -169,6 +159,14 @@ func (p *addTag) Apply(es ...*formatters.EventMsg) []*formatters.EventMsg { } } } + for _, k := range e.Deletes { + for _, re := range p.deletes { + if re.MatchString(k) { + p.addTags(e) + break + } + } + } } return es } @@ -201,3 +199,15 @@ func (p *addTag) addTags(e *formatters.EventMsg) { } } } + +func compileRegex(expr []string) ([]*regexp.Regexp, error) { + res := make([]*regexp.Regexp, 0, len(expr)) + for _, reg := range expr { + re, err := regexp.Compile(reg) + if err != nil { + return nil, err + } + res = append(res, re) + } + return res, nil +} diff --git a/pkg/formatters/event_add_tag/event_add_tag_test.go b/pkg/formatters/event_add_tag/event_add_tag_test.go index a8e60855..51e0c62f 100644 --- a/pkg/formatters/event_add_tag/event_add_tag_test.go +++ b/pkg/formatters/event_add_tag/event_add_tag_test.go @@ -850,6 +850,61 @@ var testset = map[string]struct { }, }, }, + // match delete + "match_delete_add": { + processorType: processorType, + processor: map[string]interface{}{ + "deletes": []string{"^deleted_path.*"}, + "add": map[string]string{"tag1": "new_tag"}, + }, + tests: []item{ + { + input: nil, + output: nil, + }, + { + input: make([]*formatters.EventMsg, 0), + output: make([]*formatters.EventMsg, 0), + }, + { + input: []*formatters.EventMsg{ + { + Tags: map[string]string{"old_tag": "tag_value"}, + Deletes: []string{"deleted_path"}, + }, + }, + output: []*formatters.EventMsg{ + { + Tags: map[string]string{ + "old_tag": "tag_value", + "tag1": "new_tag", + }, + Deletes: []string{"deleted_path"}, + }, + }, + }, + { + input: []*formatters.EventMsg{ + { + Tags: map[string]string{ + "old_tag": "tag_value", + "tag1": "old_value", + }, + Deletes: []string{"non_matching_deleted_path"}, + }, + }, + output: []*formatters.EventMsg{ + { + Tags: map[string]string{ + "old_tag": "tag_value", + "tag1": "old_value", + }, + Deletes: []string{"non_matching_deleted_path"}, + }, + }, + }, + }, + }, } func TestEventAddTag(t *testing.T) { From 873e1098339961b63cc16f292f12ab1028a9f90d Mon Sep 17 00:00:00 2001 From: Karim Radhouani Date: Thu, 25 Jan 2024 10:16:44 -0800 Subject: [PATCH 2/2] update docs --- docs/user_guide/event_processors/event_add_tag.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/user_guide/event_processors/event_add_tag.md b/docs/user_guide/event_processors/event_add_tag.md index d88fd62e..ce7ee922 100644 --- a/docs/user_guide/event_processors/event_add_tag.md +++ b/docs/user_guide/event_processors/event_add_tag.md @@ -18,6 +18,8 @@ processors: value-names: # list of regular expressions to be matched against the values, if matched, the tags are added values: + # list of regular expressions to be matched against the deleted paths, if matched, the tags are added + deletes: # boolean, if true tags are over-written with the added ones if they already exist. overwrite: # map of tags to be added