InstrumentedInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { HttpUrl url = chain.request().url(); String hostname = url.host(); int port = url.port(); Stopwatch stopwatch = Stopwatch.createStarted(); Response response; try { response = chain.proceed(chain.request()); } catch (IOException e) { if (!chain.call().isCanceled()) { hostEventsSink.recordIoException(serviceName, hostname, port); ioExceptionMeter.mark(); } throw e; } long micros = stopwatch.elapsed(TimeUnit.MICROSECONDS); hostEventsSink.record(serviceName, hostname, port, response.code(), micros); responseTimer.update(micros, TimeUnit.MICROSECONDS); return response; } InstrumentedInterceptor(ClientMetrics clientMetrics, HostEventsSink hostEventsSink, String serviceName); @Override Response intercept(Chain chain);  }
@Test public void testResponseFamilyMetrics() throws IOException { successfulRequest(REQUEST_A); interceptor.intercept(chain); HostMetrics hostA = hostMetrics("hosta", PORT); assertThat(hostA.get2xx().getCount()).isEqualTo(1); successfulRequest(REQUEST_B); interceptor.intercept(chain); HostMetrics hostB = hostMetrics("hostb", PORT); assertThat(hostB.get2xx().getCount()).isEqualTo(1); }
@Test public void testResponseMetricRegistered() throws IOException { MetricName name = MetricName.builder() .safeName("client.response") .putSafeTags("service-name", "client") .putSafeTags("libraryName", "conjure-java-runtime") .putSafeTags("libraryVersion", "unknown") .build(); Timer timer = registry.timer(name); assertThat(timer.getCount()).isZero(); successfulRequest(REQUEST_A); interceptor.intercept(chain); assertThat(timer.getCount()).isEqualTo(1); }
@Test public void testIoExceptionRecorded() throws IOException { failedRequest(REQUEST_A, false); assertThat(hostMetrics.getMetrics()).isEmpty(); assertThatExceptionOfType(IOException.class).isThrownBy(() -> interceptor.intercept(chain)); HostMetrics metrics = Iterables.getOnlyElement(hostMetrics.getMetrics()); assertThat(metrics.getIoExceptions().getCount()).isEqualTo(1); }
@Test public void testIoExceptionNotRecordedWhenCanceled() throws IOException { failedRequest(REQUEST_A, true); assertThat(hostMetrics.getMetrics()).isEmpty(); assertThatExceptionOfType(IOException.class).isThrownBy(() -> interceptor.intercept(chain)); assertThat(hostMetrics.getMetrics()).isEmpty(); }