### Question:
MetricsFilter implements Filter { @Override public Promise<Response, NeverThrowsException> filter(final Context context, final Request request, final Handler next) { metrics.getTotalRequestCount().inc(); metrics.getActiveRequestCount().inc(); final long start = System.nanoTime(); return next.handle(context, request) .thenOnResult(new ResultHandler<Response>() { @Override public void handleResult(final Response result) { long elapsed = TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - start); metrics.getAccumulatedResponseTime().inc(elapsed); metrics.getResponseTime().update(elapsed); metrics.getThroughput().mark(); metrics.getTotalResponseCount().inc(); metrics.getActiveRequestCount().dec(); if (result != null) { if (result.getCause() != null) { metrics.getErrorsResponseCount().inc(); } Status status = result.getStatus(); if (status != null) { switch (status.getFamily()) { case INFORMATIONAL: metrics.getInformativeResponseCount().inc(); break; case SUCCESSFUL: metrics.getSuccessResponseCount().inc(); break; case REDIRECTION: metrics.getRedirectResponseCount().inc(); break; case CLIENT_ERROR: metrics.getClientErrorResponseCount().inc(); break; case SERVER_ERROR: metrics.getServerErrorResponseCount().inc(); break; case UNKNOWN: metrics.getOtherResponseCount().inc(); break; } } } else { metrics.getNullResponseCount().inc(); } } }); } MetricsFilter(final MonitoringMetrics metrics); @Override Promise<Response, NeverThrowsException> filter(final Context context,
                                                          final Request request,
                                                          final Handler next);  }### Answer:
@Test public void shouldUpdateMetrics() throws Exception { MonitoringMetrics metrics = new MonitoringMetrics(); Filter filter = new MetricsFilter(metrics); filter.filter(null, new Request(), next); assertThat(metrics.getTotalRequestCount().getCount()).isEqualTo(1); assertThat(metrics.getActiveRequestCount().getCount()).isEqualTo(0); assertThat(metrics.getTotalResponseCount().getCount()).isEqualTo(1); assertThat(metrics.getErrorsResponseCount().getCount()).isEqualTo(0); assertThat(metrics.getOtherResponseCount().getCount()).isEqualTo(0); assertThat(metrics.getNullResponseCount().getCount()).isEqualTo(0); assertThat(metrics.getInformativeResponseCount().getCount()).isEqualTo(0); assertThat(metrics.getSuccessResponseCount().getCount()).isEqualTo(1); assertThat(metrics.getRedirectResponseCount().getCount()).isEqualTo(0); assertThat(metrics.getClientErrorResponseCount().getCount()).isEqualTo(0); assertThat(metrics.getServerErrorResponseCount().getCount()).isEqualTo(0); assertThat(metrics.getThroughput().getMeanRate()).isNotEqualTo(0); assertThat(metrics.getResponseTime().getCount()).isEqualTo(1); assertThat(metrics.getAccumulatedResponseTime().getCount()).isNotEqualTo(0); }
@Test public void shouldShowOneActiveRequest() throws Exception { final PromiseImpl<Response, NeverThrowsException> promise = Response.newResponsePromiseImpl(); Handler next = new Handler() { @Override public Promise<Response, NeverThrowsException> handle(final Context context, final Request request) { return promise; } }; MonitoringMetrics metrics = new MonitoringMetrics(); Filter filter = new MetricsFilter(metrics); Counter counter = metrics.getActiveRequestCount(); assertThat(counter.getCount()).isEqualTo(0); filter.filter(null, new Request(), next); assertThat(counter.getCount()).isEqualTo(1); promise.handleResult(new Response(Status.OK)); assertThat(counter.getCount()).isEqualTo(0); }
@Test public void shouldCountNullResponse() throws Exception { final MonitoringMetrics metrics = new MonitoringMetrics(); new MetricsFilter(metrics).filter(null, new Request(), new ResponseHandler((Response) null)); assertThat(metrics.getTotalRequestCount().getCount()).isEqualTo(1); assertThat(metrics.getNullResponseCount().getCount()).isEqualTo(1); assertThat(metrics.getSuccessResponseCount().getCount()).isEqualTo(0); assertThat(metrics.getTotalResponseCount().getCount()).isEqualTo(1); }