Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NETOBSERV-1466 add 'different' filter to prom metrics #605

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const (
AddKubernetesInfraRuleType = "add_kubernetes_infra"
ReinterpretDirectionRuleType = "reinterpret_direction"
PromFilterExact = "exact"
PromFilterDifferent = "different"
PromFilterPresence = "presence"
PromFilterAbsence = "absence"
PromFilterRegex = "regex"
Expand Down
11 changes: 6 additions & 5 deletions pkg/api/encode_prom.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ type MetricsItems []MetricsItem
type MetricsFilter struct {
Key string `yaml:"key" json:"key" doc:"the key to match and filter by"`
Value string `yaml:"value" json:"value" doc:"the value to match and filter by"`
Type string `yaml:"type" json:"type" enum:"MetricEncodeFilterTypeEnum" doc:"the type of filter match: exact (default), presence, absence or regex"`
Type string `yaml:"type" json:"type" enum:"MetricEncodeFilterTypeEnum" doc:"the type of filter match: exact (default), different, presence, absence or regex"`
}

type MetricEncodeFilterTypeEnum struct {
Exact string `yaml:"exact" json:"exact" doc:"match exactly the provided fitler value"`
Presence string `yaml:"presence" json:"presence" doc:"filter key must be present (filter value is ignored)"`
Absence string `yaml:"absence" json:"absence" doc:"filter key must be absent (filter value is ignored)"`
Regex string `yaml:"regex" json:"regex" doc:"match filter value as a regular expression"`
Exact string `yaml:"exact" json:"exact" doc:"match exactly the provided fitler value"`
Different string `yaml:"different" json:"different" doc:"not match the provided fitler value"`
Presence string `yaml:"presence" json:"presence" doc:"filter key must be present (filter value is ignored)"`
Absence string `yaml:"absence" json:"absence" doc:"filter key must be absent (filter value is ignored)"`
Regex string `yaml:"regex" json:"regex" doc:"match filter value as a regular expression"`
}

func MetricEncodeFilterTypeName(t string) string {
Expand Down
42 changes: 27 additions & 15 deletions pkg/pipeline/encode/encode_prom_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,29 @@ type MetricInfo struct {
FilterPredicates []Predicate
}

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

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

func Exact(filter api.MetricsFilter) Predicate {
func exact(filter api.MetricsFilter) 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
return matchExactly(flow, filter)
}
}

func different(filter api.MetricsFilter) Predicate {
return func(flow config.GenericMap) bool {
return !matchExactly(flow, filter)
}
}

Expand All @@ -59,16 +58,18 @@ func regex(filter api.MetricsFilter) Predicate {
func filterToPredicate(filter api.MetricsFilter) Predicate {
switch filter.Type {
case api.PromFilterExact:
return Exact(filter)
return exact(filter)
case api.PromFilterDifferent:
return different(filter)
case api.PromFilterPresence:
return Presence(filter)
return presence(filter)
case api.PromFilterAbsence:
return Absence(filter)
return absence(filter)
case api.PromFilterRegex:
return regex(filter)
}
// Default = Exact
return Exact(filter)
return exact(filter)
}

func CreateMetricInfo(def api.MetricsItem) *MetricInfo {
Expand All @@ -80,3 +81,14 @@ func CreateMetricInfo(def api.MetricsItem) *MetricInfo {
}
return &mi
}

func matchExactly(flow config.GenericMap, filter api.MetricsFilter) bool {
if val, found := flow[filter.Key]; found {
sVal, ok := val.(string)
if !ok {
sVal = fmt.Sprint(val)
}
return sVal == filter.Value
}
return false
}
Loading