/** Converts the context to a Span object after decorating it for propagation */ public Span toSpan(TraceContext context) { if (context == null) throw new NullPointerException("context == null"); if (alwaysSampleLocal) { int flags = InternalPropagation.instance.flags(context); if ((flags & FLAG_SAMPLED_LOCAL) != FLAG_SAMPLED_LOCAL) { context = InternalPropagation.instance.withFlags(context, flags | FLAG_SAMPLED_LOCAL); } } // decorating here addresses join, new traces or children and ad-hoc trace contexts return _toSpan(propagationFactory.decorate(context)); }
/** @see TraceIdContext#sampled() */ public Builder sampled(boolean sampled) { flags = InternalPropagation.sampled(sampled, flags); return this; }
@Override protected TraceContext contextWithExtra(TraceContext context, List<Object> immutableExtra) { return InternalPropagation.instance.withExtra(context, immutableExtra); } }
if (context == null) throw new NullPointerException("context == null"); if (!supportsJoin) return newChild(context); int flags = InternalPropagation.instance.flags(context); if (alwaysSampleLocal && (flags & FLAG_SAMPLED_LOCAL) != FLAG_SAMPLED_LOCAL) { flags |= FLAG_SAMPLED_LOCAL; flags = InternalPropagation.sampled(sampler.isSampled(context.traceId()), flags); } else if ((flags & FLAG_SAMPLED) == FLAG_SAMPLED) { context = InternalPropagation.instance.newTraceContext( flags | FLAG_LOCAL_ROOT, context.traceIdHigh(),
flags = InternalPropagation.sampled(sampler.isSampled(traceId), flags); return propagationFactory.decorate(InternalPropagation.instance.newTraceContext( flags, traceIdHigh,
boolean isNoop(TraceContext context) { if (finishedSpanHandler == FinishedSpanHandler.NOOP || noop.get()) return true; int flags = InternalPropagation.instance.flags(context); if ((flags & FLAG_SAMPLED_LOCAL) == FLAG_SAMPLED_LOCAL) return false; return (flags & FLAG_SAMPLED) != FLAG_SAMPLED; }
/** Reports spans orphaned by garbage collection. */ void reportOrphanedSpans() { RealKey contextKey; // This is called on critical path of unrelated traced operations. If we have orphaned spans, be // careful to not penalize the performance of the caller. It is better to cache time when // flushing a span than hurt performance of unrelated operations by calling // currentTimeMicroseconds N times long flushTime = 0L; boolean noop = zipkinHandler == FinishedSpanHandler.NOOP || this.noop.get(); while ((contextKey = (RealKey) poll()) != null) { PendingSpan value = delegate.remove(contextKey); if (noop || value == null || !contextKey.sampled) continue; if (flushTime == 0L) flushTime = clock.currentTimeMicroseconds(); TraceContext context = InternalPropagation.instance.newTraceContext( InternalPropagation.FLAG_SAMPLED_SET | InternalPropagation.FLAG_SAMPLED, contextKey.traceIdHigh, contextKey.traceId, contextKey.localRootId, 0L, contextKey.spanId, Collections.emptyList() ); value.state.annotate(flushTime, "brave.flush"); try { zipkinHandler.handle(context, value.state); } catch (RuntimeException e) { Platform.get().log("error reporting {0}", context, e); } } }
TraceContextOrSamplingFlags withFlags(int flags) { switch (type) { case 1: TraceContext context = InternalPropagation.instance.withFlags((TraceContext) value, flags); return new TraceContextOrSamplingFlags(type, context, extra); case 2: TraceIdContext traceIdContext = idContextWithFlags(flags); return new TraceContextOrSamplingFlags(type, traceIdContext, extra); case 3: SamplingFlags samplingFlags = SamplingFlags.toSamplingFlags(flags); if (extra.isEmpty()) return create(samplingFlags); return new TraceContextOrSamplingFlags(type, samplingFlags, extra); } throw new AssertionError("programming error"); }
if (context == null) throw new NullPointerException("context == null"); if (!supportsJoin) return newChild(context); int flags = InternalPropagation.instance.flags(context); if (alwaysSampleLocal && (flags & FLAG_SAMPLED_LOCAL) != FLAG_SAMPLED_LOCAL) { flags |= FLAG_SAMPLED_LOCAL; flags = InternalPropagation.sampled(sampler.isSampled(context.traceId()), flags); } else if ((flags & FLAG_SAMPLED) == FLAG_SAMPLED) { context = InternalPropagation.instance.newTraceContext( flags | FLAG_LOCAL_ROOT, context.traceIdHigh(),
flags = InternalPropagation.sampled(sampler.isSampled(traceId), flags); return propagationFactory.decorate(InternalPropagation.instance.newTraceContext( flags, traceIdHigh,
/** * Called by methods which can accept externally supplied parent trace contexts: Ex. {@link * #newChild(TraceContext)} and {@link #startScopedSpanWithParent(String, TraceContext)}. This * implies the {@link TraceContext#localRootId()} could be zero, if the context was manually * created. */ TraceContext nextContext(TraceContext parent) { return nextContext( InternalPropagation.instance.flags(parent), parent.traceIdHigh(), parent.traceId(), parent.localRootId(), parent.spanId(), parent.extra() ); }
/** Trace contexts are equal only on trace ID and span ID. try to get the parent's clock */ @Nullable TickClock getClockFromParent(TraceContext context) { long parentId = context.parentIdAsLong(); // NOTE: we still look for lookup key even on root span, as a client span can be root, and a // server can share the same ID. Essentially, a shared span is similar to a child. PendingSpan parent = null; if (context.shared() || parentId != 0L) { long spanId = parentId != 0L ? parentId : context.spanId(); parent = delegate.get(InternalPropagation.instance.newTraceContext( 0, context.traceIdHigh(), context.traceId(), 0, 0, spanId, Collections.emptyList() )); } return parent != null ? parent.clock : null; }
TraceContextOrSamplingFlags withFlags(int flags) { switch (type) { case 1: TraceContext context = InternalPropagation.instance.withFlags((TraceContext) value, flags); return new TraceContextOrSamplingFlags(type, context, extra); case 2: TraceIdContext traceIdContext = idContextWithFlags(flags); return new TraceContextOrSamplingFlags(type, traceIdContext, extra); case 3: SamplingFlags samplingFlags = SamplingFlags.toSamplingFlags(flags); if (extra.isEmpty()) return create(samplingFlags); return new TraceContextOrSamplingFlags(type, samplingFlags, extra); } throw new AssertionError("programming error"); }
/** @see TraceContext#sampled() */ public Builder sampled(boolean sampled) { flags = InternalPropagation.sampled(sampled, flags); return this; }
/** Converts the context to a Span object after decorating it for propagation */ public Span toSpan(TraceContext context) { if (context == null) throw new NullPointerException("context == null"); if (alwaysSampleLocal) { int flags = InternalPropagation.instance.flags(context); if ((flags & FLAG_SAMPLED_LOCAL) != FLAG_SAMPLED_LOCAL) { context = InternalPropagation.instance.withFlags(context, flags | FLAG_SAMPLED_LOCAL); } } // decorating here addresses join, new traces or children and ad-hoc trace contexts return _toSpan(propagationFactory.decorate(context)); }
protected TraceContext contextWithExtra(TraceContext context, List<Object> immutableExtra) { return InternalPropagation.instance.withExtra(context, immutableExtra); } }
if (traceIdContext != null) { return _toSpan(nextContext( InternalPropagation.instance.flags(extracted.traceIdContext()), traceIdContext.traceIdHigh(), traceIdContext.traceId(), flags = InternalPropagation.instance.flags(implicitParent); traceIdHigh = implicitParent.traceIdHigh(); traceId = implicitParent.traceId(); extra = concatImmutableLists(extra, implicitParent.extra()); } else { flags = InternalPropagation.instance.flags(samplingFlags);
/** Reports spans orphaned by garbage collection. */ void reportOrphanedSpans() { RealKey contextKey; // This is called on critical path of unrelated traced operations. If we have orphaned spans, be // careful to not penalize the performance of the caller. It is better to cache time when // flushing a span than hurt performance of unrelated operations by calling // currentTimeMicroseconds N times long flushTime = 0L; boolean noop = zipkinHandler == FinishedSpanHandler.NOOP || this.noop.get(); while ((contextKey = (RealKey) poll()) != null) { PendingSpan value = delegate.remove(contextKey); if (noop || value == null || !contextKey.sampled) continue; if (flushTime == 0L) flushTime = clock.currentTimeMicroseconds(); TraceContext context = InternalPropagation.instance.newTraceContext( InternalPropagation.FLAG_SAMPLED_SET | InternalPropagation.FLAG_SAMPLED, contextKey.traceIdHigh, contextKey.traceId, contextKey.localRootId, 0L, contextKey.spanId, Collections.emptyList() ); value.state.annotate(flushTime, "brave.flush"); try { zipkinHandler.handle(context, value.state); } catch (RuntimeException e) { Platform.get().log("error reporting {0}", context, e); } } }
public TraceContextOrSamplingFlags sampled(boolean sampled) { int flags = InternalPropagation.sampled(sampled, value.flags); if (flags == value.flags) return this; // save effort if no change return withFlags(flags); }
/** Returns an immutable result from the values currently in the builder */ public final TraceContextOrSamplingFlags build() { final TraceContextOrSamplingFlags result; if (!extra.isEmpty() && type == 1) { // move extra to the trace context TraceContext context = (TraceContext) value; if (context.extra().isEmpty()) { context = InternalPropagation.instance.withExtra(context, ensureImmutable(extra)); } else { context = InternalPropagation.instance.withExtra(context, concatImmutableLists(context.extra(), extra)); } result = new TraceContextOrSamplingFlags(type, context, emptyList()); } else { // make sure the extra data is immutable and unmodifiable result = new TraceContextOrSamplingFlags(type, value, ensureImmutable(extra)); } if (!sampledLocal) return result; // save effort if no change return result.withFlags(value.flags | FLAG_SAMPLED_LOCAL); }