Skip to content

Commit

Permalink
Handle null parent's SpanContextShim in the OT Shim. (open-telemetry#686
Browse files Browse the repository at this point in the history
)

SpanContextShim objects are created on demand,
as it's an expensive operation because of a global
write lock, hence we need to handle the case
of starting a Span whose parent has no SpanContextShim
yet.
  • Loading branch information
carlosalberto committed Dec 5, 2019
1 parent 74c6595 commit 2675c4f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ public Span start() {
builder.setNoParent();
} else if (parentSpan != null) {
builder.setParent(parentSpan.getSpan());
distContext = spanContextTable().get(parentSpan).getDistributedContext();
SpanContextShim contextShim = spanContextTable().get(parentSpan);
distContext = contextShim == null ? null : contextShim.getDistributedContext();
} else if (parentSpanContext != null) {
builder.setParent(parentSpanContext.getSpanContext());
distContext = parentSpanContext.getDistributedContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static io.opentelemetry.opentracingshim.TestUtils.getBaggageMap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import io.opentelemetry.sdk.distributedcontext.DistributedContextManagerSdk;
import io.opentelemetry.sdk.trace.TracerSdkFactory;
Expand Down Expand Up @@ -74,4 +75,22 @@ public void baggage_parentContext() {
parentSpan.finish();
}
}

@Test
public void parent_NullContextShim() {
/* SpanContextShim is null until Span.context() or Span.getBaggageItem() are called.
* Verify a null SpanContextShim in the parent is handled properly. */
SpanShim parentSpan = (SpanShim) new SpanBuilderShim(telemetryInfo, SPAN_NAME).start();
try {
SpanShim childSpan =
(SpanShim) new SpanBuilderShim(telemetryInfo, SPAN_NAME).asChildOf(parentSpan).start();
try {
assertFalse(childSpan.context().baggageItems().iterator().hasNext());
} finally {
childSpan.finish();
}
} finally {
parentSpan.finish();
}
}
}

0 comments on commit 2675c4f

Please sign in to comment.