/** 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(); }
@Override public String toString() { TraceContext currentSpan = currentTraceContext.get(); return "Tracer{" + (currentSpan != null ? ("currentSpan=" + currentSpan + ", ") : "") + (noop.get() ? "noop=true, " : "") + "finishedSpanHandler=" + finishedSpanHandler + "}"; }
@Override public Call newCall(Request request) { TraceContext currentSpan = currentTraceContext.get(); OkHttpClient.Builder b = ok.newBuilder(); if (currentSpan != null) b.interceptors().add(0, new SetParentSpanInScope(currentSpan)); // TODO: This can hide errors at the beginning of call.execute, such as invalid host! return b.build().newCall(request); }
void retainsContext(Scope scope) { try { assertThat(scope).isNotEqualTo(Scope.NOOP); assertThat(currentTraceContext.get()) .isEqualTo(context); verifyImplicitContext(context); } finally { scope.close(); } }
/** * Returns a new child span if there's a {@link #currentSpan()} or a new trace if there isn't. * * <p>Prefer {@link #startScopedSpan(String)} if you are tracing a synchronous function or code * block. */ public Span nextSpan() { TraceContext parent = currentTraceContext.get(); return parent != null ? newChild(parent) : newTrace(); }
/** * Returns the current span in scope or null if there isn't one. * * <p>When entering user code, prefer {@link #currentSpanCustomizer()} as it is a stable type and * will never return null. */ @Nullable public Span currentSpan() { TraceContext currentContext = currentTraceContext.get(); return currentContext != null ? toSpan(currentContext) : null; }
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(); } }
@Test public void restoresSpanAfterCallable() throws Exception { try (Scope scope0 = currentTraceContext.newScope(context)) { attachesSpanInCallable(); assertThat(currentTraceContext.get()) .isEqualTo(context); verifyImplicitContext(context); } }
@Override public <Req> void request(HttpAdapter<Req, ?> adapter, Req req, SpanCustomizer customizer) { customizer.tag("http.url", adapter.url(req)); // just the path is logged by default customizer.tag("context.visible", String.valueOf(currentTraceContext.get() != null)); customizer.tag("request_customizer.is_span", (customizer instanceof brave.Span) + ""); }
@Override public final T apply(T t) throws Exception { TraceContext assembly = currentTraceContext.get(); if (assembly == null) return oldFn.apply(t); // less overhead when there's no current trace return applyActual(oldFn.apply(t), assembly); }
@Override public <T> Future<T> execute(HttpAsyncRequestProducer requestProducer, HttpAsyncResponseConsumer<T> responseConsumer, HttpContext context, FutureCallback<T> callback) { context.setAttribute(TraceContext.class.getName(), currentTraceContext.get()); return delegate.execute( new TracingAsyncRequestProducer(requestProducer, context), new TracingAsyncResponseConsumer<>(responseConsumer, context), context, callback ); }
@Nullable static TraceContext currentTraceContext() { Tracing tracing = Tracing.current(); return tracing != null ? tracing.currentTraceContext().get() : null; }
@Test public void attachesSpanInCallable_canClear() throws Exception { Callable<?> callable = currentTraceContext.wrap(() -> { assertThat(currentTraceContext.get()).isNull(); verifyImplicitContext(null); return true; }); // Set another span between the time the task was made and executed. try (Scope scope2 = currentTraceContext.newScope(context2)) { callable.call(); // runs assertion verifyImplicitContext(context2); } }
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 <Req> void request(HttpAdapter<Req, ?> adapter, Req req, SpanCustomizer customizer) { customizer.name(adapter.method(req).toLowerCase() + " " + adapter.path(req)); customizer.tag("http.url", adapter.url(req)); // just the path is logged by default customizer.tag("context.visible", String.valueOf(currentTraceContext.get() != null)); customizer.tag("request_customizer.is_span", (customizer instanceof brave.Span) + ""); }
@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); } }
@Test public void newScope_canClearScope() { try (SafeCloseable requestContextScope = mockRequestContext.push()) { try (Scope traceContextScope = currentTraceContext.newScope(traceContext)) { try (Scope traceContextScope2 = currentTraceContext.newScope(null)) { assertThat(currentTraceContext.get()).isNull(); } assertThat(currentTraceContext.get()).isEqualTo(traceContext); } } }
@Test public void newScope_appliesWhenCurrentRequestContext() { try (SafeCloseable requestContextScope = mockRequestContext.push()) { try (Scope traceContextScope = currentTraceContext.newScope(traceContext)) { assertThat(traceContextScope).hasToString("InitialRequestScope"); assertThat(currentTraceContext.get()).isEqualTo(traceContext); } } }
@Test public void get_returnsNullWhenCurrentRequestContext_hasNoTraceAttribute() { try (SafeCloseable requestContextScope = mockRequestContext.push()) { assertThat(currentTraceContext.get()).isNull(); } }
@Test public void newScope_doesNothingWhenNoCurrentRequestContext() { try (Scope traceContextScope = currentTraceContext.newScope(traceContext)) { assertThat(traceContextScope).hasToString("IncompleteConfigurationScope"); assertThat(currentTraceContext.get()).isNull(); } }