Skip to content

Commit

Permalink
fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
jotak committed Nov 14, 2024
1 parent c10119c commit f701321
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 96 deletions.
88 changes: 3 additions & 85 deletions pkg/pipeline/encode/metrics/preprocess.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package metrics

import (
"fmt"
"regexp"
"strings"

"github.com/netobserv/flowlogs-pipeline/pkg/api"
"github.com/netobserv/flowlogs-pipeline/pkg/config"
"github.com/netobserv/flowlogs-pipeline/pkg/utils"
"github.com/netobserv/flowlogs-pipeline/pkg/utils/filters"
)

Expand All @@ -24,7 +21,7 @@ type MappedLabel struct {
}

type preprocessedFilter struct {
predicate Predicate
predicate filters.Predicate
useFlat bool
}

Expand All @@ -39,61 +36,7 @@ func (p *Preprocessed) TargetLabels() []string {
return targetLabels
}

func Presence(filter api.MetricsFilter) Predicate {
return func(flow config.GenericMap) bool {
_, found := flow[filter.Key]
return found
}
}

func Absence(filter api.MetricsFilter) Predicate {
pred := Presence(filter)
return func(flow config.GenericMap) bool { return !pred(flow) }
}

func Equal(filter api.MetricsFilter) Predicate {
varLookups := extractVarLookups(filter.Value)
return func(flow config.GenericMap) bool {
if val, found := flow[filter.Key]; found {
sVal, ok := val.(string)
if !ok {
sVal = fmt.Sprint(val)
}
value := filter.Value
if len(varLookups) > 0 {
value = injectVars(flow, value, varLookups)
}
return sVal == value
}
return false
}
}

func NotEqual(filter api.MetricsFilter) Predicate {
pred := Equal(filter)
return func(flow config.GenericMap) bool { return !pred(flow) }
}

func Regex(filter api.MetricsFilter) Predicate {
r, _ := regexp.Compile(filter.Value)
return func(flow config.GenericMap) bool {
if val, found := flow[filter.Key]; found {
sVal, ok := val.(string)
if !ok {
sVal = fmt.Sprint(val)
}
return r.MatchString(sVal)
}
return false
}
}

func NotRegex(filter api.MetricsFilter) Predicate {
pred := Regex(filter)
return func(flow config.GenericMap) bool { return !pred(flow) }
}

func filterToPredicate(filter api.MetricsFilter) Predicate {
func filterToPredicate(filter api.MetricsFilter) filters.Predicate {
switch filter.Type {
case api.MetricFilterEqual:
return filters.Equal(filter.Key, filter.Value, true)
Expand All @@ -111,32 +54,7 @@ func filterToPredicate(filter api.MetricsFilter) Predicate {
return filters.NotRegex(filter.Key, r)
}
// Default = Exact
return Equal(filter)
}

func extractVarLookups(value string) [][]string {
// Extract list of variables to lookup
// E.g: filter "$(SrcAddr):$(SrcPort)" would return [SrcAddr,SrcPort]
if len(value) > 0 {
return variableExtractor.FindAllStringSubmatch(value, -1)
}
return nil
}

func injectVars(flow config.GenericMap, filterValue string, varLookups [][]string) string {
injected := filterValue
for _, matchGroup := range varLookups {
var value string
if rawVal, found := flow[matchGroup[1]]; found {
if sVal, ok := rawVal.(string); ok {
value = sVal
} else {
value = utils.ConvertToString(rawVal)
}
}
injected = strings.ReplaceAll(injected, matchGroup[0], value)
}
return injected
return filters.Equal(filter.Key, filter.Value, true)
}

func Preprocess(def *api.MetricsItem) *Preprocessed {
Expand Down
9 changes: 0 additions & 9 deletions pkg/pipeline/encode/metrics/preprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ import (
"github.com/stretchr/testify/assert"
)

func Test_Filters_extractVarLookups(t *testing.T) {
variables := extractVarLookups("$(abc)--$(def)")

assert.Equal(t, [][]string{{"$(abc)", "abc"}, {"$(def)", "def"}}, variables)

variables = extractVarLookups("")
assert.Empty(t, variables)
}

func Test_Flatten(t *testing.T) {
pp := Preprocess(&api.MetricsItem{Flatten: []string{"interfaces", "events"}})
fl := pp.GenerateFlatParts(config.GenericMap{
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/filters/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/netobserv/flowlogs-pipeline/pkg/utils"
)

type Predicate func(flow config.GenericMap) bool
type Predicate func(config.GenericMap) bool

var variableExtractor = regexp.MustCompile(`\$\(([^\)]+)\)`)

Expand Down Expand Up @@ -106,7 +106,7 @@ func injectVars(flow config.GenericMap, filterValue string, varLookups [][]strin
if sVal, ok := rawVal.(string); ok {
value = sVal
} else {
value = fmt.Sprint(rawVal)
value = utils.ConvertToString(rawVal)
}
}
injected = strings.ReplaceAll(injected, matchGroup[0], value)
Expand Down

0 comments on commit f701321

Please sign in to comment.