Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DefaultTracer: Fix noParent being ignored after explicit parent. #1617

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions api/src/main/java/io/opentelemetry/trace/DefaultTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ static NoopSpanBuilder create(String spanName) {
return new NoopSpanBuilder(spanName);
}

private boolean isRootSpan;
@Nullable private SpanContext spanContext;

@Override
public Span startSpan() {
if (spanContext == null && !isRootSpan) {
if (spanContext == null) {
spanContext = TracingContextUtils.getCurrentSpan().getContext();
}

Expand Down Expand Up @@ -103,7 +102,7 @@ public NoopSpanBuilder setParent(Context context) {

@Override
public NoopSpanBuilder setNoParent() {
isRootSpan = true;
spanContext = SpanContext.getInvalid();
return this;
}

Expand Down
16 changes: 16 additions & 0 deletions api/src/test/java/io/opentelemetry/trace/DefaultTracerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ void testSpanContextPropagation_fromContext() {
assertThat(span.getContext()).isSameAs(spanContext);
}

@Test
void testSpanContextPropagation_fromContextAfterNoParent() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believed this test would fail before this PR initially, but it's the other way round. Still adding this test to cover this case too.

Context context = TracingContextUtils.withSpan(new DefaultSpan(spanContext), Context.current());

Span span = defaultTracer.spanBuilder(SPAN_NAME).setNoParent().setParent(context).startSpan();
assertThat(span.getContext()).isSameAs(spanContext);
}

@Test
void testSpanContextPropagation_fromContextThenNoParent() {
Context context = TracingContextUtils.withSpan(new DefaultSpan(spanContext), Context.current());

Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).setNoParent().startSpan();
assertThat(span.getContext()).isEqualTo(SpanContext.getInvalid());
}

@Test
void testSpanContextPropagationCurrentSpan() {
DefaultSpan parent = new DefaultSpan(spanContext);
Expand Down