/** * 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); }
@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); }
protected Tracing.Builder tracingBuilder(Sampler sampler) { return Tracing.newBuilder() .spanReporter(s -> { // make sure the context was cleared prior to finish.. no leaks! TraceContext current = httpTracing.tracing().currentTraceContext().get(); boolean contextLeak = false; if (current != null) { // add annotation in addition to throwing, in case we are off the main thread if (current.spanIdString().equals(s.id())) { s = s.toBuilder().addAnnotation(s.timestampAsLong(), CONTEXT_LEAK).build(); contextLeak = true; } } spans.add(s); // throw so that we can see the path to the code that leaked the context if (contextLeak) { throw new AssertionError(CONTEXT_LEAK + " on " + Thread.currentThread().getName()); } }) .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, EXTRA_KEY)) .currentTraceContext(currentTraceContext) .sampler(sampler); } }
@Override public boolean handle(TraceContext context, MutableSpan span) { if (!Boolean.TRUE.equals(context.sampled())) return true; Span.Builder builderWithContextData = Span.newBuilder() .traceId(context.traceIdString()) .parentId(context.parentIdString()) .id(context.spanIdString()); if (context.debug()) builderWithContextData.debug(true); converter.convert(span, builderWithContextData); spanReporter.report(builderWithContextData.build()); return true; }
@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"); } } }
.isEqualTo(parent.context().traceIdString()); assertThat(request.getHeader("x-b3-parentspanid")) .isEqualTo(parent.context().spanIdString());
/** * 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); }
@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); }
protected Tracing.Builder tracingBuilder(Sampler sampler) { return Tracing.newBuilder() .spanReporter(s -> { // make sure the context was cleared prior to finish.. no leaks! TraceContext current = httpTracing.tracing().currentTraceContext().get(); boolean contextLeak = false; if (current != null) { // add annotation in addition to throwing, in case we are off the main thread if (current.spanIdString().equals(s.id())) { s = s.toBuilder().addAnnotation(s.timestampAsLong(), CONTEXT_LEAK).build(); contextLeak = true; } } spans.add(s); // throw so that we can see the path to the code that leaked the context if (contextLeak) { throw new AssertionError(CONTEXT_LEAK + " on " + Thread.currentThread().getName()); } }) .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, EXTRA_KEY)) .currentTraceContext(currentTraceContext) .sampler(sampler); } }
@Override public boolean handle(TraceContext context, MutableSpan span) { if (!Boolean.TRUE.equals(context.sampled())) return true; Span.Builder builderWithContextData = Span.newBuilder() .traceId(context.traceIdString()) .parentId(context.parentIdString()) .id(context.spanIdString()); if (context.debug()) builderWithContextData.debug(true); converter.convert(span, builderWithContextData); spanReporter.report(builderWithContextData.build()); return true; }
@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"); } } }
.isEqualTo(parent.context().traceIdString()); assertThat(request.getHeader("x-b3-parentspanid")) .isEqualTo(parent.context().spanIdString());