Skip to content

Commit

Permalink
Merge pull request #592 from nokia/event-tag-value-v2
Browse files Browse the repository at this point in the history
Add processor event-value-tag-v2
  • Loading branch information
karimra authored Jan 26, 2025
2 parents cc8312b + f92c9bb commit 1626e5a
Show file tree
Hide file tree
Showing 7 changed files with 692 additions and 22 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ require (
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.9.0
github.com/xdg/scram v1.0.5
go.starlark.net v0.0.0-20231121155337-90ade8b19d09
golang.org/x/crypto v0.26.0
Expand Down Expand Up @@ -144,6 +145,7 @@ require (
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions pkg/formatters/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ import (
_ "github.com/openconfig/gnmic/pkg/formatters/event_to_tag"
_ "github.com/openconfig/gnmic/pkg/formatters/event_trigger"
_ "github.com/openconfig/gnmic/pkg/formatters/event_value_tag"
_ "github.com/openconfig/gnmic/pkg/formatters/event_value_tag_v2"
_ "github.com/openconfig/gnmic/pkg/formatters/event_write"
)
42 changes: 24 additions & 18 deletions pkg/formatters/event_value_tag/event_value_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func (vt *valueTag) Init(cfg interface{}, opts ...formatters.Option) error {
if err != nil {
return err
}
if vt.TagName == "" {
vt.TagName = vt.ValueName
}
for _, opt := range opts {
opt(vt)
}
Expand All @@ -65,17 +68,17 @@ type tagVal struct {
}

func (vt *valueTag) Apply(evs ...*formatters.EventMsg) []*formatters.EventMsg {
if vt.TagName == "" {
vt.TagName = vt.ValueName
}

cache := make(map[string]bool)
vts := vt.buildApplyRules(evs)
for _, tv := range vts {
for _, ev := range evs {
match := compareTags(tv.tags, ev.Tags, cache)
match := compareTags(tv.tags, ev.Tags)
if match {
ev.Tags[vt.TagName] = fmt.Sprint(tv.value)
switch v := tv.value.(type) {
case string:
ev.Tags[vt.TagName] = v
default:
ev.Tags[vt.TagName] = fmt.Sprint(tv.value)
}
}
}
}
Expand All @@ -95,24 +98,15 @@ func (vt *valueTag) WithTargets(tcs map[string]*types.TargetConfig) {}
func (vt *valueTag) WithActions(act map[string]map[string]interface{}) {}

// returns true if all keys match, false otherwise.
func compareTags(a map[string]string, b map[string]string, cache map[string]bool) bool {
cacheKey := fmt.Sprintf("%v-%v", a, b)
if cachedResult, exists := cache[cacheKey]; exists {
return cachedResult
}

func compareTags(a map[string]string, b map[string]string) bool {
if len(a) > len(b) {
cache[cacheKey] = false
return false
}
for k, v := range a {
if vv, ok := b[k]; !ok || v != vv {
cache[cacheKey] = false
return false
}
}

cache[cacheKey] = true
return true
}

Expand All @@ -122,11 +116,23 @@ func (vt *valueTag) buildApplyRules(evs []*formatters.EventMsg) []*tagVal {
toApply := make([]*tagVal, 0)
for _, ev := range evs {
if v, ok := ev.Values[vt.ValueName]; ok {
toApply = append(toApply, &tagVal{tags: ev.Tags, value: v})
toApply = append(toApply,
&tagVal{
tags: copyTags(ev.Tags),
value: v,
})
if vt.Consume {
delete(ev.Values, vt.ValueName)
}
}
}
return toApply
}

func copyTags(src map[string]string) map[string]string {
dest := make(map[string]string, len(src))
for k, v := range src {
dest[k] = v
}
return dest
}
122 changes: 118 additions & 4 deletions pkg/formatters/event_value_tag/event_value_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package event_value_tag

import (
"fmt"
"log"
"reflect"
"testing"

Expand All @@ -30,6 +31,7 @@ var testset = map[string]struct {
processorType: processorType,
processor: map[string]interface{}{
"value-name": "foo",
"debug": true,
},
tests: []item{
{
Expand All @@ -51,6 +53,16 @@ var testset = map[string]struct {
Tags: map[string]string{"tag": "value"},
Values: map[string]interface{}{"foo": "new_value"},
},
{
Timestamp: 3,
Tags: map[string]string{"other_tag": "value"},
Values: map[string]interface{}{"other_val": "val"},
},
{
Timestamp: 4,
Tags: map[string]string{"foo": "other_value"},
Values: map[string]interface{}{"other_val": "val"},
},
},
output: []*formatters.EventMsg{
{
Expand All @@ -62,6 +74,16 @@ var testset = map[string]struct {
Tags: map[string]string{"tag": "value", "foo": "new_value"},
Values: map[string]interface{}{"foo": "new_value"},
},
{
Timestamp: 3,
Tags: map[string]string{"other_tag": "value"},
Values: map[string]interface{}{"other_val": "val"},
},
{
Timestamp: 4,
Tags: map[string]string{"foo": "other_value"},
Values: map[string]interface{}{"other_val": "val"},
},
},
},
{
Expand Down Expand Up @@ -272,14 +294,102 @@ var testset = map[string]struct {
},
},
},
"integer_val": {
processorType: processorType,
processor: map[string]interface{}{
"value-name": "foo",
},
tests: []item{
{
input: nil,
output: nil,
},
{
input: make([]*formatters.EventMsg, 0),
output: make([]*formatters.EventMsg, 0),
},
{
input: []*formatters.EventMsg{
{
Timestamp: 1,
Tags: map[string]string{"tag": "value"},
},
{
Timestamp: 2,
Tags: map[string]string{"tag": "value"},
Values: map[string]interface{}{"foo": 42},
},
},
output: []*formatters.EventMsg{
{
Timestamp: 1,
Tags: map[string]string{"tag": "value", "foo": "42"},
},
{
Timestamp: 2,
Tags: map[string]string{"tag": "value", "foo": "42"},
Values: map[string]interface{}{"foo": 42},
},
},
},
{
input: []*formatters.EventMsg{
{
Timestamp: 1,
Tags: map[string]string{"tag": "value"},
},
{
Timestamp: 2,
Tags: map[string]string{"tag": "value"},
Values: map[string]interface{}{"bar": "value"},
},
},
output: []*formatters.EventMsg{
{
Timestamp: 1,
Tags: map[string]string{"tag": "value"},
},
{
Timestamp: 2,
Tags: map[string]string{"tag": "value"},
Values: map[string]interface{}{"bar": "value"},
},
},
},
{
input: []*formatters.EventMsg{
{
Timestamp: 1,
Tags: map[string]string{"tag": "value"},
},
{
Timestamp: 2,
Tags: map[string]string{"tag": "value1"},
Values: map[string]interface{}{"foo": "value"},
},
},
output: []*formatters.EventMsg{
{
Timestamp: 1,
Tags: map[string]string{"tag": "value"},
},
{
Timestamp: 2,
Tags: map[string]string{"tag": "value1", "foo": "value"},
Values: map[string]interface{}{"foo": "value"},
},
},
},
},
},
}

func TestEventValueTag(t *testing.T) {
for name, ts := range testset {
if pi, ok := formatters.EventProcessors[ts.processorType]; ok {
t.Log("found processor")
p := pi()
err := p.Init(ts.processor)
err := p.Init(ts.processor, formatters.WithLogger(log.Default()))
if err != nil {
t.Errorf("failed to initialize processors: %v", err)
return
Expand Down Expand Up @@ -320,7 +430,7 @@ func generateEventMsgs(numEvents, numValues int, targetKey, targetValue string)
}

func BenchmarkBuildApplyRules(b *testing.B) {
evs := generateEventMsgs(100, 10, "targetKey", "targetValue")
evs := generateEventMsgs(100_000, 10, "targetKey", "targetValue")
vt := &valueTag{ValueName: "targetKey", Consume: true}

b.ResetTimer()
Expand All @@ -330,7 +440,7 @@ func BenchmarkBuildApplyRules(b *testing.B) {
}

func BenchmarkBuildApplyRules2(b *testing.B) {
evs := generateEventMsgs(100, 10, "targetKey", "targetValue")
evs := generateEventMsgs(100_000, 10, "targetKey", "targetValue")
vt := &valueTag{ValueName: "targetKey", Consume: true}

b.ResetTimer()
Expand All @@ -346,7 +456,11 @@ func (vt *valueTag) buildApplyRules2(evs []*formatters.EventMsg) []*tagVal {
for _, ev := range evs {
for k, v := range ev.Values {
if vt.ValueName == k {
toApply = append(toApply, &tagVal{ev.Tags, v})
toApply = append(toApply, &tagVal{
// copyTags(ev.Tags),
ev.Tags,
v,
})
if vt.Consume {
delete(ev.Values, vt.ValueName)
}
Expand Down
Loading

0 comments on commit 1626e5a

Please sign in to comment.