TracingProxy implements InvocationHandler { public static <T> T instrument(Class<T> iface, T delegate) { return instrument(iface, delegate, Tracing.getTracer()); } private  TracingProxy(Object delegate, Tracer tracer); static T instrument(Class<T> iface, T delegate); @SuppressWarnings("unchecked") static T instrument(Class<T> iface, T delegate, Tracer tracer); @Override Object invoke(Object proxy, Method method, Object[] args);  }
@Test public void testTracingSuccess() { final MockSpan span = new MockSpan(); final Foo delegate = new FooImpl(); final Foo proxy = TracingProxy.instrument(Foo.class, delegate, tracer); when(tracer.spanBuilder("FooImpl.bar")).thenReturn(spanBuilder); when(spanBuilder.startSpan()).thenReturn(span); proxy.bar(); verify(tracer).spanBuilder("FooImpl.bar"); verify(spanBuilder).startSpan(); assertThat(span.ended, is(true)); assertThat(span.status, is(nullValue())); assertThat(span.annotations, is(empty())); }
@Test public void testTracingException() { final MockSpan span = new MockSpan(); final Foo delegate = new ThrowingFooImpl(); final Foo proxy = TracingProxy.instrument(Foo.class, delegate, tracer); when(tracer.spanBuilder("ThrowingFooImpl.bar")).thenReturn(spanBuilder); when(spanBuilder.startSpan()).thenReturn(span); try { proxy.bar(); fail(); } catch (Exception ignore) { } verify(tracer).spanBuilder("ThrowingFooImpl.bar"); verify(spanBuilder).startSpan(); assertThat(span.ended, is(true)); assertThat(span.status, is(UNKNOWN)); assertThat(span.annotations, contains(Annotation.fromDescription("Exception thrown"))); }