/** * 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(); }
@Override public void afterConcurrentHandlingStarted(final HttpServletRequest request, final HttpServletResponse response, final Object handler) { request.setAttribute(HTTP_SERVER_SPAN_ATTRIBUTE, serverThreadBinder.getCurrentServerSpan()); serverThreadBinder.setCurrentSpan(null); }
public final Builder state(ServerSpanState state) { this.currentSpan = new ServerSpanThreadBinder(state); this.localEndpoint = state.endpoint(); return this; }
/** * Sets the current Trace/Span state. Using this method indicates that a parent request has decided that we should not * trace the current request. * * @see ServerTracer#setStateCurrentTrace(SpanId, String) * @see ServerTracer#setStateUnknown(String) */ public void setStateNoTracing() { currentSpan().setCurrentSpan(ServerSpan.NOT_SAMPLED); }
@Override public void setClientAddress(Brave brave, Endpoint ca) { Span span = brave.serverSpanThreadBinder().get(); if (span == null) return; brave.serverTracer.recorder().address(span, "ca", ca); } };
/** * Clears current span. */ public void clearCurrentSpan() { currentSpan().setCurrentSpan(ServerSpan.EMPTY); }
@Nullable SpanId maybeParent() { Span parentSpan = currentLocalSpan().get(); if (parentSpan == null) { Span serverSpan = currentServerSpan().get(); if (serverSpan != null) { parentSpan = serverSpan; } } if (parentSpan == null) return null; return Brave.context(parentSpan); }
/** * 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(); }
@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); }
/** * {@inheritDoc} */ @Override public void run() { serverSpanThreadBinder.setCurrentSpan(currentServerSpan); final long start = System.currentTimeMillis(); try { wrappedRunnable.run(); } finally { final long duration = System.currentTimeMillis() - start; currentServerSpan.incThreadDuration(duration); } }
public final Builder state(ServerClientAndLocalSpanState state) { this.currentLocalSpan = new LocalSpanThreadBinder(state); this.currentServerSpan = new ServerSpanThreadBinder(state); this.currentSpan = new ClientSpanThreadBinder(state); this.localEndpoint = state.endpoint(); return this; }
/** * Request the span that should be considered the new span's parent. * * If {@link #allowNestedLocalSpans()} is enabled, the new span's parent * will be the current local span (if one exists). * * If nested local spans is not enabled or there is no current local span, * the new span's parent will be the current server span (if one exists). * * @return span that should be the new span's parent, or null if one does not exist. */ @Nullable SpanId maybeParent() { Span parentSpan = null; if (allowNestedLocalSpans()) { parentSpan = currentSpan().get(); } if (parentSpan == null) { Span currentServerSpan = currentServerSpan().get(); if (currentServerSpan != null) { parentSpan = currentServerSpan; } } if (parentSpan == null) return null; return Brave.context(parentSpan); }
boolean shouldTrace() { // Only join traces, don't start them. return startTraces || (serverSpanThreadBinder != null && serverSpanThreadBinder.getCurrentServerSpan() != null && serverSpanThreadBinder.getCurrentServerSpan().getSpan() != null); }
/** * {@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); } }
/** * {@inheritDoc} */ @Override public T call() throws Exception { serverTracer.setCurrentSpan(currentServerSpan); final long start = System.currentTimeMillis(); try { return wrappedCallable.call(); } finally { final long duration = System.currentTimeMillis() - start; currentServerSpan.incThreadDuration(duration); } }
private Brave(Builder builder) { clock = builder.clock; serverSpanThreadBinder = new ServerSpanThreadBinder(builder.state); clientSpanThreadBinder = new ClientSpanThreadBinder(builder.state); localSpanThreadBinder = new LocalSpanThreadBinder(builder.state);
Boolean sampled() { ServerSpan result = getCurrentServerSpan(); return result != null ? result.getSample(): null; } }
/** * {@inheritDoc} */ @Override public void run() { if (localSpanThreadBinder() == null) { // old behavior serverSpanThreadBinder().setCurrentSpan(currentServerSpan()); wrappedRunnable().run(); return; } ServerSpan previousServerSpan = serverSpanThreadBinder().getCurrentServerSpan(); Span previousLocalSpan = localSpanThreadBinder().getCurrentLocalSpan(); try { serverSpanThreadBinder().setCurrentSpan(currentServerSpan()); localSpanThreadBinder().setCurrentSpan(currentLocalSpan()); wrappedRunnable().run(); } finally { serverSpanThreadBinder().setCurrentSpan(previousServerSpan); localSpanThreadBinder().setCurrentSpan(previousLocalSpan); } } }
/** Sets a span associated with the context as the current server span. */ public static void setServerSpan(TraceContext context, ServerSpanThreadBinder threadBinder) { if (threadBinder == null) throw new NullPointerException("threadBinder == null"); ServerSpan serverSpan = ServerSpan.create(toSpan(context)); threadBinder.setCurrentSpan(serverSpan); }
private Brave(Builder builder) { serverTracer = ServerTracer.builder() .randomGenerator(builder.random) .spanCollector(builder.spanCollector) .state(builder.state) .traceSampler(builder.sampler).build(); clientTracer = ClientTracer.builder() .randomGenerator(builder.random) .spanCollector(builder.spanCollector) .state(builder.state) .traceSampler(builder.sampler).build(); localTracer = LocalTracer.builder() .randomGenerator(builder.random) .spanCollector(builder.spanCollector) .spanAndEndpoint(SpanAndEndpoint.LocalSpanAndEndpoint.create(builder.state)) .traceSampler(builder.sampler).build(); serverRequestInterceptor = new ServerRequestInterceptor(serverTracer); serverResponseInterceptor = new ServerResponseInterceptor(serverTracer); clientRequestInterceptor = new ClientRequestInterceptor(clientTracer); clientResponseInterceptor = new ClientResponseInterceptor(clientTracer); serverSpanAnnotationSubmitter = AnnotationSubmitter.create(SpanAndEndpoint.ServerSpanAndEndpoint.create(builder.state)); serverSpanThreadBinder = new ServerSpanThreadBinder(builder.state); clientSpanThreadBinder = new ClientSpanThreadBinder(builder.state); } }