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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use crate::layers::Layer;
use aho_corasick::{AhoCorasick, AhoCorasickBuilder};
use metrics::{Counter, Gauge, Histogram, Key, KeyName, Recorder, Unit};
pub struct Filter<R> {
inner: R,
automaton: AhoCorasick,
}
impl<R> Filter<R> {
fn should_filter(&self, key: &str) -> bool {
self.automaton.is_match(key)
}
}
impl<R: Recorder> Recorder for Filter<R> {
fn describe_counter(&self, key_name: KeyName, unit: Option<Unit>, description: &'static str) {
if self.should_filter(key_name.as_str()) {
return;
}
self.inner.describe_counter(key_name, unit, description)
}
fn describe_gauge(&self, key_name: KeyName, unit: Option<Unit>, description: &'static str) {
if self.should_filter(key_name.as_str()) {
return;
}
self.inner.describe_gauge(key_name, unit, description)
}
fn describe_histogram(&self, key_name: KeyName, unit: Option<Unit>, description: &'static str) {
if self.should_filter(key_name.as_str()) {
return;
}
self.inner.describe_histogram(key_name, unit, description)
}
fn register_counter(&self, key: &Key) -> Counter {
if self.should_filter(key.name()) {
return Counter::noop();
}
self.inner.register_counter(key)
}
fn register_gauge(&self, key: &Key) -> Gauge {
if self.should_filter(key.name()) {
return Gauge::noop();
}
self.inner.register_gauge(key)
}
fn register_histogram(&self, key: &Key) -> Histogram {
if self.should_filter(key.name()) {
return Histogram::noop();
}
self.inner.register_histogram(key)
}
}
#[derive(Default)]
pub struct FilterLayer {
patterns: Vec<String>,
case_insensitive: bool,
use_dfa: bool,
}
impl FilterLayer {
pub fn from_patterns<P, I>(patterns: P) -> Self
where
P: IntoIterator<Item = I>,
I: AsRef<str>,
{
FilterLayer {
patterns: patterns.into_iter().map(|s| s.as_ref().to_string()).collect(),
case_insensitive: false,
use_dfa: true,
}
}
pub fn add_pattern<P>(&mut self, pattern: P) -> &mut FilterLayer
where
P: AsRef<str>,
{
self.patterns.push(pattern.as_ref().to_string());
self
}
pub fn case_insensitive(&mut self, case_insensitive: bool) -> &mut FilterLayer {
self.case_insensitive = case_insensitive;
self
}
pub fn use_dfa(&mut self, dfa: bool) -> &mut FilterLayer {
self.use_dfa = dfa;
self
}
}
impl<R> Layer<R> for FilterLayer {
type Output = Filter<R>;
fn layer(&self, inner: R) -> Self::Output {
let mut automaton_builder = AhoCorasickBuilder::new();
let automaton = automaton_builder
.ascii_case_insensitive(self.case_insensitive)
.dfa(self.use_dfa)
.auto_configure(&self.patterns)
.build(&self.patterns);
Filter { inner, automaton }
}
}
#[cfg(test)]
mod tests {
use super::FilterLayer;
use crate::{layers::Layer, test_util::*};
use metrics::{Counter, Gauge, Histogram, Unit};
#[test]
fn test_basic_functionality() {
let inputs = vec![
RecorderOperation::DescribeCounter(
"tokio.loops".into(),
Some(Unit::Count),
"counter desc",
),
RecorderOperation::DescribeGauge(
"hyper.bytes_read".into(),
Some(Unit::Bytes),
"gauge desc",
),
RecorderOperation::DescribeHistogram(
"hyper.response_latency".into(),
Some(Unit::Nanoseconds),
"histogram desc",
),
RecorderOperation::DescribeCounter(
"tokio.spurious_wakeups".into(),
Some(Unit::Count),
"counter desc",
),
RecorderOperation::DescribeGauge(
"bb8.pooled_conns".into(),
Some(Unit::Count),
"gauge desc",
),
RecorderOperation::RegisterCounter("tokio.loops".into(), Counter::noop()),
RecorderOperation::RegisterGauge("hyper.bytes_read".into(), Gauge::noop()),
RecorderOperation::RegisterHistogram(
"hyper.response_latency".into(),
Histogram::noop(),
),
RecorderOperation::RegisterCounter("tokio.spurious_wakeups".into(), Counter::noop()),
RecorderOperation::RegisterGauge("bb8.pooled_conns".into(), Gauge::noop()),
];
let expectations = vec![
RecorderOperation::DescribeGauge(
"hyper.bytes_read".into(),
Some(Unit::Bytes),
"gauge desc",
),
RecorderOperation::DescribeHistogram(
"hyper.response_latency".into(),
Some(Unit::Nanoseconds),
"histogram desc",
),
RecorderOperation::RegisterGauge("hyper.bytes_read".into(), Gauge::noop()),
RecorderOperation::RegisterHistogram(
"hyper.response_latency".into(),
Histogram::noop(),
),
];
let recorder = MockBasicRecorder::from_operations(expectations);
let filter = FilterLayer::from_patterns(&["tokio", "bb8"]);
let filter = filter.layer(recorder);
for operation in inputs {
operation.apply_to_recorder(&filter);
}
}
#[test]
fn test_case_insensitivity() {
let inputs = vec![
RecorderOperation::DescribeCounter(
"tokiO.loops".into(),
Some(Unit::Count),
"counter desc",
),
RecorderOperation::DescribeGauge(
"hyper.bytes_read".into(),
Some(Unit::Bytes),
"gauge desc",
),
RecorderOperation::DescribeHistogram(
"hyper.response_latency".into(),
Some(Unit::Nanoseconds),
"histogram desc",
),
RecorderOperation::DescribeCounter(
"Tokio.spurious_wakeups".into(),
Some(Unit::Count),
"counter desc",
),
RecorderOperation::DescribeGauge(
"bB8.pooled_conns".into(),
Some(Unit::Count),
"gauge desc",
),
RecorderOperation::RegisterCounter("tokiO.loops".into(), Counter::noop()),
RecorderOperation::RegisterGauge("hyper.bytes_read".into(), Gauge::noop()),
RecorderOperation::RegisterHistogram(
"hyper.response_latency".into(),
Histogram::noop(),
),
RecorderOperation::RegisterCounter("Tokio.spurious_wakeups".into(), Counter::noop()),
RecorderOperation::RegisterGauge("bB8.pooled_conns".into(), Gauge::noop()),
];
let expectations = vec![
RecorderOperation::DescribeGauge(
"hyper.bytes_read".into(),
Some(Unit::Bytes),
"gauge desc",
),
RecorderOperation::DescribeHistogram(
"hyper.response_latency".into(),
Some(Unit::Nanoseconds),
"histogram desc",
),
RecorderOperation::RegisterGauge("hyper.bytes_read".into(), Gauge::noop()),
RecorderOperation::RegisterHistogram(
"hyper.response_latency".into(),
Histogram::noop(),
),
];
let recorder = MockBasicRecorder::from_operations(expectations);
let mut filter = FilterLayer::from_patterns(&["tokio", "bb8"]);
let filter = filter.case_insensitive(true).layer(recorder);
for operation in inputs {
operation.apply_to_recorder(&filter);
}
}
}