-
Notifications
You must be signed in to change notification settings - Fork 962
/
OkHttpMetricsEventListenerTest.java
308 lines (255 loc) · 12.5 KB
/
OkHttpMetricsEventListenerTest.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Copyright 2017 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.core.instrument.binder.okhttp3;
import com.github.tomakehurst.wiremock.WireMockServer;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.MockClock;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.simple.SimpleConfig;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import ru.lanwen.wiremock.ext.WiremockResolver;
import java.io.IOException;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
/**
* Tests for {@link OkHttpMetricsEventListener}.
*
* @author Bjarte S. Karlsen
* @author Jon Schneider
* @author Johnny Lim
* @author Nurettin Yilmaz
*/
@ExtendWith(WiremockResolver.class)
class OkHttpMetricsEventListenerTest {
private static final String URI_EXAMPLE_VALUE = "uriExample";
private static final Function<Request, String> URI_MAPPER = req -> URI_EXAMPLE_VALUE;
private MeterRegistry registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock());
private OkHttpClient client = new OkHttpClient.Builder()
.eventListener(OkHttpMetricsEventListener.builder(registry, "okhttp.requests")
.tags(Tags.of("foo", "bar"))
.uriMapper(URI_MAPPER)
.build())
.build();
@Test
void timeSuccessful(@WiremockResolver.Wiremock WireMockServer server) throws IOException {
server.stubFor(any(anyUrl()));
Request request = new Request.Builder().url(server.baseUrl()).build();
client.newCall(request).execute().close();
assertThat(registry.get("okhttp.requests")
.tags("foo", "bar", "status", "200", "outcome", "SUCCESS", "uri", URI_EXAMPLE_VALUE, "target.host",
"localhost", "target.port", String.valueOf(server.port()), "target.scheme", "http")
.timer()
.count()).isEqualTo(1L);
}
@Test
void timeNotFound(@WiremockResolver.Wiremock WireMockServer server) throws IOException {
server.stubFor(any(anyUrl()).willReturn(aResponse().withStatus(404)));
Request request = new Request.Builder().url(server.baseUrl()).build();
client.newCall(request).execute().close();
assertThat(registry.get("okhttp.requests")
.tags("foo", "bar", "status", "404", "outcome", "CLIENT_ERROR", "uri", "NOT_FOUND", "target.host",
"localhost", "target.port", String.valueOf(server.port()), "target.scheme", "http")
.timer()
.count()).isEqualTo(1L);
}
@Test
void timeFailureDueToTimeout(@WiremockResolver.Wiremock WireMockServer server) {
Request request = new Request.Builder().url(server.baseUrl()).build();
server.stop();
OkHttpClient client = new OkHttpClient.Builder().connectTimeout(1, TimeUnit.MILLISECONDS)
.eventListener(OkHttpMetricsEventListener.builder(registry, "okhttp.requests")
.tags(Tags.of("foo", "bar"))
.uriMapper(URI_MAPPER)
.build())
.build();
try {
client.newCall(request).execute().close();
fail("Expected IOException.");
}
catch (IOException ignored) {
// expected
}
assertThat(registry.get("okhttp.requests")
.tags("foo", "bar", "uri", URI_EXAMPLE_VALUE, "status", "IO_ERROR", "outcome", "UNKNOWN", "target.host",
"localhost")
.timer()
.count()).isEqualTo(1L);
}
@Test
void uriTagWorksWithUriPatternHeader(@WiremockResolver.Wiremock WireMockServer server) throws IOException {
server.stubFor(any(anyUrl()));
Request request = new Request.Builder().url(server.baseUrl() + "/helloworld.txt")
.header(OkHttpMetricsEventListener.URI_PATTERN, "/")
.build();
client = new OkHttpClient.Builder()
.eventListener(
OkHttpMetricsEventListener.builder(registry, "okhttp.requests").tags(Tags.of("foo", "bar")).build())
.build();
client.newCall(request).execute().close();
assertThat(registry.get("okhttp.requests")
.tags("foo", "bar", "uri", "/", "status", "200", "target.host", "localhost", "target.port",
String.valueOf(server.port()), "target.scheme", "http")
.timer()
.count()).isEqualTo(1L);
}
@Test
void uriTagWorksWithUriMapper(@WiremockResolver.Wiremock WireMockServer server) throws IOException {
server.stubFor(any(anyUrl()));
OkHttpClient client = new OkHttpClient.Builder()
.eventListener(OkHttpMetricsEventListener.builder(registry, "okhttp.requests")
.uriMapper(req -> req.url().encodedPath())
.tags(Tags.of("foo", "bar"))
.build())
.build();
Request request = new Request.Builder().url(server.baseUrl() + "/helloworld.txt").build();
client.newCall(request).execute().close();
assertThat(registry.get("okhttp.requests")
.tags("foo", "bar", "uri", "/helloworld.txt", "status", "200", "target.host", "localhost", "target.port",
String.valueOf(server.port()), "target.scheme", "http")
.timer()
.count()).isEqualTo(1L);
}
@Test
void timeWithStatusCodesMappedAsNotFound(@WiremockResolver.Wiremock WireMockServer server) throws IOException {
server.stubFor(any(anyUrl()).willReturn(aResponse().withStatus(404)));
Request request = new Request.Builder().url(server.baseUrl()).build();
OkHttpClient client = new OkHttpClient.Builder()
.eventListener(OkHttpMetricsEventListener.builder(registry, "okhttp.requests")
.statusCodesMappedAsNotFound(404) // Specifying that 404 should be treated
// as 'NOT_FOUND'
.uriMapper(URI_MAPPER)
.tags(Tags.of("foo", "bar"))
.build())
.build();
client.newCall(request).execute().close();
assertThat(registry.get("okhttp.requests")
.tags("foo", "bar", "status", "404", "outcome", "CLIENT_ERROR", "uri", "NOT_FOUND", "target.host",
"localhost", "target.port", String.valueOf(server.port()), "target.scheme", "http")
.timer()
.count()).isEqualTo(1L);
}
@Test
void contextSpecificTags(@WiremockResolver.Wiremock WireMockServer server) throws IOException {
server.stubFor(any(anyUrl()));
OkHttpClient client = new OkHttpClient.Builder()
.eventListener(OkHttpMetricsEventListener.builder(registry, "okhttp.requests")
.tag((req, res) -> Tag.of("another.uri", req.url().encodedPath()))
.build())
.build();
Request request = new Request.Builder().url(server.baseUrl() + "/helloworld.txt").build();
client.newCall(request).execute().close();
assertThat(
registry.get("okhttp.requests").tags("another.uri", "/helloworld.txt", "status", "200").timer().count())
.isEqualTo(1L);
}
@Test
void cachedResponsesDoNotLeakMemory(@WiremockResolver.Wiremock WireMockServer server, @TempDir Path tempDir)
throws IOException {
OkHttpMetricsEventListener listener = OkHttpMetricsEventListener.builder(registry, "okhttp.requests").build();
OkHttpClient clientWithCache = new OkHttpClient.Builder().eventListener(listener)
.cache(new Cache(tempDir.toFile(), 55555))
.build();
server.stubFor(any(anyUrl()).willReturn(aResponse().withHeader("Cache-Control", "max-age=9600")));
Request request = new Request.Builder().url(server.baseUrl()).build();
clientWithCache.newCall(request).execute().close();
assertThat(listener.callState).isEmpty();
try (Response response = clientWithCache.newCall(request).execute()) {
assertThat(response.cacheResponse()).isNotNull();
}
assertThat(listener.callState).isEmpty();
}
@Test
void requestTagsWithClass(@WiremockResolver.Wiremock WireMockServer server) throws IOException {
Request request = new Request.Builder().url(server.baseUrl() + "/helloworld.txt")
.tag(Tags.class, Tags.of("requestTag1", "tagValue1"))
.build();
testRequestTags(server, request);
}
@Test
void requestTagsWithoutClass(@WiremockResolver.Wiremock WireMockServer server) throws IOException {
Request request = new Request.Builder().url(server.baseUrl() + "/helloworld.txt")
.tag(Tags.of("requestTag1", "tagValue1"))
.build();
testRequestTags(server, request);
}
@Test
void hostTagCanBeDisabled(@WiremockResolver.Wiremock WireMockServer server) throws IOException {
server.stubFor(any(anyUrl()));
OkHttpClient client = new OkHttpClient.Builder()
.eventListener(
OkHttpMetricsEventListener.builder(registry, "okhttp.requests").includeHostTag(false).build())
.build();
Request request = new Request.Builder().url(server.baseUrl()).build();
client.newCall(request).execute().close();
assertThat(registry.get("okhttp.requests")
.tags("status", "200", "target.host", "localhost", "target.port", String.valueOf(server.port()),
"target.scheme", "http")
.timer()
.getId()
.getTags()).doesNotContain(Tag.of("host", "localhost"));
}
@Test
void timeWhenRequestIsNull() {
OkHttpMetricsEventListener listener = OkHttpMetricsEventListener.builder(registry, "okhttp.requests").build();
OkHttpMetricsEventListener.CallState state = new OkHttpMetricsEventListener.CallState(
registry.config().clock().monotonicTime(), null);
listener.time(state);
assertThat(registry.get("okhttp.requests")
.tags("uri", "UNKNOWN", "target.host", "UNKNOWN", "target.port", "UNKNOWN", "target.scheme", "UNKNOWN")
.timer()
.count()).isEqualTo(1L);
}
@Test
void timeWhenRequestIsNullAndRequestTagKeysAreGiven() {
OkHttpMetricsEventListener listener = OkHttpMetricsEventListener.builder(registry, "okhttp.requests")
.requestTagKeys("tag1", "tag2")
.build();
OkHttpMetricsEventListener.CallState state = new OkHttpMetricsEventListener.CallState(
registry.config().clock().monotonicTime(), null);
listener.time(state);
assertThat(registry.get("okhttp.requests")
.tags("uri", "UNKNOWN", "tag1", "UNKNOWN", "tag2", "UNKNOWN")
.timer()
.count()).isEqualTo(1L);
}
private void testRequestTags(@WiremockResolver.Wiremock WireMockServer server, Request request) throws IOException {
server.stubFor(any(anyUrl()));
OkHttpClient client = new OkHttpClient.Builder()
.eventListener(OkHttpMetricsEventListener.builder(registry, "okhttp.requests")
.uriMapper(req -> req.url().encodedPath())
.tags(Tags.of("foo", "bar"))
.build())
.build();
client.newCall(request).execute().close();
assertThat(registry.get("okhttp.requests")
.tags("foo", "bar", "uri", "/helloworld.txt", "status", "200", "requestTag1", "tagValue1", "target.host",
"localhost", "target.port", String.valueOf(server.port()), "target.scheme", "http")
.timer()
.count()).isEqualTo(1L);
}
}