-
Notifications
You must be signed in to change notification settings - Fork 772
/
MetricsFilterTest.java
237 lines (182 loc) · 8.62 KB
/
MetricsFilterTest.java
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
package io.prometheus.client.filter;
import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import org.junit.After;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class MetricsFilterTest {
public static final String GET = "GET";
public static final String POST = "POST";
MetricsFilter f = new MetricsFilter();
@After
public void clear() {
CollectorRegistry.defaultRegistry.clear();
}
@Test
public void init() throws Exception {
FilterConfig cfg = mock(FilterConfig.class);
when(cfg.getInitParameter(anyString())).thenReturn(null);
String metricName = "foo";
when(cfg.getInitParameter(MetricsFilter.METRIC_NAME_PARAM)).thenReturn(metricName);
when(cfg.getInitParameter(MetricsFilter.PATH_COMPONENT_PARAM)).thenReturn("4");
f.init(cfg);
assertEquals(f.pathComponents, 4);
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getRequestURI()).thenReturn("/foo/bar/baz/bang/zilch/zip/nada");
when(req.getMethod()).thenReturn(GET);
HttpServletResponse res = mock(HttpServletResponse.class);
FilterChain c = mock(FilterChain.class);
f.doFilter(req, res, c);
verify(c).doFilter(req, res);
final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue(metricName + "_count", new String[]{"path", "method"}, new String[]{"/foo/bar/baz/bang", GET});
assertNotNull(sampleValue);
assertEquals(1, sampleValue, 0.0001);
}
@Test
public void doFilter() throws Exception {
HttpServletRequest req = mock(HttpServletRequest.class);
final String path = "/foo/bar/baz/bang/zilch/zip/nada";
when(req.getRequestURI()).thenReturn(path);
when(req.getMethod()).thenReturn(GET);
HttpServletResponse res = mock(HttpServletResponse.class);
FilterChain c = mock(FilterChain.class);
String name = "foo";
FilterConfig cfg = mock(FilterConfig.class);
when(cfg.getInitParameter(MetricsFilter.METRIC_NAME_PARAM)).thenReturn(name);
when(cfg.getInitParameter(MetricsFilter.PATH_COMPONENT_PARAM)).thenReturn("0");
f.init(cfg);
f.doFilter(req, res, c);
verify(c).doFilter(req, res);
final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue(name + "_count", new String[]{"path", "method"}, new String[]{path, GET});
assertNotNull(sampleValue);
assertEquals(1, sampleValue, 0.0001);
}
@Test
public void testConstructor() throws Exception {
HttpServletRequest req = mock(HttpServletRequest.class);
final String path = "/foo/bar/baz/bang";
when(req.getRequestURI()).thenReturn(path);
when(req.getMethod()).thenReturn(POST);
FilterChain c = mock(FilterChain.class);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
Thread.sleep(100);
return null;
}
}).when(c).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
MetricsFilter constructed = new MetricsFilter(
"foobar_baz_filter_duration_seconds",
"Help for my filter",
0,
null
);
constructed.init(mock(FilterConfig.class));
HttpServletResponse res = mock(HttpServletResponse.class);
constructed.doFilter(req, res, c);
final Double sum = CollectorRegistry.defaultRegistry.getSampleValue("foobar_baz_filter_duration_seconds_sum", new String[]{"path", "method"}, new String[]{path, POST});
assertNotNull(sum);
assertEquals(0.1, sum, 0.01);
}
@Test
public void testBucketsAndName() throws Exception {
HttpServletRequest req = mock(HttpServletRequest.class);
final String path = "/foo/bar/baz/bang";
when(req.getRequestURI()).thenReturn(path);
when(req.getMethod()).thenReturn(POST);
FilterChain c = mock(FilterChain.class);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
Thread.sleep(100);
return null;
}
}).when(c).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
final String buckets = "0.01,0.05,0.1,0.15,0.25";
FilterConfig cfg = mock(FilterConfig.class);
when(cfg.getInitParameter(MetricsFilter.BUCKET_CONFIG_PARAM)).thenReturn(buckets);
when(cfg.getInitParameter(MetricsFilter.METRIC_NAME_PARAM)).thenReturn("foo");
HttpServletResponse res = mock(HttpServletResponse.class);
f.init(cfg);
f.doFilter(req, res, c);
final Double sum = CollectorRegistry.defaultRegistry.getSampleValue("foo_sum", new String[]{"path", "method"}, new String[]{"/foo", POST});
assertEquals(0.1, sum, 0.01);
final Double le05 = CollectorRegistry.defaultRegistry.getSampleValue("foo_bucket", new String[]{"path", "method", "le"}, new String[]{"/foo", POST, "0.05"});
assertNotNull(le05);
assertEquals(0, le05, 0.01);
final Double le15 = CollectorRegistry.defaultRegistry.getSampleValue("foo_bucket", new String[]{"path", "method", "le"}, new String[]{"/foo", POST, "0.15"});
assertNotNull(le15);
assertEquals(1, le15, 0.01);
final Enumeration<Collector.MetricFamilySamples> samples = CollectorRegistry.defaultRegistry.metricFamilySamples();
Collector.MetricFamilySamples sample = null;
while(samples.hasMoreElements()) {
sample = samples.nextElement();
if (sample.name.equals("foo")) {
break;
}
}
assertNotNull(sample);
int count = 0;
for (Collector.MetricFamilySamples.Sample s : sample.samples) {
if (s.name.equals("foo_bucket")) {
count++;
}
}
// +1 because of the final le=+infinity bucket
assertEquals(buckets.split(",").length+1, count);
}
@Test
public void testStatusCode() throws Exception {
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getRequestURI()).thenReturn("/foo/bar/baz/bang");
when(req.getMethod()).thenReturn(GET);
HttpServletResponse res = mock(HttpServletResponse.class);
when(res.getStatus()).thenReturn(200);
FilterChain c = mock(FilterChain.class);
MetricsFilter constructed = new MetricsFilter(
"foobar_filter",
"Help for my filter",
2,
null
);
constructed.init(mock(FilterConfig.class));
constructed.doFilter(req, res, c);
final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue("foobar_filter_status_total", new String[]{"path", "method", "status"}, new String[]{"/foo/bar", GET, "200"});
assertNotNull(sampleValue);
assertEquals(1, sampleValue, 0.0001);
}
@Test
public void testStatusCodeWithNonHttpServletResponse() throws Exception {
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getRequestURI()).thenReturn("/foo/bar/baz/bang");
when(req.getMethod()).thenReturn(GET);
ServletResponse res = mock(ServletResponse.class);
FilterChain c = mock(FilterChain.class);
MetricsFilter constructed = new MetricsFilter(
"foobar_filter",
"Help for my filter",
2,
null
);
constructed.init(mock(FilterConfig.class));
constructed.doFilter(req, res, c);
final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue("foobar_filter_status_total", new String[]{"path", "method", "status"}, new String[]{"/foo/bar", GET, MetricsFilter.UNKNOWN_HTTP_STATUS_CODE});
assertNotNull(sampleValue);
assertEquals(1, sampleValue, 0.0001);
}
}