TracedUnaryCallable extends UnaryCallable<RequestT, ResponseT> { @Override public ApiFuture<ResponseT> futureCall(RequestT request, ApiCallContext context) { ApiTracer tracer = tracerFactory.newTracer(context.getTracer(), spanName, OperationType.Unary); TraceFinisher<ResponseT> finisher = new TraceFinisher<>(tracer); try { context = context.withTracer(tracer); ApiFuture<ResponseT> future = innerCallable.futureCall(request, context); ApiFutures.addCallback(future, finisher, MoreExecutors.directExecutor()); return future; } catch (RuntimeException e) { finisher.onFailure(e); throw e; } } TracedUnaryCallable(
      UnaryCallable<RequestT, ResponseT> innerCallable,
      ApiTracerFactory tracerFactory,
      SpanName spanName); @Override ApiFuture<ResponseT> futureCall(RequestT request, ApiCallContext context);  }
@Test public void testTracerCreated() { tracedUnaryCallable.futureCall("test", callContext); verify(tracerFactory, times(1)).newTracer(parentTracer, SPAN_NAME, OperationType.Unary); }
@Test public void testOperationFinish() { innerResult.set("successful result"); tracedUnaryCallable.futureCall("test", callContext); verify(tracer, times(1)).operationSucceeded(); }
@Test public void testOperationCancelled() { innerResult.cancel(true); tracedUnaryCallable.futureCall("test", callContext); verify(tracer, times(1)).operationCancelled(); }
@Test public void testOperationFailed() { RuntimeException fakeError = new RuntimeException("fake error"); innerResult.setException(fakeError); tracedUnaryCallable.futureCall("test", callContext); verify(tracer, times(1)).operationFailed(fakeError); }
@Test public void testSyncError() { RuntimeException fakeError = new RuntimeException("fake error"); @SuppressWarnings("unchecked") UnaryCallable<String, String>[] innerCallableWrapper = new UnaryCallable[] {innerCallable}; reset(innerCallableWrapper); when(innerCallable.futureCall(eq("failing test"), any(ApiCallContext.class))) .thenThrow(fakeError); try { tracedUnaryCallable.futureCall("failing test", callContext); } catch (RuntimeException e) { } verify(tracer, times(1)).operationFailed(fakeError); }