/
BardLoggingFilter.java
418 lines (369 loc) · 16.4 KB
/
BardLoggingFilter.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Copyright 2016 Yahoo Inc.
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms.
package com.yahoo.bard.webservice.web.filters;
import static javax.ws.rs.core.Response.Status.Family.SUCCESSFUL;
import com.yahoo.bard.webservice.logging.RequestLog;
import com.yahoo.bard.webservice.logging.TimedPhase;
import com.yahoo.bard.webservice.logging.blocks.Epilogue;
import com.yahoo.bard.webservice.logging.blocks.Preface;
import com.yahoo.bard.webservice.util.CacheLastObserver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;
import rx.subjects.ReplaySubject;
import rx.subjects.Subject;
import java.io.BufferedInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Pattern;
import javax.annotation.Priority;
import javax.inject.Singleton;
import javax.validation.constraints.NotNull;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.client.ClientResponseContext;
import javax.ws.rs.client.ClientResponseFilter;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.WriterInterceptor;
import javax.ws.rs.ext.WriterInterceptorContext;
/**
* Logs request/response in this format.
*
* <pre><code>
Request: GET, length=-1
{@literal http://localhost:9998/test/data?metrics=pageViews&dateTime=2014-06-11%2F2014-06-12}
{@literal > user-agent=Jersey/2.9 (HttpUrlConnection 1.7.0_55)}
{@literal > host=localhost:9998}
{@literal > accept=text/html, image/gif, image/jpeg, *; q=.2,}
{@literal > connection=keep-alive}
{@literal < Content-Type=application/json}
Response: 200, OK, 1020.253 ms
length=2
* </code></pre>
*/
@PreMatching
@Singleton
@Priority(1)
public class BardLoggingFilter implements ContainerRequestFilter, ContainerResponseFilter, ClientRequestFilter,
ClientResponseFilter, WriterInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(BardLoggingFilter.class);
private static final String PROPERTY_NANOS = BardLoggingFilter.class.getName() + ".nanos";
private static final String PROPERTY_REQ_LEN = BardLoggingFilter.class.getName() + ".reqlen";
private static final String PROPERTY_OUTPUT_STREAM = BardLoggingFilter.class.getName() + ".ostream";
private static final Pattern VALID_REQUEST_ID = Pattern.compile("[a-zA-Z0-9+/=\\-_]+");
public static final double MILLISECONDS_PER_NANOSECOND = 1000000.0;
public static final String TOTAL_TIMER = "TotalTime";
public static final String CLIENT_TOTAL_TIMER = "ClientRequestTotalTime";
public static final String X_REQUEST_ID_HEADER = "x-request-id";
/**
* Intercept the Container request to add length of request and a start timestamp.
*
* @param request Request to intercept
*
* @throws IOException if there's a problem processing the request
*/
@Override
public void filter(ContainerRequestContext request) throws IOException {
appendRequestId(request.getHeaders().getFirst(X_REQUEST_ID_HEADER));
RequestLog.startTiming(TOTAL_TIMER);
try (TimedPhase timer = RequestLog.startTiming(this)) {
RequestLog.record(new Preface(request));
// sets PROPERTY_REQ_LEN if content-length not defined
lengthOfRequestEntity(request);
// store start time to later calculate elapsed time
request.setProperty(PROPERTY_NANOS, System.nanoTime());
}
}
/**
* Intercept the Container request/response and log.
* <p>
* If output is streamed, also hook the output stream.
*
* @param request Request to intercept
* @param response Response to intercept
*
* @throws IOException if there's a problem processing the request or response
*/
@Override
public void filter(ContainerRequestContext request, ContainerResponseContext response)
throws IOException {
appendRequestId(request.getHeaders().getFirst(X_REQUEST_ID_HEADER));
RequestLog.startTiming(this);
StringBuilder debugMsgBuilder = new StringBuilder();
debugMsgBuilder.append("\tRequest: ").append(request.getMethod());
debugMsgBuilder.append("\tlength=").append(request.getProperty(PROPERTY_REQ_LEN)).append("\t");
debugMsgBuilder.append(renderUri(request.getUriInfo().getRequestUri())).append("\t");
debugMsgBuilder.append("Response: ").append(response.getStatus()).append("\t");
debugMsgBuilder.append(response.getStatusInfo()).append("\t");
Long requestStartTime = (Long) request.getProperty(PROPERTY_NANOS);
if (requestStartTime != null) {
debugMsgBuilder.append((System.nanoTime() - requestStartTime) / MILLISECONDS_PER_NANOSECOND);
}
debugMsgBuilder.append(" ms\t");
if (request.getSecurityContext().getUserPrincipal() != null) {
String user = request.getSecurityContext().getUserPrincipal().getName();
debugMsgBuilder.append("User=").append(user).append("\t");
}
appendStringHeaders(debugMsgBuilder, "> ", request.getHeaders().entrySet());
appendObjectHeaders(debugMsgBuilder, "< ", response.getHeaders().entrySet());
Response.StatusType status = response.getStatusInfo();
String msg = "Successful request";
if (status.getFamily() != SUCCESSFUL) {
msg = response.hasEntity() ? response.getEntity().toString() : "Request without entity failed";
}
CacheLastObserver<Long> responseLengthObserver = new CacheLastObserver<>();
RequestLog.record(new Epilogue(msg, status, responseLengthObserver));
// if response is not yet finalized, we must intercept the output stream
if (response.getLength() == -1 && response.hasEntity()) {
//Pass the length of the response stream to the epilogue as soon as we know what that length is.
Subject<Long, Long> lengthBroadcaster = ReplaySubject.create();
lengthBroadcaster.subscribe(responseLengthObserver);
OutputStream stream = new LengthOfOutputStream(response.getEntityStream(), lengthBroadcaster);
response.setEntityStream(stream);
request.setProperty(PROPERTY_OUTPUT_STREAM, stream);
} else {
debugMsgBuilder.append("length=").append(response.getLength()).append("\t");
Observable.just((long) response.getLength()).subscribe(responseLengthObserver);
LOG.debug(debugMsgBuilder.toString());
RequestLog.stopTiming(this);
RequestLog.stopTiming(TOTAL_TIMER);
RequestLog.log();
}
}
/**
* Intercept Client request and add start timestamp.
*
* @param request Request to intercept
*
* @throws IOException if there's a problem processing the request
*/
@Override
public void filter(ClientRequestContext request) throws IOException {
appendRequestId(request.getStringHeaders().getFirst(X_REQUEST_ID_HEADER));
RequestLog.startTiming(CLIENT_TOTAL_TIMER);
request.setProperty(PROPERTY_NANOS, System.nanoTime());
}
/* Intercept Client request/response and log
* @see ClientResponseFilter#filter(ClientRequestContext, ClientResponseContext)
*/
@Override
public void filter(ClientRequestContext request, ClientResponseContext response) throws IOException {
StringBuilder debugMsgBuilder = new StringBuilder();
debugMsgBuilder.append("ClientRequest: ").append(request.getMethod()).append("\t");
debugMsgBuilder.append(request.getUri().toASCIIString()).append("\t");
appendObjectHeaders(debugMsgBuilder, "> ", request.getHeaders().entrySet());
appendStringHeaders(debugMsgBuilder, "< ", response.getHeaders().entrySet());
debugMsgBuilder.append(response.getStatusInfo()).append(", length=").append(response.getLength()).append(" ");
Long requestStartTime = (Long) request.getProperty(PROPERTY_NANOS);
if (requestStartTime != null) {
debugMsgBuilder
.append((System.nanoTime() - requestStartTime) / MILLISECONDS_PER_NANOSECOND)
.append(" ms\t");
}
LOG.debug(debugMsgBuilder.toString());
RequestLog.stopTiming(CLIENT_TOTAL_TIMER);
}
/**
* Add a request id to pass to druid. Only added if x-request-id follows the format specified at:
* https://devcenter.heroku.com/articles/http-request-id
*
* @param requestId Request id to add as queryId prefix to druid
*/
private void appendRequestId(String requestId) {
if (isNotValidRequestId(requestId)) {
return; // Ignore according to https://devcenter.heroku.com/articles/http-request-id
}
RequestLog.addIdPrefix(requestId);
}
/**
* Validate whether or not a string is acceptable as an x-request-id header argument.
*
* @param requestId Request id to validate
* @return True if the request id is not valid, false otherwise
*/
private boolean isNotValidRequestId(String requestId) {
return requestId == null || requestId.isEmpty() || requestId.length() > 200
|| !VALID_REQUEST_ID.matcher(requestId).matches();
}
/**
* Render the URI as a string.
*
* @param uri The URI to render
*
* @return A String representation of the URI
*/
protected String renderUri(URI uri) {
return uri.toASCIIString();
}
/**
* Add the entries (assumed to be headers) to the given StringBuilder, prefixing their keys.
*
* @param builder StringBuilder to append to
* @param prefix Prefix to put in front of the keys
* @param entries Entries to add
*/
private void appendObjectHeaders(StringBuilder builder, String prefix, Set<Entry<String, List<Object>>> entries) {
for (Entry<String, List<Object>> e : entries) {
appendHeader(builder, prefix, e.getKey(), e.getValue());
}
}
/**
* Add the entries (assumed to be headers) to the given StringBuilder, prefixing their keys.
*
* @param builder StringBuilder to append to
* @param prefix Prefix to put in front of the keys
* @param entries Entries to add
*/
private void appendStringHeaders(StringBuilder builder, String prefix, Set<Entry<String, List<String>>> entries) {
for (Entry<String, List<String>> e : entries) {
appendHeader(builder, prefix, e.getKey(), e.getValue());
}
}
/**
* Append the given values to the StringBuilder in a key-value way, prefixing the key and tab-separating the values.
*
* @param builder StringBuilder to build upon
* @param prefix Prefix to put in front of the key
* @param name Name of the key
* @param values Values to tab-separate
*/
private void appendHeader(StringBuilder builder, String prefix, String name, List<?> values) {
String value = values.size() == 1 ? values.get(0).toString() : values.toString();
builder.append(prefix).append(name).append("=").append(value).append("\t");
}
/**
* This allows us to compute the length of the output stream when stream is closed.
*
* @param writerInterceptorContext Interceptor to use for intercepting the entity being written
*
* @throws IOException if there's a problem processing the request
*/
@Override
public void aroundWriteTo(WriterInterceptorContext writerInterceptorContext) throws IOException {
LengthOfOutputStream stream = (LengthOfOutputStream) writerInterceptorContext
.getProperty(PROPERTY_OUTPUT_STREAM);
try {
writerInterceptorContext.proceed();
if (stream != null) {
emitSuccess(stream);
}
} catch (EOFException e) {
if (stream != null) {
emitError(stream, e);
}
LOG.warn("Connection to client closed prematurely.", e);
} catch (Error | RuntimeException | IOException e) {
if (stream != null) {
emitError(stream, e);
}
// catch all and rethrow after logging
LOG.error("Error encountered while streaming response back to the client.", e);
throw e;
} finally {
try {
RequestLog.stopTiming(this);
RequestLog.stopTiming(TOTAL_TIMER);
RequestLog.log();
} catch (Exception e) {
LOG.error("Error finalizing the BardLoggingFilter output stream wrapper.", e);
}
}
}
/**
* Handle publishing the length and completed to the Subject.
*
* @param stream Stream to get the length from
*/
private void emitSuccess(@NotNull LengthOfOutputStream stream) {
Subject<Long, Long> lengthBroadcaster = stream.getLengthBroadcaster();
lengthBroadcaster.onNext(stream.getResponseLength());
lengthBroadcaster.onCompleted();
}
/**
* Handle publishing the length and an error to the Subject.
*
* @param stream Stream to get the length from
* @param t Error that was encountered (to be published)
*/
private void emitError(LengthOfOutputStream stream, Throwable t) {
Subject<Long, Long> lengthBroadcaster = stream.getLengthBroadcaster();
lengthBroadcaster.onNext(stream.getResponseLength());
lengthBroadcaster.onError(t);
}
// maximum input buffer we can hold before lengthOfInputStream returns -1
static final int MAX_ENTITY_SIZE = 65535;
/**
* If content-length not defined, finds length of request stream and sets PROPERTY_REQ_LEN.
*
* @param request the request
* @throws IOException if reading the entity stream from the request throws an IOException
*/
private void lengthOfRequestEntity(ContainerRequestContext request) throws IOException {
// Scan entity stream if unknown length
if (request.getLength() == -1 && request.hasEntity()) {
InputStream istream = request.getEntityStream();
if (!istream.markSupported()) {
istream = new BufferedInputStream(istream);
}
istream.mark(MAX_ENTITY_SIZE + 1);
byte[] entity = new byte[MAX_ENTITY_SIZE + 1];
int entitySize = istream.read(entity);
if (entitySize > MAX_ENTITY_SIZE) {
request.setProperty(PROPERTY_REQ_LEN, -1);
} else {
request.setProperty(PROPERTY_REQ_LEN, entitySize);
}
istream.reset();
request.setEntityStream(istream);
} else {
request.setProperty(PROPERTY_REQ_LEN, request.getLength());
}
}
/**
* Contains a Subject that fires a single message with the number of bytes the output stream has written at the
* point where {@code fireLengthMessage} is called.
*/
static private class LengthOfOutputStream extends OutputStream {
private long length;
private final OutputStream outputStream;
private final Subject<Long, Long> lengthBroadcaster;
/**
* Constructor.
*
* @param outputStream OutputStream to get the length of
* @param lengthBroadcaster Subject to publish events to (final length and completion status or error)
*/
LengthOfOutputStream(OutputStream outputStream, Subject<Long, Long> lengthBroadcaster) {
this.outputStream = outputStream;
this.lengthBroadcaster = lengthBroadcaster;
this.length = 0;
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
outputStream.write(b, off, len);
length += len;
}
@Override
public void write(int i) throws IOException {
outputStream.write(i);
length++;
}
public Subject<Long, Long> getLengthBroadcaster() {
return lengthBroadcaster;
}
public long getResponseLength() {
return length;
}
}
}