-
Notifications
You must be signed in to change notification settings - Fork 52
/
PromSinkTest.java
310 lines (266 loc) · 14.8 KB
/
PromSinkTest.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
309
310
package io.odpf.firehose.sink.prometheus;
import com.gojek.de.stencil.client.StencilClient;
import cortexpb.Cortex;
import io.odpf.firehose.config.converter.RangeToHashMapConverter;
import io.odpf.firehose.consumer.Message;
import io.odpf.firehose.exception.DeserializerException;
import io.odpf.firehose.exception.NeedToRetry;
import io.odpf.firehose.metrics.Instrumentation;
import io.odpf.firehose.sink.prometheus.request.PromRequest;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicHeader;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.xerial.snappy.Snappy;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static io.odpf.firehose.sink.prometheus.PromSinkConstants.PROMETHEUS_LABEL_FOR_METRIC_NAME;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;
@RunWith(MockitoJUnitRunner.class)
public class PromSinkTest {
@Mock
private Instrumentation instrumentation;
@Mock
private PromRequest request;
@Mock
private HttpClient httpClient;
@Mock
private StencilClient stencilClient;
@Mock
private HttpPost httpPost;
@Mock
private HttpResponse response;
@Mock
private HttpEntity httpEntity;
@Mock
private StatusLine statusLine;
@Mock
private Map<Integer, Boolean> retryStatusCodeRange;
@Mock
private Map<Integer, Boolean> requestLogStatusCodeRanges;
private List<Message> messages;
private Cortex.WriteRequest writeRequest;
private List<HttpEntityEnclosingRequestBase> httpPostList;
@Before
public void setup() {
initMocks(this);
httpPostList = Collections.singletonList(httpPost);
Cortex.Sample sample = Cortex.Sample.newBuilder().setValue(10).setTimestampMs(1000000).build();
Cortex.LabelPair labelPair = Cortex.LabelPair.newBuilder().setName(PROMETHEUS_LABEL_FOR_METRIC_NAME).setValue("test_metric").build();
Cortex.TimeSeries timeSeries = Cortex.TimeSeries.newBuilder()
.addSamples(sample).addLabels(labelPair)
.build();
writeRequest = Cortex.WriteRequest.newBuilder().addTimeseries(timeSeries).build();
messages = new ArrayList<>();
Message message = new Message(null, writeRequest.toByteArray(), "", 0, 1);
messages.add(message);
}
@Test
public void shouldPrepareRequestDuringPreparationAndCallItDuringExecution() throws Exception {
when(httpPost.getURI()).thenReturn(new URI("http://dummy.com"));
when(response.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(200);
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient, retryStatusCodeRange, requestLogStatusCodeRanges);
promSink.prepare(messages);
promSink.execute();
verify(request, times(1)).build(messages);
verify(httpClient, times(1)).execute(httpPost);
}
@Test(expected = NeedToRetry.class)
public void shouldThrowNeedToRetryExceptionWhenResponseCodeIsGivenRange() throws Exception {
when(response.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(500);
when(httpPost.getURI()).thenReturn(new URI("http://dummy.com"));
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient,
new RangeToHashMapConverter().convert(null, "400-505"), requestLogStatusCodeRanges);
promSink.prepare(messages);
promSink.execute();
}
@Test(expected = NeedToRetry.class)
public void shouldThrowNeedToRetryExceptionWhenResponseCodeIsNull() throws Exception {
when(httpPost.getURI()).thenReturn(new URI("http://dummy.com"));
when(httpPost.getAllHeaders()).thenReturn(new Header[]{});
when(httpPost.getEntity()).thenReturn(httpEntity);
when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(Snappy.compress(writeRequest.toByteArray())));
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(null);
PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient, retryStatusCodeRange, requestLogStatusCodeRanges);
promSink.prepare(messages);
promSink.execute();
}
@Test(expected = IOException.class)
public void shouldCatchURISyntaxExceptionAndThrowIOException() throws URISyntaxException, DeserializerException, IOException {
when(request.build(messages)).thenThrow(new URISyntaxException("", ""));
PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient, retryStatusCodeRange, requestLogStatusCodeRanges);
promSink.prepare(messages);
}
@Test
public void shouldCatchIOExceptionInAbstractSinkAndCaptureFailedExecutionTelemetry() throws Exception {
when(httpPost.getURI()).thenReturn(new URI("http://dummy.com"));
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(any(HttpPost.class))).thenThrow(IOException.class);
PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient, retryStatusCodeRange, requestLogStatusCodeRanges);
promSink.pushMessage(messages);
verify(instrumentation, times(1)).captureFailedExecutionTelemetry(any(IOException.class), anyInt());
}
@Test
public void shouldCloseStencilClient() throws IOException {
PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient, retryStatusCodeRange, requestLogStatusCodeRanges);
promSink.close();
verify(stencilClient, times(1)).close();
}
@Test
public void shouldLogConnectionClosing() throws IOException {
PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient, retryStatusCodeRange, requestLogStatusCodeRanges);
promSink.close();
verify(instrumentation, times(1)).logInfo("HTTP connection closing");
}
@Test
public void shouldLogEntireRequestIfInStatusCodeRangeAndCaptureDroppedMessages() throws Exception {
String body = "timeseries {\n labels {\n name: \"__name__\"\n value: \"test_metric\"\n }\n samples {\n value: 10.0\n timestamp_ms: 1000000\n }\n}\n";
when(response.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(500);
when(httpPost.getMethod()).thenReturn("POST");
when(httpPost.getURI()).thenReturn(new URI("http://dummy.com"));
when(httpPost.getAllHeaders()).thenReturn(new Header[]{new BasicHeader("Accept", "text/plain")});
when(httpPost.getEntity()).thenReturn(httpEntity);
when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(Snappy.compress(writeRequest.toByteArray())));
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient,
retryStatusCodeRange, new RangeToHashMapConverter().convert(null, "400-505"));
promSink.prepare(messages);
promSink.execute();
verify(instrumentation, times(1)).logInfo(
"\nRequest Method: POST"
+ "\nRequest Url: http://dummy.com"
+ "\nRequest Headers: [Accept: text/plain]"
+ "\nRequest Body: " + body);
verify(instrumentation, times(1)).logInfo("Message dropped because of status code: 500");
verify(instrumentation, times(1)).captureCountWithTags("firehose_sink_messages_drop_total", 1, "cause= 500");
}
@Test
public void shouldNotLogEntireRequestIfNotInStatusCodeRange() throws Exception {
String body = "[timeseries {\n labels {\n name: \"__name__\"\n value: \"test_metric\"\n }\n samples {\n value: 10.0\n timestamp_ms: 1000000\n }\n}\n]";
when(response.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(500);
when(httpPost.getURI()).thenReturn(new URI("http://dummy.com"));
when(httpPost.getEntity()).thenReturn(httpEntity);
when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(Snappy.compress(writeRequest.toByteArray())));
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient,
retryStatusCodeRange, new RangeToHashMapConverter().convert(null, "400-499"));
promSink.prepare(messages);
promSink.execute();
verify(instrumentation, times(0)).logInfo(
"\nRequest Method: POST"
+ "\nRequest Url: http://dummy.com"
+ "\nRequest Headers: [Accept: text/plain]"
+ "\nRequest Body: " + body);
}
@Test
public void shouldCaptureDroppedMessagesMetricsIfNotInStatusCodeRange() throws Exception {
when(response.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(500);
when(httpPost.getURI()).thenReturn(new URI("http://dummy.com"));
when(httpPost.getEntity()).thenReturn(httpEntity);
when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(Snappy.compress(writeRequest.toByteArray())));
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient,
new RangeToHashMapConverter().convert(null, "400-499"), requestLogStatusCodeRanges);
promSink.prepare(messages);
promSink.execute();
verify(instrumentation, times(1)).logInfo("Message dropped because of status code: 500");
verify(instrumentation, times(1)).captureCountWithTags("firehose_sink_messages_drop_total", 1, "cause= 500");
}
@Test(expected = NeedToRetry.class)
public void shouldNotCaptureDroppedMessagesMetricsIfInStatusCodeRange() throws Exception {
when(response.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(500);
when(httpPost.getURI()).thenReturn(new URI("http://dummy.com"));
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient,
new RangeToHashMapConverter().convert(null, "400-600"), requestLogStatusCodeRanges);
promSink.prepare(messages);
promSink.execute();
verify(instrumentation, times(0)).logInfo("Message dropped because of status code: 500");
}
@Test
public void shouldNotCaptureDroppedMessagesMetricsIfStatusCodeIs200() throws Exception {
when(response.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(200);
when(httpPost.getURI()).thenReturn(new URI("http://dummy.com"));
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient,
retryStatusCodeRange, requestLogStatusCodeRanges);
promSink.prepare(messages);
promSink.execute();
verify(instrumentation, times(0)).logInfo("Message dropped because of status code: 200");
verify(instrumentation, times(0)).captureCountWithTags("firehose_sink_messages_drop_total", 1, "200");
}
@Test
public void shouldNotCaptureDroppedMessagesMetricsIfStatusCodeIs201() throws Exception {
when(response.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(201);
when(httpPost.getURI()).thenReturn(new URI("http://dummy.com"));
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient,
retryStatusCodeRange, requestLogStatusCodeRanges);
promSink.prepare(messages);
promSink.execute();
verify(instrumentation, times(0)).logInfo("Message dropped because of status code: 201");
verify(instrumentation, times(0)).captureCountWithTags("firehose_sink_messages_drop_total", 1, "201");
}
@Test
public void shouldCaptureResponseStatusCount() throws Exception {
when(response.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(200);
URI uri = new URI("http://dummy.com");
when(httpPost.getURI()).thenReturn(uri);
when(request.build(messages)).thenReturn(httpPostList);
when(httpClient.execute(httpPost)).thenReturn(response);
PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient,
retryStatusCodeRange, requestLogStatusCodeRanges);
promSink.prepare(messages);
promSink.execute();
verify(instrumentation, times(1)).captureCountWithTags("firehose_sink_http_response_code_total", 1, "status_code=" + statusLine.getStatusCode(), "url=" + uri.getPath());
}
@Test
public void shouldReadSnappyCompressedContent() throws Exception {
String body = "[timeseries {\n labels {\n name: \"__name__\"\n value: \"test_metric\"\n }\n samples {\n value: 10.0\n timestamp_ms: 1000000\n }\n}\n]";
InputStream inputStream = new ByteArrayInputStream(Snappy.compress(writeRequest.toByteArray()));
PromSink promSink = new PromSink(instrumentation, request, httpClient, stencilClient,
retryStatusCodeRange, requestLogStatusCodeRanges);
List<String> requestBody = promSink.readContent(inputStream);
assertEquals(body, requestBody.toString());
}
}