.parentId(parentId != 0L ? parentId : null) .spanId(spanId) .sampled(debug ? Boolean.TRUE : sampled(flags)) .debug(debug) .build()
static SpanId getSpanId(String traceId, String spanId, String parentSpanId, Boolean sampled) { return SpanId.builder() .traceIdHigh(traceId.length() == 32 ? convertToLong(traceId, 0) : 0) .traceId(convertToLong(traceId)) .spanId(convertToLong(spanId)) .sampled(sampled) .parentId(parentSpanId == null ? null : convertToLong(parentSpanId)).build(); } }
static SpanId getSpanId(String traceId, String spanId, String parentSpanId, Boolean sampled) { return SpanId.builder() .traceIdHigh(traceId.length() == 32 ? convertToLong(traceId, 0) : 0) .traceId(convertToLong(traceId)) .spanId(convertToLong(spanId)) .sampled(sampled) .parentId(parentSpanId == null ? null : convertToLong(parentSpanId)).build(); } }
@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); } } }
@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()); } }
static SpanId toSpanId(TraceContext context) { return SpanId.builder() .traceIdHigh(context.traceIdHigh()) .traceId(context.traceId()) .parentId(context.parentId()) .spanId(context.spanId()) .debug(context.debug()) .sampled(context.sampled()).build(); } }