public static brave.Span toSpan(Tracer tracer, Span span) { if (tracer == null) throw new NullPointerException("tracer == null"); return tracer.toSpan(toTraceContext(Brave.context(span))); }
/** Converts the input into a new server span or {@linkplain ServerSpan#NOT_SAMPLED}. */ static ServerSpan create(Span span) { SpanId context = Brave.context(span); if (Boolean.FALSE.equals(context.sampled())) { return ServerSpan.NOT_SAMPLED; } return new AutoValue_ServerSpan(context, span, context.sampled()); }
@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); }
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; }
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); }
/** * 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); }
span = serverTracer.spanFactory().nextSpan(null); SpanId context = Brave.context(span);
/** * Start a new span for a new client request that will be bound to current thread. The ClientTracer can decide to return * <code>null</code> in case this request should not be traced (eg sampling). * * @param requestName Request name. Should be lowercase. Null or empty will defer to the server's name of the operation. * @return Span id for new request or <code>null</code> in case we should not trace this new client request. */ public SpanId startNewSpan(@Nullable String requestName) { // 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 newSpan = spanFactory().nextSpan(maybeParent()); SpanId nextContext = Brave.context(newSpan); if (Boolean.FALSE.equals(nextContext.sampled())) { currentSpan().setCurrentSpan(null); return null; } recorder().name(newSpan, requestName); currentSpan().setCurrentSpan(newSpan); return nextContext; }