/** * Use this to create a span for processing the given message. Note: the result has no * name and is not started. * <p> * <p> * This creates a child from identifiers extracted from the message headers, or a new * span if one couldn't be extracted. */ public Span nextSpan(Message<?> message) { MessageHeaderAccessor headers = mutableHeaderAccessor(message); TraceContextOrSamplingFlags extracted = this.extractor.extract(headers); headers.setImmutable(); Span result = this.tracer.nextSpan(extracted); if (extracted.context() == null && !result.isNoop()) { addTags(message, result, null); } if (log.isDebugEnabled()) { log.debug("Created a new span " + result); } return result; }
static TraceContextOrSamplingFlags tryParseSamplingFlags(CharSequence b3, int pos) { int flags = parseFlags(b3, pos); if (flags == 0) return null; return TraceContextOrSamplingFlags.create(SamplingFlags.toSamplingFlags(flags)); }
/** Creates a potentially noop span representing this request */ // copy/pasted from HttpServerHandler.nextSpan Span nextSpan(TraceContextOrSamplingFlags extracted, HttpRequest request) { Boolean sampled = extracted.sampled(); // only recreate the context if the http sampler made a decision if (sampled == null && (sampled = sampler.trySample(adapter, request)) != null) { extracted = extracted.sampled(sampled.booleanValue()); } return extracted.context() != null ? tracer.joinSpan(extracted.context()) : tracer.nextSpan(extracted); }
/** @deprecated do not use object variant.. only set when you have a sampling decision */ @Deprecated public TraceContextOrSamplingFlags sampled(@Nullable Boolean sampled) { if (sampled != null) return sampled(sampled.booleanValue()); int flags = value.flags; flags &= ~FLAG_SAMPLED_SET; flags &= ~FLAG_SAMPLED; if (flags == value.flags) return this; // save effort if no change return withFlags(flags); }
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"); }
TraceContext context = extracted.context(); if (context != null) return newChild(context); TraceIdContext traceIdContext = extracted.traceIdContext(); if (traceIdContext != null) { return _toSpan(nextContext( InternalPropagation.instance.flags(extracted.traceIdContext()), traceIdContext.traceIdHigh(), traceIdContext.traceId(), 0L, 0L, extracted.extra() )); SamplingFlags samplingFlags = extracted.samplingFlags(); List<Object> extra = extracted.extra();
void verifyRoundTrip(TraceContextOrSamplingFlags expected) { TraceContextOrSamplingFlags extracted = propagation.extractor(mapEntry).extract(map); assertThat(extracted) .isEqualTo(expected); Map<K, String> injected = new LinkedHashMap<>(); if (expected.context() != null) { propagation.injector(mapEntry).inject(expected.context(), injected); } else { inject(injected, expected.samplingFlags()); } assertThat(map).isEqualTo(injected); }
/** Returns a mapping of any fields in the extraction result. */ public static Map<String, String> getAll(TraceContextOrSamplingFlags extracted) { if (extracted == null) throw new NullPointerException("extracted == null"); TraceContext extractedContext = extracted.context(); if (extractedContext != null) return getAll(extractedContext); PropagationFields fields = TraceContext.findExtra(Extra.class, extracted.extra()); return fields != null ? fields.toMap() : Collections.emptyMap(); }
@Override public TraceContextOrSamplingFlags extract(C carrier) { Tags tags = null; if (carrier instanceof Metadata) { tags = extractTags(((Metadata) carrier).get(GRPC_TAGS_BIN)); byte[] bytes = ((Metadata) carrier).get(GRPC_TRACE_BIN); if (bytes != null) { TraceContext maybeContext = TraceContextBinaryFormat.parseBytes(bytes, tags); if (maybeContext != null) return TraceContextOrSamplingFlags.create(maybeContext); } } TraceContextOrSamplingFlags result = delegate.extract(carrier); if (tags == null) return result; return result.toBuilder().addExtra(tags).build(); } }
@Override public TraceContextOrSamplingFlags extract(C carrier) { if (carrier == null) throw new NullPointerException("carrier == null"); // try to extract single-header format TraceContextOrSamplingFlags extracted = singleExtractor.extract(carrier); if (!extracted.equals(TraceContextOrSamplingFlags.EMPTY)) return extracted; // Start by looking at the sampled state as this is used regardless // Official sampled value is 1, though some old instrumentation send true String sampled = getter.get(carrier, propagation.sampledKey); Boolean sampledV = sampled != null ? sampled.equals("1") || sampled.equalsIgnoreCase("true") : null; boolean debug = "1".equals(getter.get(carrier, propagation.debugKey)); String traceIdString = getter.get(carrier, propagation.traceIdKey); // It is ok to go without a trace ID, if sampling or debug is set if (traceIdString == null) return TraceContextOrSamplingFlags.create(sampledV, debug); // Try to parse the trace IDs into the context TraceContext.Builder result = TraceContext.newBuilder(); if (result.parseTraceId(traceIdString, propagation.traceIdKey) && result.parseSpanId(getter, carrier, propagation.spanIdKey) && result.parseParentId(getter, carrier, propagation.parentSpanIdKey)) { if (sampledV != null) result.sampled(sampledV.booleanValue()); if (debug) result.debug(true); return TraceContextOrSamplingFlags.create(result.build()); } return TraceContextOrSamplingFlags.EMPTY; // trace context is malformed so return empty } }
return TraceContextOrSamplingFlags.newBuilder() .extra(extra) .samplingFlags(SamplingFlags.Builder.build(sampled)) .build(); } else if (parent == null) { return TraceContextOrSamplingFlags.newBuilder() .extra(extra) .traceIdContext(TraceIdContext.newBuilder() .build(); return TraceContextOrSamplingFlags.create(TraceContext.newBuilder() .traceIdHigh(traceIdHigh) .traceId(traceId)
if (extracted.samplingFlags() != null && extracted.extra().isEmpty()) { Span span = consumerSpansForTopic.get(topic); if (span == null) {
TraceContextOrSamplingFlags extractAndClearHeaders(Headers headers) { TraceContextOrSamplingFlags extracted = extractor.extract(headers); // clear propagation headers if we were able to extract a span if (!extracted.equals(TraceContextOrSamplingFlags.EMPTY)) { clearHeaders(headers); } return extracted; }
TraceContext context = extracted.context(); if (context != null) return newChild(context); TraceIdContext traceIdContext = extracted.traceIdContext(); if (traceIdContext != null) { return _toSpan(nextContext( InternalPropagation.instance.flags(extracted.traceIdContext()), traceIdContext.traceIdHigh(), traceIdContext.traceId(), 0L, 0L, extracted.extra() )); SamplingFlags samplingFlags = extracted.samplingFlags(); List<Object> extra = extracted.extra();
void verifyRoundTrip(TraceContextOrSamplingFlags expected) { TraceContextOrSamplingFlags extracted = propagation.extractor(mapEntry).extract(map); assertThat(extracted) .isEqualTo(expected); Map<K, String> injected = new LinkedHashMap<>(); if (expected.context() != null) { propagation.injector(mapEntry).inject(expected.context(), injected); } else { inject(injected, expected.samplingFlags()); } assertThat(map).isEqualTo(injected); }
/** Returns a mapping of any fields in the extraction result. */ public static Map<String, String> getAll(TraceContextOrSamplingFlags extracted) { if (extracted == null) throw new NullPointerException("extracted == null"); TraceContext extractedContext = extracted.context(); if (extractedContext != null) return getAll(extractedContext); PropagationFields fields = TraceContext.findExtra(Extra.class, extracted.extra()); return fields != null ? fields.toMap() : Collections.emptyMap(); }
@Override public TraceContextOrSamplingFlags extract(C carrier) { Tags tags = null; if (carrier instanceof Metadata) { tags = extractTags(((Metadata) carrier).get(GRPC_TAGS_BIN)); byte[] bytes = ((Metadata) carrier).get(GRPC_TRACE_BIN); if (bytes != null) { TraceContext maybeContext = TraceContextBinaryFormat.parseBytes(bytes, tags); if (maybeContext != null) return TraceContextOrSamplingFlags.create(maybeContext); } } TraceContextOrSamplingFlags result = delegate.extract(carrier); if (tags == null) return result; return result.toBuilder().addExtra(tags).build(); } }
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"); }
@Override public TraceContextOrSamplingFlags extract(C carrier) { if (carrier == null) throw new NullPointerException("carrier == null"); // try to extract single-header format TraceContextOrSamplingFlags extracted = singleExtractor.extract(carrier); if (!extracted.equals(TraceContextOrSamplingFlags.EMPTY)) return extracted; // Start by looking at the sampled state as this is used regardless // Official sampled value is 1, though some old instrumentation send true String sampled = getter.get(carrier, propagation.sampledKey); Boolean sampledV = sampled != null ? sampled.equals("1") || sampled.equalsIgnoreCase("true") : null; boolean debug = "1".equals(getter.get(carrier, propagation.debugKey)); String traceIdString = getter.get(carrier, propagation.traceIdKey); // It is ok to go without a trace ID, if sampling or debug is set if (traceIdString == null) return TraceContextOrSamplingFlags.create(sampledV, debug); // Try to parse the trace IDs into the context TraceContext.Builder result = TraceContext.newBuilder(); if (result.parseTraceId(traceIdString, propagation.traceIdKey) && result.parseSpanId(getter, carrier, propagation.spanIdKey) && result.parseParentId(getter, carrier, propagation.parentSpanIdKey)) { if (sampledV != null) result.sampled(sampledV.booleanValue()); if (debug) result.debug(true); return TraceContextOrSamplingFlags.create(result.build()); } return TraceContextOrSamplingFlags.EMPTY; // trace context is malformed so return empty } }
if (extracted.samplingFlags() != null && extracted.extra().isEmpty()) { Span span = consumerSpansForTopic.get(topic); if (span == null) {