forked from StephanU/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.go
157 lines (130 loc) · 3.6 KB
/
filters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package adapter
import (
"strings"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/monitoring"
)
// provide filters for filtering and adapting a metric type
// to monitoring.Var.
// MetricFilter type used to defined and combine filters.
type MetricFilter func(*metricFilters) *metricFilters
// metricFilters provides set of filters to apply to a new metric.
type metricFilters struct {
filters []varFilter
}
type varFilter func(state) state
// state provides the filter state to be changed by every filter.
//
// After filtering the state will be used to choose on the metric name, the
// metric type , or wether the metric is to be ignored.
type state struct {
kind kind
action action
mode monitoring.Mode
reg *monitoring.Registry
name string
metric interface{}
}
// action defines the action to be
type action uint8
// kind defines the kind of operation to be executed
type kind uint8
const (
kndFind kind = iota
kndAdd
kndRemove
)
const (
actIgnore action = iota
actAccept
)
func makeFilters(in ...MetricFilter) *metricFilters {
if len(in) == 0 {
return nil
}
m := &metricFilters{}
for _, mk := range in {
m = mk(m)
}
return m
}
func (m *metricFilters) apply(st state) state {
if m != nil {
for _, filter := range m.filters {
st = filter(st)
}
}
return st
}
func ApplyIf(pred func(name string) bool, filters ...MetricFilter) MetricFilter {
then := makeFilters(filters...)
return withVarFilter(func(st state) state {
if pred(st.name) {
st = then.apply(st)
}
return st
})
}
var Accept = withVarFilter(func(st state) state {
st.action = actAccept
return st
})
// WhitelistIf will accept a metric if the metrics name matches
// the given predicate.
func WhitelistIf(pred func(string) bool) MetricFilter {
return ApplyIf(pred, Accept)
}
// NameIn checks a metrics name matching any of the set names
func NameIn(names ...string) func(string) bool {
return common.MakeStringSet(names...).Has
}
// Whitelist sets a list of metric names to be accepted.
func Whitelist(names ...string) MetricFilter {
return WhitelistIf(NameIn(names...))
}
// ReportIf sets variable report mode for all metrics satisfying the predicate.
func ReportIf(pred func(string) bool) MetricFilter {
return ApplyIf(pred, withVarFilter(func(st state) state {
st.mode = monitoring.Reported
return st
}))
}
// ReportNames enables reporting for all metrics matching any of the given names.
func ReportNames(names ...string) MetricFilter {
return ReportIf(NameIn(names...))
}
// ModifyName changes a metric its name using the provided
// function.
func ModifyName(f func(string) string) MetricFilter {
return withVarFilter(func(st state) state {
st.name = f(st.name)
return st
})
}
// Rename renames a metric to `to`, if the names matches `from`
// If the name matches, it will be automatically white-listed.
func Rename(from, to string) MetricFilter {
return withVarFilter(func(st state) state {
if st.name == from {
st.action, st.name = actAccept, to
}
return st
})
}
// NameReplace replaces substrings in a metrics names with `new`.
func NameReplace(old, new string) MetricFilter {
return ModifyName(func(name string) string {
return strings.Replace(name, old, new, -1)
})
}
// ToLowerName converts all metric names to lower-case
var ToLowerName = ModifyName(strings.ToLower)
// ToUpperName converts all metric name to upper-case
var ToUpperName = ModifyName(strings.ToUpper)
// withVarFilter lifts a varFilter into a MetricFilter
func withVarFilter(f varFilter) MetricFilter {
return func(m *metricFilters) *metricFilters {
m.filters = append(m.filters, f)
return m
}
}