Assert span not null and use NoopSpan instead.

This commit is contained in:
Tyler Benson 2019-07-18 12:38:49 -07:00
parent e08e3a787c
commit 17d26a4934
5 changed files with 18 additions and 15 deletions

View File

@ -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();
}
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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"() {

View File

@ -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()
}