Skip to content

Commit

Permalink
add 'different' filter to prom metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
jpinsonneau committed Feb 22, 2024
1 parent 831e2ad commit 756ebfa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
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
}

0 comments on commit 756ebfa

Please sign in to comment.