/** * Ensures the specified {@link Tracing} uses a {@link RequestContextCurrentTraceContext}. * * @throws IllegalStateException if {@code tracing} does not use {@link RequestContextCurrentTraceContext} */ public static void ensureScopeUsesRequestContext(Tracing tracing) { final PingPongExtra extra = new PingPongExtra(); // trace contexts are not recorded until Tracer.toSpan, so this won't end up as junk data final TraceContext dummyContext = TraceContext.newBuilder().traceId(1).spanId(1) .extra(Collections.singletonList(extra)).build(); final boolean scopeUsesRequestContext; try (Scope scope = tracing.currentTraceContext().newScope(dummyContext)) { scopeUsesRequestContext = extra.isPong(); } if (!scopeUsesRequestContext) { throw new IllegalStateException( "Tracing.currentTraceContext is not a " + RequestContextCurrentTraceContext.class .getSimpleName() + " scope. " + "Please call Tracing.Builder.currentTraceContext(" + RequestContextCurrentTraceContext.class .getSimpleName() + ".INSTANCE)."); } }
@Test public void newScope_respondsToPing() { final PingPongExtra extra = new PingPongExtra(); final TraceContext extraContext = TraceContext.newBuilder().traceId(1).spanId(1) .extra(Collections.singletonList(extra)).build(); try (Scope traceContextScope = currentTraceContext.newScope(extraContext)) { assertThat(traceContextScope).hasToString("NoopScope"); assertThat(extra.isPong()).isTrue(); } }
@Test public void shouldSetPongIfOnlyExtra() { final PingPongExtra extra = new PingPongExtra(); final TraceContext context = TraceContext.newBuilder().traceId(1).spanId(1) .extra(Collections.singletonList(extra)).build(); PingPongExtra.maybeSetPong(context); assertThat(extra.isPong()).isTrue(); } }
.spanId(parent) .sampled(sampled) .extra(extra) .build());
.spanId(spanId); if (sampled != null) builder.sampled(sampled.booleanValue()); if (tags != null) builder.extra(Collections.singletonList(tags)); return builder.build();
.spanId(spanId); if (sampled != null) builder.sampled(sampled.booleanValue()); if (tags != null) builder.extra(Collections.singletonList(tags)); return builder.build();