SleuthSpanContextSupplier should take sampling decision into consideration

fixes gh-2116
This commit is contained in:
Jonatan Ivanov
2022-02-16 11:04:29 -08:00
parent 66c63a53ff
commit dd680254bd
2 changed files with 16 additions and 3 deletions

View File

@@ -43,13 +43,13 @@ public class SleuthSpanContextSupplier implements SpanContextSupplier {
@Override
public String getTraceId() {
Span span = tracer.currentSpan();
return span != null ? span.context().traceId() : null;
return (span != null && span.context().sampled()) ? span.context().traceId() : null;
}
@Override
public String getSpanId() {
Span span = tracer.currentSpan();
return span != null ? span.context().spanId() : null;
return (span != null && span.context().sampled()) ? span.context().spanId() : null;
}
}

View File

@@ -54,11 +54,24 @@ class SleuthSpanContextSupplierTests {
}
@Test
void should_provide_values_from_tracer_if_span_is_available() {
void should_provide_null_values_if_span_is_available_but_not_sampled() {
Span span = mock(Span.class);
TraceContext context = mock(TraceContext.class);
when(tracer.currentSpan()).thenReturn(span);
when(span.context()).thenReturn(context);
when(context.sampled()).thenReturn(false);
assertThat(spanContextSupplier.getTraceId()).isNull();
assertThat(spanContextSupplier.getSpanId()).isNull();
}
@Test
void should_provide_values_from_tracer_if_span_is_available_and_sampled() {
Span span = mock(Span.class);
TraceContext context = mock(TraceContext.class);
when(tracer.currentSpan()).thenReturn(span);
when(span.context()).thenReturn(context);
when(context.sampled()).thenReturn(true);
when(context.traceId()).thenReturn("42");
when(context.spanId()).thenReturn("24");