Skip to content

Commit

Permalink
Merge pull request #365 from openconfig/add-tag-delete
Browse files Browse the repository at this point in the history
add a `deletes` field to event-add-tag processor
  • Loading branch information
karimra authored Jan 25, 2024
2 parents 93fdcc5 + 873e109 commit 91f4205
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 30 deletions.
2 changes: 2 additions & 0 deletions docs/user_guide/event_processors/event_add_tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 40 additions & 30 deletions pkg/formatters/event_add_tag/event_add_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
55 changes: 55 additions & 0 deletions pkg/formatters/event_add_tag/event_add_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 91f4205

Please sign in to comment.