/** * Creates a new instance. * * @param wrappedCallable The wrapped Callable. * @param serverSpanThreadBinder ServerSpan thread binder. */ public BraveCallable(final Callable<T> wrappedCallable, final ServerSpanThreadBinder serverSpanThreadBinder) { this.wrappedCallable = wrappedCallable; this.serverTracer = serverSpanThreadBinder; this.currentServerSpan = serverSpanThreadBinder.getCurrentServerSpan(); }
/** * Creates a new instance. * * @param runnable The wrapped Runnable. * @param serverTracer ServerTracer. * @param currentServerSpan Current ServerSpan. This ServerSpan will also get binded to the wrapped thread. */ public BraveRunnable(final Runnable runnable, final ServerSpanThreadBinder serverTracer) { wrappedRunnable = runnable; serverSpanThreadBinder = serverTracer; currentServerSpan = serverTracer.getCurrentServerSpan(); }
boolean shouldTrace() { // Only join traces, don't start them. return startTraces || (serverSpanThreadBinder != null && serverSpanThreadBinder.getCurrentServerSpan() != null && serverSpanThreadBinder.getCurrentServerSpan().getSpan() != null); }
@Override public void afterConcurrentHandlingStarted(final HttpServletRequest request, final HttpServletResponse response, final Object handler) { request.setAttribute(HTTP_SERVER_SPAN_ATTRIBUTE, serverThreadBinder.getCurrentServerSpan()); serverTracer.clearCurrentSpan(); }
Boolean sampled() { ServerSpan result = getCurrentServerSpan(); return result != null ? result.getSample(): null; } }
/** * Creates a new instance. * * @param wrappedCallable The wrapped Callable. * @param serverSpanThreadBinder ServerSpan thread binder. */ public static <T> BraveCallable<T> create(Callable<T> wrappedCallable, ServerSpanThreadBinder serverSpanThreadBinder) { return new AutoValue_BraveCallable<T>(wrappedCallable, serverSpanThreadBinder, serverSpanThreadBinder.getCurrentServerSpan()); }
/** * Creates a new instance. * * @param runnable The wrapped Callable. * @param serverSpanThreadBinder ServerSpan thread binder. */ public static BraveRunnable create(Runnable runnable, ServerSpanThreadBinder serverSpanThreadBinder) { return new AutoValue_BraveRunnable(runnable, serverSpanThreadBinder, serverSpanThreadBinder.getCurrentServerSpan()); }
@Override public void afterConcurrentHandlingStarted(final HttpServletRequest request, final HttpServletResponse response, final Object handler) { request.setAttribute(HTTP_SERVER_SPAN_ATTRIBUTE, serverThreadBinder.getCurrentServerSpan()); serverThreadBinder.setCurrentSpan(null); }
@Override public void afterConcurrentHandlingStarted(final HttpServletRequest request, final HttpServletResponse response, final Object handler) { request.setAttribute(HTTP_SERVER_SPAN_ATTRIBUTE, serverThreadBinder.getCurrentServerSpan()); serverThreadBinder.setCurrentSpan(ServerSpan.EMPTY); }
@Override Span get() { ServerSpan result = getCurrentServerSpan(); return result != null ? result.getSpan(): null; }
static BraveRunnable wrap( // hidden for package-scoped use Runnable runnable, LocalSpanThreadBinder localSpanThreadBinder, ServerSpanThreadBinder serverSpanThreadBinder ) { return new AutoValue_BraveRunnable( runnable, localSpanThreadBinder, localSpanThreadBinder.getCurrentLocalSpan(), serverSpanThreadBinder, serverSpanThreadBinder.getCurrentServerSpan() ); }
/** * @deprecated use {@link #wrap(Callable, Brave)} because this constructor loses thread * state for local span parents. */ @Deprecated public static <T> BraveCallable<T> create(Callable<T> wrappedCallable, ServerSpanThreadBinder serverSpanThreadBinder) { checkNotNull(serverSpanThreadBinder, "serverSpanThreadBinder"); // auto-value will check the others. return new AutoValue_BraveCallable( wrappedCallable, null, null, serverSpanThreadBinder, serverSpanThreadBinder.getCurrentServerSpan() ); }
/** * @deprecated use {@link #wrap(Runnable, Brave)} because this constructor loses thread * state for local span parents. */ @Deprecated public static BraveRunnable create(Runnable runnable, ServerSpanThreadBinder serverSpanThreadBinder) { checkNotNull(serverSpanThreadBinder, "serverSpanThreadBinder"); // auto-value will check the others. return new AutoValue_BraveRunnable( runnable, null, null, serverSpanThreadBinder, serverSpanThreadBinder.getCurrentServerSpan() ); }
static <T> BraveCallable<T> wrap( // hidden for package-scoped use Callable<T> callable, LocalSpanThreadBinder localSpanThreadBinder, ServerSpanThreadBinder serverSpanThreadBinder ) { return new AutoValue_BraveCallable( callable, localSpanThreadBinder, localSpanThreadBinder.getCurrentLocalSpan(), serverSpanThreadBinder, serverSpanThreadBinder.getCurrentServerSpan() ); }
/** * Returns a span representing the sampled status of the current server span or null if there's no * span attached to the current thread. */ @Nullable public static brave.Span getServerSpan(Tracer tracer, ServerSpanThreadBinder threadBinder) { if (tracer == null) throw new NullPointerException("tracer == null"); if (threadBinder == null) throw new NullPointerException("threadBinder == null"); ServerSpan result = threadBinder.getCurrentServerSpan(); if (result == null || result.equals(ServerSpan.EMPTY)) return null; if (result.getSpan() != null) return toSpan(tracer, result.getSpan()); assert result.getSample() != null && !result.getSample() : "unexpected sample state: " + result; return tracer.withSampler(Sampler.NEVER_SAMPLE).newTrace(); }
/** * @since 3.17 */ public static BraveRunnable wrap(Runnable runnable, Brave brave) { checkNotNull(brave, "brave"); // auto-value will check the others. return new AutoValue_BraveRunnable( runnable, brave.localSpanThreadBinder(), brave.localSpanThreadBinder().getCurrentLocalSpan(), brave.serverSpanThreadBinder(), brave.serverSpanThreadBinder().getCurrentServerSpan() ); }
/** * @since 3.17 */ public static <T> BraveCallable<T> wrap(Callable<T> callable, Brave brave) { checkNotNull(brave, "brave"); // auto-value will check the others. return new AutoValue_BraveCallable( callable, brave.localSpanThreadBinder(), brave.localSpanThreadBinder().getCurrentLocalSpan(), brave.serverSpanThreadBinder(), brave.serverSpanThreadBinder().getCurrentServerSpan() ); }
/** Creates a TaskRequestWrapper from passed in params and sets the current server span on it*/ private <S> TaskRequestWrapper createRequestFromParams(String commandName, S data, Map<String, Object> params) { TaskRequestWrapper taskRequestWrapper = new TaskRequestWrapper(); taskRequestWrapper.setCommandName(commandName); taskRequestWrapper.setData(data); taskRequestWrapper.setParams(params); ServerSpan serverSpan = Brave.getServerSpanThreadBinder().getCurrentServerSpan(); if (serverSpan.getSpan() != null) { // set the request context and the current server span on the received request only if a span exists RequestContext serverRequestContext = new RequestContext(); serverRequestContext.setCurrentServerSpan(serverSpan); taskRequestWrapper.setRequestContext(Optional.of(serverRequestContext)); } return taskRequestWrapper; }
@Test public void usesExistingTraceId_server() throws Exception { server.enqueue(new MockResponse()); brave.serverTracer().setStateUnknown("test"); ServerSpan parent = brave.serverSpanThreadBinder().getCurrentServerSpan(); try { get(client, "/foo"); } finally { brave.serverTracer().clearCurrentSpan(); } RecordedRequest request = server.takeRequest(); assertThat(request.getHeader("x-b3-traceId")) .endsWith(IdConversion.convertToString(parent.getSpan().getTrace_id())); assertThat(request.getHeader("x-b3-parentspanid")) .endsWith(IdConversion.convertToString(parent.getSpan().getId())); }
/** * {@inheritDoc} */ @Override public T call() throws Exception { if (localSpanThreadBinder() == null) { // old behavior serverSpanThreadBinder().setCurrentSpan(currentServerSpan()); return wrappedCallable().call(); } ServerSpan previousServerSpan = serverSpanThreadBinder().getCurrentServerSpan(); Span previousLocalSpan = localSpanThreadBinder().getCurrentLocalSpan(); try { serverSpanThreadBinder().setCurrentSpan(currentServerSpan()); localSpanThreadBinder().setCurrentSpan(currentLocalSpan()); return wrappedCallable().call(); } finally { serverSpanThreadBinder().setCurrentSpan(previousServerSpan); localSpanThreadBinder().setCurrentSpan(previousLocalSpan); } }