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:
parent
3884cc088e
commit
e08e3a787c
|
@ -619,7 +619,7 @@ public class DDTracer implements io.opentracing.Tracer, Closeable, datadog.trace
|
||||||
if (parentContext == null && !ignoreScope) {
|
if (parentContext == null && !ignoreScope) {
|
||||||
// use the Scope as parent unless overridden or ignored.
|
// use the Scope as parent unless overridden or ignored.
|
||||||
final Scope scope = scopeManager.active();
|
final Scope scope = scopeManager.active();
|
||||||
if (scope != null) {
|
if (scope != null && scope.span() != null) {
|
||||||
parentContext = scope.span().context();
|
parentContext = scope.span().context();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ public class ContinuableScope implements Scope, TraceScope {
|
||||||
this.openCount = openCount;
|
this.openCount = openCount;
|
||||||
this.continuation = continuation;
|
this.continuation = continuation;
|
||||||
this.spanUnderScope = spanUnderScope;
|
this.spanUnderScope = spanUnderScope;
|
||||||
this.finishOnClose = finishOnClose;
|
this.finishOnClose = finishOnClose && spanUnderScope != null;
|
||||||
toRestore = scopeManager.tlsScope.get();
|
toRestore = scopeManager.tlsScope.get();
|
||||||
scopeManager.tlsScope.set(this);
|
scopeManager.tlsScope.set(this);
|
||||||
for (final ScopeListener listener : scopeManager.scopeListeners) {
|
for (final ScopeListener listener : scopeManager.scopeListeners) {
|
||||||
|
|
|
@ -17,8 +17,8 @@ public class SimpleScope implements Scope {
|
||||||
final boolean finishOnClose) {
|
final boolean finishOnClose) {
|
||||||
this.scopeManager = scopeManager;
|
this.scopeManager = scopeManager;
|
||||||
this.spanUnderScope = spanUnderScope;
|
this.spanUnderScope = spanUnderScope;
|
||||||
this.finishOnClose = finishOnClose;
|
this.finishOnClose = finishOnClose && spanUnderScope != null;
|
||||||
this.toRestore = scopeManager.tlsScope.get();
|
toRestore = scopeManager.tlsScope.get();
|
||||||
scopeManager.tlsScope.set(this);
|
scopeManager.tlsScope.set(this);
|
||||||
for (final ScopeListener listener : scopeManager.scopeListeners) {
|
for (final ScopeListener listener : scopeManager.scopeListeners) {
|
||||||
listener.afterScopeActivated();
|
listener.afterScopeActivated();
|
||||||
|
|
|
@ -6,6 +6,7 @@ import datadog.trace.api.Config
|
||||||
import datadog.trace.api.DDTags
|
import datadog.trace.api.DDTags
|
||||||
import datadog.trace.api.sampling.PrioritySampling
|
import datadog.trace.api.sampling.PrioritySampling
|
||||||
import datadog.trace.common.writer.ListWriter
|
import datadog.trace.common.writer.ListWriter
|
||||||
|
import io.opentracing.Scope
|
||||||
import spock.lang.Specification
|
import spock.lang.Specification
|
||||||
|
|
||||||
import static datadog.opentracing.DDSpanContext.ORIGIN_KEY
|
import static datadog.opentracing.DDSpanContext.ORIGIN_KEY
|
||||||
|
@ -173,6 +174,33 @@ class DDSpanBuilderTest extends Specification {
|
||||||
actualContext.getTraceId() == spanId
|
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"() {
|
def "should inherit the DD parent attributes"() {
|
||||||
setup:
|
setup:
|
||||||
def expectedName = "fakeName"
|
def expectedName = "fakeName"
|
||||||
|
|
|
@ -96,19 +96,19 @@ class ScopeManagerTest extends Specification {
|
||||||
def "sets parent as current upon close"() {
|
def "sets parent as current upon close"() {
|
||||||
setup:
|
setup:
|
||||||
def parentScope = tracer.buildSpan("parent").startActive(finishSpan)
|
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:
|
expect:
|
||||||
scopeManager.active() == childScope
|
scopeManager.active() == childScope
|
||||||
childScope.span().context().parentId == parentScope.span().context().spanId
|
nullChild || childScope.span().context().parentId == parentScope.span().context().spanId
|
||||||
childScope.span().context().trace == parentScope.span().context().trace
|
nullChild || childScope.span().context().trace == parentScope.span().context().trace
|
||||||
|
|
||||||
when:
|
when:
|
||||||
childScope.close()
|
childScope.close()
|
||||||
|
|
||||||
then:
|
then:
|
||||||
scopeManager.active() == parentScope
|
scopeManager.active() == parentScope
|
||||||
spanFinished(childScope.span()) == finishSpan
|
spanFinished(childScope.span()) == (nullChild ? null : finishSpan)
|
||||||
!spanFinished(parentScope.span())
|
!spanFinished(parentScope.span())
|
||||||
writer == []
|
writer == []
|
||||||
|
|
||||||
|
@ -116,13 +116,17 @@ class ScopeManagerTest extends Specification {
|
||||||
parentScope.close()
|
parentScope.close()
|
||||||
|
|
||||||
then:
|
then:
|
||||||
spanFinished(childScope.span()) == finishSpan
|
spanFinished(childScope.span()) == (nullChild ? null : finishSpan)
|
||||||
spanFinished(parentScope.span()) == finishSpan
|
spanFinished(parentScope.span()) == finishSpan
|
||||||
writer == [[parentScope.span(), childScope.span()]] || !finishSpan
|
writer == [[parentScope.span(), childScope.span()]] || !finishSpan || nullChild
|
||||||
scopeManager.active() == null
|
scopeManager.active() == null
|
||||||
|
|
||||||
where:
|
where:
|
||||||
finishSpan << [true, false]
|
finishSpan | nullChild
|
||||||
|
true | false
|
||||||
|
false | false
|
||||||
|
true | true
|
||||||
|
false | true
|
||||||
}
|
}
|
||||||
|
|
||||||
def "ContinuableScope only creates continuations when propagation is set"() {
|
def "ContinuableScope only creates continuations when propagation is set"() {
|
||||||
|
@ -567,8 +571,8 @@ class ScopeManagerTest extends Specification {
|
||||||
closedCount.get() == 4
|
closedCount.get() == 4
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean spanFinished(Span span) {
|
Boolean spanFinished(Span span) {
|
||||||
return ((DDSpan) span).isFinished()
|
return ((DDSpan) span)?.isFinished()
|
||||||
}
|
}
|
||||||
|
|
||||||
class AtomicReferenceScope extends AtomicReference<Span> implements ScopeContext, Scope {
|
class AtomicReferenceScope extends AtomicReference<Span> implements ScopeContext, Scope {
|
||||||
|
|
Loading…
Reference in New Issue