/** 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()); }
public static TraceData create(SpanId spanId) { return new AutoValue_TraceData(spanId, spanId.sampled()); }
@Override public TraceData build() { if (spanId == null) { return new AutoValue_TraceData(spanId, sample); } if (sample != null ) { return new AutoValue_TraceData(spanId.toBuilder().sampled(sample).build(), sample); } return new AutoValue_TraceData(spanId, spanId.sampled()); } }
@Override Span joinSpan(SpanId context) { if (!supportsJoin()) return nextSpan(context); // If the sampled flag was left unset, we need to make the decision here if (context.sampled() == null) { return Brave.toSpan(context.toBuilder() .sampled(sampler().isSampled(context.traceId)) .build()); } else if (context.sampled()) { // We know an instrumented caller initiated the trace if they sampled it return Brave.toSpan(context).setShared(); } else { return Brave.toSpan(context); } } }
public static TraceContext toTraceContext(SpanId spanId) { // visible for testing if (spanId == null) throw new NullPointerException("spanId == null"); return TraceContext.newBuilder() .traceIdHigh(spanId.traceIdHigh) .traceId(spanId.traceId) .parentId(spanId.nullableParentId()) .spanId(spanId.spanId) .debug(spanId.debug()) .sampled(spanId.sampled()).build(); }
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; }
if (!context.sampled()) { LOGGER.fine("Trace is unsampled."); serverTracer.setStateNoTracing();
/** * 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; }