Skip to content

Commit

Permalink
Fix noParent being ignored after explicit parent. (#1617)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oberon00 authored Sep 2, 2020
1 parent 225acce commit 8418753
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
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() {
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

0 comments on commit 8418753

Please sign in to comment.