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; }
/** * 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); }
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); }
/** * {@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 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); } } }
/** * 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); }