/** Wraps the input so that it executes with the same context as now. */ public <C> Callable<C> wrap(Callable<C> task) { final TraceContext invocationContext = get(); class CurrentTraceContextCallable implements Callable<C> { @Override public C call() throws Exception { try (Scope scope = maybeScope(invocationContext)) { return task.call(); } } } return new CurrentTraceContextCallable(); }
@Test public void attachesSpanInRunnable() throws Exception { Runnable runnable; try (Scope scope = currentTraceContext.newScope(context)) { runnable = currentTraceContext.wrap(() -> { assertThat(currentTraceContext.get()) .isEqualTo(context); verifyImplicitContext(context); }); runnable.run(); // runs assertion in the same scope } // Set another span between the time the task was made and executed. try (Scope scope2 = currentTraceContext.newScope(context2)) { runnable.run(); // runs assertion verifyImplicitContext(context2); } }
@Override public void request(long n) { try (CurrentTraceContext.Scope scope = this.currentTraceContext .maybeScope(this.traceContext)) { this.s.request(n); } }
@Test public void maybeScope_doesntDuplicateContext() { try (Scope scope = currentTraceContext.newScope(context)) { try (Scope scope2 = currentTraceContext.maybeScope(context)) { assertThat(scope2).isEqualTo(Scope.NOOP); } } }
void noticesDifferentContext(Scope scope) { try (Scope scope2 = currentTraceContext.maybeScope(context2)) { assertThat(scope2).isNotEqualTo(Scope.NOOP); assertThat(currentTraceContext.get()) .isEqualTo(context2); verifyImplicitContext(context2); } finally { scope.close(); } }
@Benchmark public void newScope_clear_default() { try (CurrentTraceContext.Scope ws = base.newScope(null)) { } }
@Override public <T> ListenableFuture<T> submitListenable(Callable<T> task) { return this.delegate .submitListenable(this.tracing.currentTraceContext().wrap(task)); }
ExecutorService tracingExecutor = httpTracing.tracing().currentTraceContext().executorService( new Dispatcher().executorService() );
void canClearScope(Supplier<Scope> noScoper) { try (Scope scope = currentTraceContext.newScope(context)) { try (Scope noScope = noScoper.get()) { assertThat(noScope).isNotEqualTo(Scope.NOOP); assertThat(currentTraceContext.get()) .isNull(); verifyImplicitContext(null); } // old context reverted assertThat(currentTraceContext.get()) .isEqualTo(context); verifyImplicitContext(context); } }
@Benchmark public void newScope_log4j2() { try (CurrentTraceContext.Scope ws = log4j2.newScope(contextWithParent)) { } }
void noticesDifferentSpanId(Scope scope) { TraceContext differentSpanId = context.toBuilder().spanId(context.spanId() + 1L).build(); try (Scope scope2 = currentTraceContext.maybeScope(differentSpanId)) { assertThat(scope2).isNotEqualTo(Scope.NOOP); assertThat(currentTraceContext.get()) .isEqualTo(differentSpanId); verifyImplicitContext(differentSpanId); } finally { scope.close(); } }
@Override public void process(HttpRequest request, HttpContext context) { HttpHost host = HttpClientContext.adapt(context).getTargetHost(); TraceContext parent = (TraceContext) context.getAttribute(TraceContext.class.getName()); Span span; try (Scope scope = currentTraceContext.maybeScope(parent)) { span = handler.nextSpan(request); } HttpRequestWrapper requestWrapper = HttpRequestWrapper.wrap(request, host); parseTargetAddress(requestWrapper, span); handler.handleSend(injector, request, requestWrapper, span); context.setAttribute(Span.class.getName(), span); context.setAttribute(Scope.class.getName(), currentTraceContext.newScope(span.context())); } }
@Override public ListenableFuture<?> submitListenable(Runnable task) { return this.delegate .submitListenable(this.tracing.currentTraceContext().wrap(task)); }
.tracing() .currentTraceContext() .executorService(new Dispatcher().executorService()); Tracer tracer = httpTracing.tracing().tracer(); CurrentTraceContext currentTraceContext = httpTracing.tracing().currentTraceContext();
@Override public TraceContext get() { return delegate.get(); }
@Test public void attachesSpanInCallable() throws Exception { Callable<?> callable; try (Scope scope = currentTraceContext.newScope(context)) { callable = currentTraceContext.wrap(() -> { assertThat(currentTraceContext.get()) .isEqualTo(context); verifyImplicitContext(context); return true; }); callable.call(); // runs assertion in the same scope } // Set another span between the time the task was made and executed. try (Scope scope2 = currentTraceContext.newScope(context2)) { callable.call(); // runs assertion verifyImplicitContext(context2); } }
@Test public void restoresSpanAfterRunnable() throws Exception { TraceContext context0 = TraceContext.newBuilder().traceId(3L).spanId(3L).build(); try (Scope scope0 = currentTraceContext.newScope(context0)) { attachesSpanInRunnable(); assertThat(currentTraceContext.get()) .isEqualTo(context0); verifyImplicitContext(context0); } }
@Benchmark public void newScope_redundant_default() { try (CurrentTraceContext.Scope ws = base.newScope(context)) { } }
@Override public Response intercept(Chain chain) throws IOException { // using maybeScope as when there's no backlog situation the span may already be in scope try (Scope ws = currentTraceContext.maybeScope(previous)) { return chain.proceed(chain.request()); } } }