|
2 | 2 |
|
3 | 3 | import com.google.common.collect.ImmutableList; |
4 | 4 | import com.google.common.io.BaseEncoding; |
| 5 | +import com.timgroup.statsd.NonBlockingStatsDClient; |
| 6 | +import com.timgroup.statsd.StatsDClient; |
5 | 7 | import io.netty.buffer.ByteBuf; |
6 | 8 | import io.netty.channel.ChannelHandlerContext; |
7 | 9 | import io.netty.handler.codec.http.HttpContent; |
|
44 | 46 | import java.util.concurrent.TimeUnit; |
45 | 47 | import java.util.concurrent.atomic.AtomicInteger; |
46 | 48 |
|
| 49 | +import static net.lightbody.bmp.filters.StatsDMetricsFilter.*; |
| 50 | +import static net.lightbody.bmp.filters.StatsDMetricsFilter.getStatsDPort; |
| 51 | + |
47 | 52 | public class HarCaptureFilter extends HttpsAwareFiltersAdapter { |
48 | 53 | private static final Logger log = LoggerFactory.getLogger(HarCaptureFilter.class); |
| 54 | + private static final StatsDClient statsDClient = new NonBlockingStatsDClient("automated_tests", getStatsDHost(), getStatsDPort()); |
49 | 55 |
|
50 | 56 | /** |
51 | 57 | * The currently active HAR at the time the current request is received. |
@@ -123,16 +129,16 @@ public class HarCaptureFilter extends HttpsAwareFiltersAdapter { |
123 | 129 | * <p/> |
124 | 130 | * Regardless of the CaptureTypes specified in <code>dataToCapture</code>, the HarCaptureFilter will always capture: |
125 | 131 | * <ul> |
126 | | - * <li>Request and response sizes</li> |
127 | | - * <li>HTTP request and status lines</li> |
128 | | - * <li>Page timing information</li> |
| 132 | + * <li>Request and response sizes</li> |
| 133 | + * <li>HTTP request and status lines</li> |
| 134 | + * <li>Page timing information</li> |
129 | 135 | * </ul> |
130 | 136 | * |
131 | 137 | * @param originalRequest the original HttpRequest from the HttpFiltersSource factory |
132 | | - * @param har a reference to the ProxyServer's current HAR file at the time this request is received (can be null if HAR capture is not required) |
133 | | - * @param currentPageRef the ProxyServer's currentPageRef at the time this request is received from the client |
134 | | - * @param dataToCapture the data types to capture for this request. null or empty set indicates only basic information will be |
135 | | - * captured (see {@link net.lightbody.bmp.proxy.CaptureType} for information on data collected for each CaptureType) |
| 138 | + * @param har a reference to the ProxyServer's current HAR file at the time this request is received (can be null if HAR capture is not required) |
| 139 | + * @param currentPageRef the ProxyServer's currentPageRef at the time this request is received from the client |
| 140 | + * @param dataToCapture the data types to capture for this request. null or empty set indicates only basic information will be |
| 141 | + * captured (see {@link net.lightbody.bmp.proxy.CaptureType} for information on data collected for each CaptureType) |
136 | 142 | */ |
137 | 143 | public HarCaptureFilter(HttpRequest originalRequest, ChannelHandlerContext ctx, Har har, String currentPageRef, Set<CaptureType> dataToCapture) { |
138 | 144 | super(originalRequest, ctx); |
@@ -199,6 +205,8 @@ public HttpResponse clientToProxyRequest(HttpObject httpObject) { |
199 | 205 | HarResponse defaultHarResponse = HarCaptureUtil.createHarResponseForFailure(); |
200 | 206 | defaultHarResponse.setError(HarCaptureUtil.getNoResponseReceivedErrorMessage()); |
201 | 207 | harEntry.setResponse(defaultHarResponse); |
| 208 | + statsDClient.increment(getProxyPrefix().concat(prepareMetric(harEntry.getRequest().getUrl())) |
| 209 | + .concat("." + harEntry.getResponse().getStatus()).concat(".client_proxy_connection_fail")); |
202 | 210 |
|
203 | 211 | captureQueryParameters(httpRequest); |
204 | 212 | // not capturing user agent: in many cases, it doesn't make sense to capture at the HarLog level, since the proxy could be |
@@ -278,6 +286,8 @@ public void serverToProxyResponseTimedOut() { |
278 | 286 | // replace any existing HarResponse that was created if the server sent a partial response |
279 | 287 | HarResponse response = HarCaptureUtil.createHarResponseForFailure(); |
280 | 288 | harEntry.setResponse(response); |
| 289 | + statsDClient.increment(getProxyPrefix().concat(prepareMetric(harEntry.getRequest().getUrl())) |
| 290 | + .concat("." + harEntry.getResponse().getStatus()).concat(".response_timeout")); |
281 | 291 |
|
282 | 292 | response.setError(HarCaptureUtil.getResponseTimedOutErrorMessage()); |
283 | 293 |
|
@@ -408,7 +418,7 @@ protected void captureRequestContent(HttpRequest httpRequest, byte[] fullMessage |
408 | 418 |
|
409 | 419 | Charset charset; |
410 | 420 | try { |
411 | | - charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentType); |
| 421 | + charset = BrowserMobHttpUtil.readCharsetInContentTypeHeader(contentType); |
412 | 422 | } catch (UnsupportedCharsetException e) { |
413 | 423 | log.warn("Found unsupported character set in Content-Type header '{}' in HTTP request to {}. Content will not be captured in HAR.", contentType, httpRequest.getUri(), e); |
414 | 424 | return; |
@@ -651,6 +661,8 @@ public InetSocketAddress proxyToServerResolutionStarted(String resolvingServerHo |
651 | 661 | public void proxyToServerResolutionFailed(String hostAndPort) { |
652 | 662 | HarResponse response = HarCaptureUtil.createHarResponseForFailure(); |
653 | 663 | harEntry.setResponse(response); |
| 664 | + statsDClient.increment(getProxyPrefix().concat(prepareMetric(harEntry.getRequest().getUrl())) |
| 665 | + .concat("." + harEntry.getResponse().getStatus()).concat(".server_resolution_fail")); |
654 | 666 |
|
655 | 667 | response.setError(HarCaptureUtil.getResolutionFailedErrorMessage(hostAndPort)); |
656 | 668 |
|
@@ -692,6 +704,8 @@ public void proxyToServerConnectionStarted() { |
692 | 704 | @Override |
693 | 705 | public void proxyToServerConnectionFailed() { |
694 | 706 | HarResponse response = HarCaptureUtil.createHarResponseForFailure(); |
| 707 | + statsDClient.increment(getProxyPrefix().concat(prepareMetric(harEntry.getRequest().getUrl())) |
| 708 | + .concat("." + harEntry.getResponse().getStatus()).concat(".server_connection_fail")); |
695 | 709 | harEntry.setResponse(response); |
696 | 710 |
|
697 | 711 | response.setError(HarCaptureUtil.getConnectionFailedErrorMessage()); |
|
0 commit comments