-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NETOBSERV-1231: Improve promencode API: scaling & filters (#513)
* Improve promencode API: scaling & filters - New ValueScale config to allow scale conversion, e.g. RTT ns to seconds - To avoid having reserved characters or words in filters, use a dedicated API instead: new setting Filter.Type (exact,presence,absence or regex) - Pre-compute regex by setting up a "predicate" function per filter * Rename func, add doc
- Loading branch information
Showing
8 changed files
with
208 additions
and
60 deletions.
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
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
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
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,82 @@ | ||
package encode | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/netobserv/flowlogs-pipeline/pkg/api" | ||
"github.com/netobserv/flowlogs-pipeline/pkg/config" | ||
) | ||
|
||
type predicate func(flow config.GenericMap) bool | ||
|
||
type metricInfo struct { | ||
api.PromMetricsItem | ||
filterPredicates []predicate | ||
} | ||
|
||
func presence(filter api.PromMetricsFilter) predicate { | ||
return func(flow config.GenericMap) bool { | ||
_, found := flow[filter.Key] | ||
return found | ||
} | ||
} | ||
|
||
func absence(filter api.PromMetricsFilter) predicate { | ||
return func(flow config.GenericMap) bool { | ||
_, found := flow[filter.Key] | ||
return !found | ||
} | ||
} | ||
|
||
func exact(filter api.PromMetricsFilter) predicate { | ||
return func(flow config.GenericMap) bool { | ||
if val, found := flow[filter.Key]; found { | ||
sVal, ok := val.(string) | ||
if !ok { | ||
sVal = fmt.Sprint(val) | ||
} | ||
return sVal == filter.Value | ||
} | ||
return false | ||
} | ||
} | ||
|
||
func regex(filter api.PromMetricsFilter) 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 filterToPredicate(filter api.PromMetricsFilter) predicate { | ||
switch filter.Type { | ||
case api.PromFilterExact: | ||
return exact(filter) | ||
case api.PromFilterPresence: | ||
return presence(filter) | ||
case api.PromFilterAbsence: | ||
return absence(filter) | ||
case api.PromFilterRegex: | ||
return regex(filter) | ||
} | ||
// Default = exact | ||
return exact(filter) | ||
} | ||
|
||
func CreateMetricInfo(def api.PromMetricsItem) *metricInfo { | ||
mi := metricInfo{ | ||
PromMetricsItem: def, | ||
} | ||
for _, f := range def.GetFilters() { | ||
mi.filterPredicates = append(mi.filterPredicates, filterToPredicate(f)) | ||
} | ||
return &mi | ||
} |
Oops, something went wrong.