Skip to content

Commit

Permalink
Merge pull request #2661 from DataDog/rgs/sampling-aware-scope-listeners
Browse files Browse the repository at this point in the history
don't notify scope listeners if trace is eligible for dropping
  • Loading branch information
richardstartin committed May 4, 2021
2 parents bf784c0 + c8eb1e7 commit dae508a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
11 changes: 11 additions & 0 deletions dd-trace-core/src/main/java/datadog/trace/core/DDSpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ public final DDSpan setResourceName(final CharSequence resourceName) {
return this;
}

@Override
public boolean eligibleForDropping() {
switch (context.getSamplingPriority()) {
case PrioritySampling.USER_DROP:
case PrioritySampling.SAMPLER_DROP:
return !context.getErrorFlag() & !forceKeep;
default:
return false;
}
}

/**
* Set the sampling priority of the root span of this span's trace
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private static final class ContinuableScope implements AgentScope {
/** Flag to propagate this scope across async boundaries. */
private boolean isAsyncPropagating;

private final byte source;
private byte flags;

private short referenceCount = 1;

Expand All @@ -194,7 +194,7 @@ private static final class ContinuableScope implements AgentScope {
this.span = span;
this.scopeManager = scopeManager;
this.continuation = continuation;
this.source = source;
this.flags = source;
}

@Override
Expand All @@ -210,7 +210,7 @@ public void close() {

scopeManager.statsDClient.incrementCounter("scope.close.error");

if (source == ScopeSource.MANUAL.id()) {
if (source() == ScopeSource.MANUAL.id()) {
scopeManager.statsDClient.incrementCounter("scope.user.close.error");

if (scopeManager.strictMode) {
Expand Down Expand Up @@ -247,7 +247,7 @@ final void onProperClose() {
}
}

if (span instanceof NoopAgentSpan) {
if (!notifiedOnActivate()) {
return;
}

Expand Down Expand Up @@ -297,7 +297,7 @@ public void setAsyncPropagation(final boolean value) {
@Override
public ContinuableScopeManager.Continuation capture() {
return isAsyncPropagating
? new SingleContinuation(scopeManager, span, source).register()
? new SingleContinuation(scopeManager, span, source()).register()
: null;
}

Expand All @@ -309,7 +309,7 @@ public ContinuableScopeManager.Continuation capture() {
@Override
public ContinuableScopeManager.Continuation captureConcurrent() {
return isAsyncPropagating
? new ConcurrentContinuation(scopeManager, span, source).register()
? new ConcurrentContinuation(scopeManager, span, source()).register()
: null;
}

Expand All @@ -327,9 +327,10 @@ public void afterActivated() {
}
}

if (span instanceof NoopAgentSpan) {
if (span.eligibleForDropping()) {
return;
}
flags |= 0x80;

for (final ExtendedScopeListener listener : scopeManager.extendedScopeListeners) {
try {
Expand All @@ -339,6 +340,14 @@ public void afterActivated() {
}
}
}

private byte source() {
return (byte) (flags & 0x7F);
}

private boolean notifiedOnActivate() {
return flags < 0;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public interface AgentSpan extends MutableSpan {
@Override
AgentSpan setResourceName(final CharSequence resourceName);

boolean eligibleForDropping();

interface Context {
DDId getTraceId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,11 @@ public AgentSpan setResourceName(final CharSequence resourceName) {
return this;
}

@Override
public boolean eligibleForDropping() {
return true;
}

@Override
public Integer getSamplingPriority() {
return (int) PrioritySampling.UNSET;
Expand Down

0 comments on commit dae508a

Please sign in to comment.