diff --git a/dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java b/dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java index 487f53be66..74bf6129e7 100644 --- a/dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java +++ b/dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java @@ -619,7 +619,7 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace if (parentContext == null && !ignoreScope) { // use the Scope as parent unless overridden or ignored. final Scope scope = scopeManager.active(); - if (scope != null && scope.span() != null) { + if (scope != null) { parentContext = scope.span().context(); } } diff --git a/dd-trace-ot/src/main/java/datadog/opentracing/scopemanager/ContinuableScope.java b/dd-trace-ot/src/main/java/datadog/opentracing/scopemanager/ContinuableScope.java index 53a788dd9e..049a14ad77 100644 --- a/dd-trace-ot/src/main/java/datadog/opentracing/scopemanager/ContinuableScope.java +++ b/dd-trace-ot/src/main/java/datadog/opentracing/scopemanager/ContinuableScope.java @@ -44,11 +44,12 @@ public class ContinuableScope implements Scope, TraceScope { final Continuation continuation, final DDSpan spanUnderScope, final boolean finishOnClose) { + assert spanUnderScope != null : "span must not be null"; this.scopeManager = scopeManager; this.openCount = openCount; this.continuation = continuation; this.spanUnderScope = spanUnderScope; - this.finishOnClose = finishOnClose && spanUnderScope != null; + this.finishOnClose = finishOnClose; toRestore = scopeManager.tlsScope.get(); scopeManager.tlsScope.set(this); for (final ScopeListener listener : scopeManager.scopeListeners) { diff --git a/dd-trace-ot/src/main/java/datadog/opentracing/scopemanager/SimpleScope.java b/dd-trace-ot/src/main/java/datadog/opentracing/scopemanager/SimpleScope.java index d11194a9c9..3f97c8a2d7 100644 --- a/dd-trace-ot/src/main/java/datadog/opentracing/scopemanager/SimpleScope.java +++ b/dd-trace-ot/src/main/java/datadog/opentracing/scopemanager/SimpleScope.java @@ -15,9 +15,10 @@ public class SimpleScope implements Scope { final ContextualScopeManager scopeManager, final Span spanUnderScope, final boolean finishOnClose) { + assert spanUnderScope != null : "span must not be null"; this.scopeManager = scopeManager; this.spanUnderScope = spanUnderScope; - this.finishOnClose = finishOnClose && spanUnderScope != null; + this.finishOnClose = finishOnClose; toRestore = scopeManager.tlsScope.get(); scopeManager.tlsScope.set(this); for (final ScopeListener listener : scopeManager.scopeListeners) { diff --git a/dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanBuilderTest.groovy b/dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanBuilderTest.groovy index e7a6da24e2..86452c433d 100644 --- a/dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanBuilderTest.groovy +++ b/dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanBuilderTest.groovy @@ -7,6 +7,7 @@ import datadog.trace.api.DDTags import datadog.trace.api.sampling.PrioritySampling import datadog.trace.common.writer.ListWriter import io.opentracing.Scope +import io.opentracing.noop.NoopSpan import spock.lang.Specification import static datadog.opentracing.DDSpanContext.ORIGIN_KEY @@ -176,12 +177,12 @@ class DDSpanBuilderTest extends Specification { def "should link to parent span implicitly"() { setup: - final Scope parent = nullParent ? - tracer.scopeManager().activate(null, false) : + final Scope parent = noopParent ? + tracer.scopeManager().activate(NoopSpan.INSTANCE, false) : tracer.buildSpan("parent") .startActive(false) - final String expectedParentId = nullParent ? "0" : parent.span().context().getSpanId() + final String expectedParentId = noopParent ? "0" : parent.span().context().getSpanId() final String expectedName = "fakeName" @@ -198,7 +199,7 @@ class DDSpanBuilderTest extends Specification { parent.close() where: - nullParent << [false, true] + noopParent << [false, true] } def "should inherit the DD parent attributes"() { diff --git a/dd-trace-ot/src/test/groovy/datadog/opentracing/scopemanager/ScopeManagerTest.groovy b/dd-trace-ot/src/test/groovy/datadog/opentracing/scopemanager/ScopeManagerTest.groovy index 54afaade40..80e2eeb879 100644 --- a/dd-trace-ot/src/test/groovy/datadog/opentracing/scopemanager/ScopeManagerTest.groovy +++ b/dd-trace-ot/src/test/groovy/datadog/opentracing/scopemanager/ScopeManagerTest.groovy @@ -96,19 +96,19 @@ class ScopeManagerTest extends Specification { def "sets parent as current upon close"() { setup: def parentScope = tracer.buildSpan("parent").startActive(finishSpan) - def childScope = nullChild ? tracer.scopeManager().activate(null, finishSpan) : tracer.buildSpan("parent").startActive(finishSpan) + def childScope = noopChild ? tracer.scopeManager().activate(NoopSpan.INSTANCE, finishSpan) : tracer.buildSpan("parent").startActive(finishSpan) expect: scopeManager.active() == childScope - nullChild || childScope.span().context().parentId == parentScope.span().context().spanId - nullChild || childScope.span().context().trace == parentScope.span().context().trace + noopChild || childScope.span().context().parentId == parentScope.span().context().spanId + noopChild || childScope.span().context().trace == parentScope.span().context().trace when: childScope.close() then: scopeManager.active() == parentScope - spanFinished(childScope.span()) == (nullChild ? null : finishSpan) + noopChild || spanFinished(childScope.span()) == finishSpan !spanFinished(parentScope.span()) writer == [] @@ -116,13 +116,13 @@ class ScopeManagerTest extends Specification { parentScope.close() then: - spanFinished(childScope.span()) == (nullChild ? null : finishSpan) + noopChild || spanFinished(childScope.span()) == finishSpan spanFinished(parentScope.span()) == finishSpan - writer == [[parentScope.span(), childScope.span()]] || !finishSpan || nullChild + writer == [[parentScope.span(), childScope.span()]] || !finishSpan || noopChild scopeManager.active() == null where: - finishSpan | nullChild + finishSpan | noopChild true | false false | false true | true @@ -571,7 +571,7 @@ class ScopeManagerTest extends Specification { closedCount.get() == 4 } - Boolean spanFinished(Span span) { + boolean spanFinished(Span span) { return ((DDSpan) span)?.isFinished() }