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; }
/** * Completes the span, assigning the most precise duration possible. */ public void finishSpan() { Span span = currentSpan().get(); if (span == null) return; recorder().finish(span, recorder().currentTimeMicroseconds(span)); currentSpan().setCurrentSpan(null); }
@Override Span get() { return getCurrentLocalSpan(); } }
/** * {@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); } }
Span newSpan() { // When a trace context is extracted from an incoming request, it may have only the // sampled header (no ids). If the header says unsampled, we must honor that. Since // we currently don't synthesize a fake span when a trace is unsampled, we have to // check sampled state explicitly. Boolean sample = currentServerSpan().sampled(); if (Boolean.FALSE.equals(sample)) { currentSpan().setCurrentSpan(null); return null; } Span span = spanFactory().nextSpan(maybeParent()); SpanId context = Brave.context(span); if (Boolean.FALSE.equals(context.sampled())) { currentSpan().setCurrentSpan(null); return null; } return span; }
/** * 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); }
/** * {@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); } } }
SpanId startSpan(String component, String operation, long timestamp, Span span) { recorder().start(span, timestamp); recorder().name(span, operation); recorder().tag(span, "lc", component); currentSpan().setCurrentSpan(span); return Brave.context(span); }
/** * Completes the span, which took {@code duration} microseconds. */ public void finishSpan(long duration) { Span span = currentSpan().get(); if (span == null) return; final Long timestamp; synchronized (span) { timestamp = span.getTimestamp(); } if (timestamp == null) { recorder().flush(span); } else { recorder().finish(span, timestamp + duration); } currentSpan().setCurrentSpan(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() ); }
serverSpanThreadBinder = new ServerSpanThreadBinder(builder.state); clientSpanThreadBinder = new ClientSpanThreadBinder(builder.state); localSpanThreadBinder = new LocalSpanThreadBinder(builder.state);
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() ); }
/** * @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() ); }