Middlewares { public static <T> Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> tracer( Tracer tracer, String service) { return innerHandler -> requestContext -> { final Request request = requestContext.request(); final URI uri = URI.create(request.uri()); final Span span = tracer.spanBuilder(service + '/' + uri.getPath()).startSpan(); span.putAttribute("method", stringAttributeValue(request.method())); span.putAttribute("uri", stringAttributeValue(request.uri())); final CompletionStage<Response<T>> response; try { response = innerHandler.invoke(requestContext); } catch (Exception e) { span.setStatus(UNKNOWN); span.end(); Throwables.throwIfUnchecked(e); throw new RuntimeException(e); } response.whenComplete((r, ex) -> { if (ex != null) { span.setStatus(UNKNOWN); } span.end(); }); return response; }; } private  Middlewares(); static Middleware<SyncHandler<? extends Response<?>>, AsyncHandler<Response<ByteString>>> json(); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> clientValidator(
      Supplier<Set<String>> supplier); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> exceptionAndRequestIdHandler(); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> tracer(
      Tracer tracer, String service); static Middleware<Requested<Authenticated<Response<T>>>, SyncHandler<Response<T>>> authed(
      RequestAuthenticator authenticator); static Middleware<Authenticated<EntityResponseHandler<E, R>>, SyncHandler<Response<ByteString>>> authedEntity(
      Middleware<EntityResponseHandler<E, R>, SyncHandler<Response<ByteString>>> entityMiddleware,
      Middleware<Requested<Authenticated<Response<ByteString>>>, SyncHandler<Response<ByteString>>> authed); static Middleware<Authenticated<EntityResponseHandler<E, R>>, SyncHandler<Response<ByteString>>> authedEntity(
      RequestAuthenticator authenticator,
      Middleware<EntityResponseHandler<E, R>, SyncHandler<Response<ByteString>>> entityMiddleware); static Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<T>>> authenticator(
      RequestAuthenticator authenticator);  }
@Test public void testTracingSync() throws Exception { final RequestContext requestContext = mock(RequestContext.class); final Request request = Request.forUri("/bar", "GET"); final MockSpan span = new MockSpan(); when(requestContext.request()).thenReturn(request); when(tracer.spanBuilder("foo-service when(spanBuilder.startSpan()).thenReturn(span); awaitResponse(Middlewares.tracer(tracer, "foo-service") .apply(rc -> completedFuture(Response.ok())) .invoke(requestContext)); verify(tracer).spanBuilder("foo-service verify(spanBuilder).startSpan(); assertThat(span.attributes.get("method"), is(stringAttributeValue("GET"))); assertThat(span.attributes.get("uri"), is(stringAttributeValue("/bar"))); assertThat(span.ended, is(true)); assertThat(span.status, is(nullValue())); }
@Test public void testTracingSyncError() { final RequestContext requestContext = mock(RequestContext.class); final Request request = Request.forUri("/bar", "GET"); final MockSpan span = new MockSpan(); when(requestContext.request()).thenReturn(request); when(tracer.spanBuilder("foo-service when(spanBuilder.startSpan()).thenReturn(span); try { awaitResponse(Middlewares.tracer(tracer, "foo-service") .apply(rc -> { throw new RuntimeException(); }) .invoke(requestContext)); fail(); } catch (Exception ignore) { } assertThat(span.ended, is(true)); assertThat(span.status, is(io.opencensus.trace.Status.UNKNOWN)); }
@Test public void testTracingAsync() throws ExecutionException, InterruptedException { final RequestContext requestContext = mock(RequestContext.class); final Request request = Request.forUri("/bar", "GET"); final MockSpan span = new MockSpan(); when(requestContext.request()).thenReturn(request); when(tracer.spanBuilder("foo-service when(spanBuilder.startSpan()).thenReturn(span); final CompletableFuture<Response<Object>> handlerFuture = new CompletableFuture<>(); final CompletableFuture<Response<Object>> responseFuture = Middlewares.tracer(tracer, "foo-service") .apply(rc -> handlerFuture) .invoke(requestContext) .toCompletableFuture(); assertThat(span.ended, is(false)); handlerFuture.complete(Response.ok()); responseFuture.get(); assertThat(span.ended, is(true)); assertThat(span.status, is(nullValue())); }
@Test public void testTracingAsyncError() throws InterruptedException { final RequestContext requestContext = mock(RequestContext.class); final Request request = Request.forUri("/bar", "GET"); final MockSpan span = new MockSpan(); when(requestContext.request()).thenReturn(request); when(tracer.spanBuilder("foo-service when(spanBuilder.startSpan()).thenReturn(span); final CompletableFuture<Response<Object>> handlerFuture = new CompletableFuture<>(); final CompletableFuture<Response<Object>> responseFuture = Middlewares.tracer(tracer, "foo-service") .apply(rc -> handlerFuture) .invoke(requestContext) .toCompletableFuture(); assertThat(span.ended, is(false)); handlerFuture.completeExceptionally(new RuntimeException()); try { responseFuture.get(); } catch (ExecutionException ignore) { } assertThat(span.ended, is(true)); assertThat(span.status, is(io.opencensus.trace.Status.UNKNOWN)); }