static byte[] toBytes(TraceContext traceContext) { checkNotNull(traceContext, "traceContext"); byte[] bytes = new byte[FORMAT_LENGTH]; bytes[0] = VERSION; bytes[1] = TRACE_ID_FIELD_ID; writeLong(bytes, 2, traceContext.traceIdHigh()); writeLong(bytes, 10, traceContext.traceId()); bytes[18] = SPAN_ID_FIELD_ID; writeLong(bytes, 19, traceContext.spanId()); bytes[27] = TRACE_OPTION_FIELD_ID; if (traceContext.sampled() != null && traceContext.sampled()) { bytes[28] = 1; } return bytes; }
@Override public void accept(CurrentTraceContext current) { try (Scope ws = current.newScope(TraceContext.newBuilder().traceId(1L).spanId(2L).build())) { } } }
String traceIdString = currentSpan.traceIdString(); MDC.put("traceId", traceIdString); MDC.put(LEGACY_TRACE_ID_NAME, traceIdString); String parentId = currentSpan.parentId() != null ? HexCodec.toLowerHex(currentSpan.parentId()) : null; replace("parentId", parentId); replace(LEGACY_PARENT_ID_NAME, parentId); String spanId = HexCodec.toLowerHex(currentSpan.spanId()); MDC.put("spanId", spanId); MDC.put(LEGACY_SPAN_ID_NAME, spanId); String sampled = String.valueOf(currentSpan.sampled()); MDC.put("spanExportable", sampled); MDC.put(LEGACY_EXPORTABLE_NAME, sampled); log("Starting scope for span: {}", currentSpan); if (currentSpan.parentId() != null) { if (log.isTraceEnabled()) { log.trace("With parent: {}", currentSpan.parentId());
RealKey(TraceContext context, ReferenceQueue<TraceContext> queue) { super(context, queue); hashCode = context.hashCode(); traceIdHigh = context.traceIdHigh(); traceId = context.traceId(); localRootId = context.localRootId(); spanId = context.spanId(); sampled = Boolean.TRUE.equals(context.sampled()); }
/** Resolves hash code collisions */ @Override public boolean equals(Object other) { RealKey that = (RealKey) other; TraceContext thatContext = that.get(); if (thatContext == null) return false; return (traceIdHigh == thatContext.traceIdHigh()) && (traceId == thatContext.traceId()) && (spanId == thatContext.spanId()) && shared == thatContext.shared(); } }
/** * 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() ); }
private void serverResponse(Tracing brave, String serviceName, Exchange exchange) { Span span = null; ZipkinState state = exchange.getProperty(ZipkinState.KEY, ZipkinState.class); if (state != null) { // only process if it was a zipkin server event span = state.popServerSpan(); } if (span != null) { ZipkinServerResponseAdapter parser = new ZipkinServerResponseAdapter(this, exchange); parser.onResponse(exchange, span.customizer()); span.finish(); TraceContext context = span.context(); String traceId = "" + context.traceIdString(); String spanId = "" + context.spanId(); String parentId = context.parentId() != null ? "" + context.parentId() : null; if (camelContext.isUseMDCLogging()) { MDC.put("traceId", traceId); MDC.put("spanId", spanId); MDC.put("parentId", parentId); } if (LOG.isDebugEnabled()) { if (parentId != null) { LOG.debug(String.format("serverResponse[service=%s, traceId=%20s, spanId=%20s, parentId=%20s]", serviceName, traceId, spanId, parentId)); } else { LOG.debug(String.format("serverResponse[service=%s, traceId=%20s, spanId=%20s]", serviceName, traceId, spanId)); } } } }
Tracer tracer= zipkinCollectorConfigurationFactory.getTracing().tracer(); TraceContext traceContext= TraceContext.newBuilder() .traceId(RpcTraceContext.getTraceId()) .parentId(RpcTraceContext.getParentId()) span.context().traceId(), span.context().parentId(), span.context().spanId());
@Override public void inject(TraceContext traceContext, C carrier) { setter.put(carrier, propagation.traceIdKey, traceContext.traceIdString()); setter.put(carrier, propagation.spanIdKey, traceContext.spanIdString()); String parentId = traceContext.parentIdString(); if (parentId != null) { setter.put(carrier, propagation.parentSpanIdKey, parentId); } if (traceContext.debug()) { setter.put(carrier, propagation.debugKey, "1"); } else if (traceContext.sampled() != null) { setter.put(carrier, propagation.sampledKey, traceContext.sampled() ? "1" : "0"); } } }
flags = InternalPropagation.sampled(sampler.isSampled(context.traceId()), flags); } else if ((flags & FLAG_SAMPLED) == FLAG_SAMPLED) { context.traceIdHigh(), context.traceId(), context.spanId(), // local root context.parentIdAsLong(), context.spanId(), context.extra() ); return _toSpan(propagationFactory.decorate(context));
public final TraceContext decorate(TraceContext context) { long traceId = context.traceId(), spanId = context.spanId(); Class<E> type = type(); List<Object> extra = context.extra(); int extraSize = extra.size(); if (extraSize == 0) { extra.add(consolidated); if (extra == context.extra()) return context; return contextWithExtra(context, Collections.unmodifiableList(extra));
for (int i = 0, length = traceContext.extra().size(); i < length; i++) { Object next = traceContext.extra().get(i); if (next instanceof Extra) { extra = ((Extra) next).fields; writeRoot(traceContext, result, 5); System.arraycopy(PARENT, 0, result, 40, 8); writeHexLong(result, 48, traceContext.spanId()); System.arraycopy(SAMPLED, 0, result, 64, 9); Boolean sampled = traceContext.sampled();
void noticesDifferentSpanId(Scope scope) { TraceContext differentSpanId = context.toBuilder().spanId(context.spanId() + 1L).build(); try (Scope scope2 = currentTraceContext.maybeScope(differentSpanId)) { assertThat(scope2).isNotEqualTo(Scope.NOOP); assertThat(currentTraceContext.get()) .isEqualTo(differentSpanId); verifyImplicitContext(differentSpanId); } finally { scope.close(); } }
@Test public void makesChildOfCurrentSpan() throws Exception { Tracer tracer = httpTracing.tracing().tracer(); server.enqueue(new MockResponse()); ScopedSpan parent = tracer.startScopedSpan("test"); try { get(client, "/foo"); } finally { parent.finish(); } RecordedRequest request = server.takeRequest(); assertThat(request.getHeader("x-b3-traceId")) .isEqualTo(parent.context().traceIdString()); assertThat(request.getHeader("x-b3-parentspanid")) .isEqualTo(parent.context().spanIdString()); assertThat(Arrays.asList(takeSpan(), takeSpan())) .extracting(Span::kind) .containsOnly(null, Span.Kind.CLIENT); }
@Test public void testThatProvidedSpanIsNotDetachedWhenActiveUsingAsyncClient() throws Exception { final WebClient client = createWebClient("/bookstore/books", braveClientProvider); final Span span = brave.tracer().nextSpan().name("test span").start(); try { try (SpanInScope scope = brave.tracer().withSpanInScope(span)) { final Future<Response> f = client.async().get(); final Response r = f.get(1, TimeUnit.SECONDS); assertEquals(Status.OK.getStatusCode(), r.getStatus()); assertThat(brave.tracer().currentSpan().context().spanId(), equalTo(span.context().spanId())); assertThat(TestSpanReporter.getAllSpans().size(), equalTo(3)); assertThat(TestSpanReporter.getAllSpans().get(0).name(), equalTo("get books")); assertThat(TestSpanReporter.getAllSpans().get(1).name(), equalTo("get /bookstore/books")); assertThat(TestSpanReporter.getAllSpans().get(2).name(), equalTo("get " + client.getCurrentURI())); assertThatTraceHeadersArePresent(r, true); } } finally { span.finish(); } // Await till flush happens, usually a second is enough await().atMost(Duration.ONE_SECOND).until(()-> TestSpanReporter.getAllSpans().size() == 4); assertThat(TestSpanReporter.getAllSpans().size(), equalTo(4)); assertThat(TestSpanReporter.getAllSpans().get(3).name(), equalTo("test span")); }
/** * Idempotently sets correlation properties to hex representation of trace identifiers in this * context. */ void maybeReplaceTraceContext( TraceContext currentSpan, String previousTraceId, @Nullable String previousParentId, String previousSpanId ) { String traceId = currentSpan.traceIdString(); if (!traceId.equals(previousTraceId)) put("traceId", currentSpan.traceIdString()); String parentId = currentSpan.parentIdString(); if (parentId == null) { remove("parentId"); } else { boolean sameParentId = parentId.equals(previousParentId); if (!sameParentId) put("parentId", parentId); } String spanId = currentSpan.spanIdString(); if (!spanId.equals(previousSpanId)) put("spanId", spanId); }
@Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof RealSpan)) return false; return context.equals(((RealSpan) o).context); }
@Override public boolean isSampled(Span span) { // TODO replace with Span#unwrap once https://github.com/opentracing/opentracing-java/pull/211 is merged if (span instanceof SpanWrapper) { span = ((SpanWrapper) span).getDelegate(); } if (span instanceof BraveSpan) { final BraveSpan braveSpan = (BraveSpan) span; return braveSpan.unwrap().context().sampled(); } return false; }
/** Writes 35 characters representing the input trace ID to the buffer at the given offset */ static void writeRoot(TraceContext context, char[] result, int offset) { result[offset] = '1'; // version result[offset + 1] = '-'; // delimiter long high = context.traceIdHigh(); writeHexByte(result, offset + 2, (byte) ((high >>> 56L) & 0xff)); writeHexByte(result, offset + 4, (byte) ((high >>> 48L) & 0xff)); writeHexByte(result, offset + 6, (byte) ((high >>> 40L) & 0xff)); writeHexByte(result, offset + 8, (byte) ((high >>> 32L) & 0xff)); result[offset + 10] = '-'; writeHexByte(result, offset + 11, (byte) ((high >>> 24L) & 0xff)); writeHexByte(result, offset + 13, (byte) ((high >>> 16L) & 0xff)); writeHexByte(result, offset + 15, (byte) ((high >>> 8L) & 0xff)); writeHexByte(result, offset + 17, (byte) (high & 0xff)); writeHexLong(result, offset + 19, context.traceId()); }
@Override public boolean isRoot(Span span) { // TODO replace with Span#unwrap once https://github.com/opentracing/opentracing-java/pull/211 is merged if (span instanceof SpanWrapper) { span = ((SpanWrapper) span).getDelegate(); } if (span instanceof BraveSpan) { final BraveSpan braveSpan = (BraveSpan) span; return braveSpan.unwrap().context().parentId() == null; } return false; }