/** * Sets the current Trace/Span state. Using this method indicates that we got no information about being part of an * existing trace or about the fact that we should not trace the current request. In this case the ServerTracer will * decide what to do. * * @param spanName The name of our current request/span. */ public void setStateUnknown(String spanName) { checkNotBlank(spanName, "Null or blank span name"); long newTraceId = randomGenerator().nextLong(); if (!traceSampler().isSampled(newTraceId)) { spanAndEndpoint().state().setCurrentServerSpan(ServerSpan.NOT_SAMPLED); return; } spanAndEndpoint().state().setCurrentServerSpan( ServerSpan.create(newTraceId, newTraceId, null, spanName)); }
@Override Span nextSpan(@Nullable SpanId maybeParent) { // Generates a new 64-bit ID, taking care to dodge zero which can be confused with absent long newSpanId = randomGenerator().nextLong(); while (newSpanId == 0L) { newSpanId = randomGenerator().nextLong(); } if (maybeParent == null) { // new trace return Brave.toSpan(SpanId.builder() .traceIdHigh(traceId128Bit() ? nextTraceIdHigh(randomGenerator()) : 0L) .traceId(newSpanId) .spanId(newSpanId) .sampled(sampler().isSampled(newSpanId)) .build()); } return Brave.toSpan(maybeParent.toBuilder() .parentId(maybeParent.spanId) .spanId(newSpanId) .build()); }
@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); } } }
/** * 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 and not <code>null</code> or empty. * @return Span id for new request or <code>null</code> in case we should not trace this new client request. */ public SpanId startNewSpan(String requestName) { Boolean sample = spanAndEndpoint().state().sample(); if (Boolean.FALSE.equals(sample)) { spanAndEndpoint().state().setCurrentClientSpan(null); return null; } SpanId newSpanId = getNewSpanId(); if (sample == null) { // No sample indication is present. if (!traceSampler().isSampled(newSpanId.traceId)) { spanAndEndpoint().state().setCurrentClientSpan(null); return null; } } Span newSpan = newSpanId.toSpan(); newSpan.setName(requestName); spanAndEndpoint().state().setCurrentClientSpan(newSpan); return newSpanId; }
if (sample == null) { if (!traceSampler().isSampled(newSpanId.traceId)) { spanAndEndpoint().state().setCurrentLocalSpan(null); return null;