Allow null to be set in Scope for Span

This is useful to temporarily remove a trace from scope for a defined period.
This commit is contained in:
Tyler Benson 2019-07-15 19:11:51 -07:00
parent 3884cc088e
commit e08e3a787c
5 changed files with 45 additions and 13 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) {
if (scope != null && scope.span() != null) {
parentContext = scope.span().context();
}
}

View File

@ -48,7 +48,7 @@ public class ContinuableScope implements Scope, TraceScope {
this.openCount = openCount;
this.continuation = continuation;
this.spanUnderScope = spanUnderScope;
this.finishOnClose = finishOnClose;
this.finishOnClose = finishOnClose && spanUnderScope != null;
toRestore = scopeManager.tlsScope.get();
scopeManager.tlsScope.set(this);
for (final ScopeListener listener : scopeManager.scopeListeners) {

View File

@ -17,8 +17,8 @@ public class SimpleScope implements Scope {
final boolean finishOnClose) {
this.scopeManager = scopeManager;
this.spanUnderScope = spanUnderScope;
this.finishOnClose = finishOnClose;
this.toRestore = scopeManager.tlsScope.get();
this.finishOnClose = finishOnClose && spanUnderScope != null;
toRestore = scopeManager.tlsScope.get();
scopeManager.tlsScope.set(this);
for (final ScopeListener listener : scopeManager.scopeListeners) {
listener.afterScopeActivated();

View File

@ -6,6 +6,7 @@ import datadog.trace.api.Config
import datadog.trace.api.DDTags
import datadog.trace.api.sampling.PrioritySampling
import datadog.trace.common.writer.ListWriter
import io.opentracing.Scope
import spock.lang.Specification
import static datadog.opentracing.DDSpanContext.ORIGIN_KEY
@ -173,6 +174,33 @@ class DDSpanBuilderTest extends Specification {
actualContext.getTraceId() == spanId
}
def "should link to parent span implicitly"() {
setup:
final Scope parent = nullParent ?
tracer.scopeManager().activate(null, false) :
tracer.buildSpan("parent")
.startActive(false)
final String expectedParentId = nullParent ? "0" : parent.span().context().getSpanId()
final String expectedName = "fakeName"
final DDSpan span = tracer
.buildSpan(expectedName)
.start()
final DDSpanContext actualContext = span.context()
expect:
actualContext.getParentId() == expectedParentId
cleanup:
parent.close()
where:
nullParent << [false, true]
}
def "should inherit the DD parent attributes"() {
setup:
def expectedName = "fakeName"

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 = tracer.buildSpan("parent").startActive(finishSpan)
def childScope = nullChild ? tracer.scopeManager().activate(null, finishSpan) : tracer.buildSpan("parent").startActive(finishSpan)
expect:
scopeManager.active() == childScope
childScope.span().context().parentId == parentScope.span().context().spanId
childScope.span().context().trace == parentScope.span().context().trace
nullChild || childScope.span().context().parentId == parentScope.span().context().spanId
nullChild || childScope.span().context().trace == parentScope.span().context().trace
when:
childScope.close()
then:
scopeManager.active() == parentScope
spanFinished(childScope.span()) == finishSpan
spanFinished(childScope.span()) == (nullChild ? null : finishSpan)
!spanFinished(parentScope.span())
writer == []
@ -116,13 +116,17 @@ class ScopeManagerTest extends Specification {
parentScope.close()
then:
spanFinished(childScope.span()) == finishSpan
spanFinished(childScope.span()) == (nullChild ? null : finishSpan)
spanFinished(parentScope.span()) == finishSpan
writer == [[parentScope.span(), childScope.span()]] || !finishSpan
writer == [[parentScope.span(), childScope.span()]] || !finishSpan || nullChild
scopeManager.active() == null
where:
finishSpan << [true, false]
finishSpan | nullChild
true | false
false | false
true | true
false | true
}
def "ContinuableScope only creates continuations when propagation is set"() {
@ -567,8 +571,8 @@ class ScopeManagerTest extends Specification {
closedCount.get() == 4
}
boolean spanFinished(Span span) {
return ((DDSpan) span).isFinished()
Boolean spanFinished(Span span) {
return ((DDSpan) span)?.isFinished()
}
class AtomicReferenceScope extends AtomicReference<Span> implements ScopeContext, Scope {