Skip to content

Remove ScopeSource enum #8565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import datadog.trace.api.DDSpanId
import datadog.trace.api.DDTraceId
import datadog.trace.api.sampling.PrioritySampling
import datadog.trace.api.datastreams.NoopPathwayContext
import datadog.trace.bootstrap.instrumentation.api.ScopeSource
import datadog.trace.core.DDSpan
import datadog.trace.core.DDSpanContext
import datadog.trace.core.PendingTrace
Expand Down Expand Up @@ -52,8 +51,8 @@ class TypeConverterTest extends AgentTestRunner {
def context = createTestSpanContext()
def span1 = new DDSpan("test", 0, context, null)
def span2 = new DDSpan("test", 0, context, null)
def scope1 = scopeManager.activate(span1, ScopeSource.MANUAL)
def scope2 = scopeManager.activate(span2, ScopeSource.MANUAL)
def scope1 = scopeManager.activateManualSpan(span1)
def scope2 = scopeManager.activateManualSpan(span2)
expect:
// return the same wrapper for the same scope
typeConverter.toScope(scope1) is typeConverter.toScope(scope1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import datadog.trace.api.DDSpanId
import datadog.trace.api.DDTraceId
import datadog.trace.api.sampling.PrioritySampling
import datadog.trace.api.datastreams.NoopPathwayContext
import datadog.trace.bootstrap.instrumentation.api.ScopeSource
import datadog.trace.core.DDSpan
import datadog.trace.core.DDSpanContext
import datadog.trace.core.PendingTrace
Expand Down Expand Up @@ -57,8 +56,8 @@ class TypeConverterTest extends AgentTestRunner {
def context = createTestSpanContext()
def span1 = new DDSpan("test", 0, context, null)
def span2 = new DDSpan("test", 0, context, null)
def scope1 = scopeManager.activate(span1, ScopeSource.MANUAL)
def scope2 = scopeManager.activate(span2, ScopeSource.MANUAL)
def scope1 = scopeManager.activateManualSpan(span1)
def scope2 = scopeManager.activateManualSpan(span2)
expect:
// return the same wrapper for the same scope
typeConverter.toScope(scope1, true) is typeConverter.toScope(scope1, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import datadog.trace.api.DDSpanId
import datadog.trace.api.DDTraceId
import datadog.trace.api.sampling.PrioritySampling
import datadog.trace.api.datastreams.NoopPathwayContext
import datadog.trace.bootstrap.instrumentation.api.ScopeSource
import datadog.trace.core.DDSpan
import datadog.trace.core.DDSpanContext
import datadog.trace.core.PendingTrace
Expand Down Expand Up @@ -57,8 +56,8 @@ class TypeConverterTest extends AgentTestRunner {
def context = createTestSpanContext()
def span1 = new DDSpan("test", 0, context, null)
def span2 = new DDSpan("test", 0, context, null)
def scope1 = scopeManager.activate(span1, ScopeSource.MANUAL)
def scope2 = scopeManager.activate(span2, ScopeSource.MANUAL)
def scope1 = scopeManager.activateManualSpan(span1)
def scope2 = scopeManager.activateManualSpan(span2)
expect:
// return the same wrapper for the same scope
typeConverter.toScope(scope1, true) is typeConverter.toScope(scope1, true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package datadog.trace.core;

import static datadog.communication.monitor.DDAgentStatsDClientManager.statsDClientManager;
import static datadog.trace.api.ConfigDefaults.DEFAULT_ASYNC_PROPAGATING;
import static datadog.trace.api.DDTags.DJM_ENABLED;
import static datadog.trace.api.DDTags.DSM_ENABLED;
import static datadog.trace.api.DDTags.PROFILING_CONTEXT_ENGINE;
Expand Down Expand Up @@ -61,7 +60,6 @@
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.BlackHoleSpan;
import datadog.trace.bootstrap.instrumentation.api.ProfilingContextIntegration;
import datadog.trace.bootstrap.instrumentation.api.ScopeSource;
import datadog.trace.bootstrap.instrumentation.api.ScopeState;
import datadog.trace.bootstrap.instrumentation.api.TagContext;
import datadog.trace.civisibility.interceptor.CiVisibilityApmProtocolInterceptor;
Expand Down Expand Up @@ -908,12 +906,12 @@ public AgentSpan startSpan(

@Override
public AgentScope activateSpan(AgentSpan span) {
return scopeManager.activate(span, ScopeSource.INSTRUMENTATION, DEFAULT_ASYNC_PROPAGATING);
return scopeManager.activateSpan(span);
}

@Override
public AgentScope activateManualSpan(final AgentSpan span) {
return scopeManager.activate(span, ScopeSource.MANUAL /* inherit async propagation flag */);
return scopeManager.activateManualSpan(span);
}

@Override
Expand All @@ -923,7 +921,7 @@ public AgentScope.Continuation captureActiveSpan() {

@Override
public AgentScope.Continuation captureSpan(final AgentSpan span) {
return scopeManager.captureSpan(span, ScopeSource.INSTRUMENTATION.id());
return scopeManager.captureSpan(span);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void onFinishSpan() {}

public void onCreateTrace() {}

public void onScopeCloseError(int scopeSource) {}
public void onScopeCloseError(boolean manual) {}

public void onCaptureContinuation() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import datadog.trace.api.StatsDClient;
import datadog.trace.api.cache.RadixTreeCache;
import datadog.trace.bootstrap.instrumentation.api.ScopeSource;
import datadog.trace.common.writer.RemoteApi;
import datadog.trace.core.DDSpan;
import datadog.trace.util.AgentTaskScheduler;
Expand Down Expand Up @@ -282,9 +281,9 @@ public void onCreateTrace() {
}

@Override
public void onScopeCloseError(int scopeSource) {
public void onScopeCloseError(boolean manual) {
scopeCloseErrors.inc();
if (scopeSource == ScopeSource.MANUAL.id()) {
if (manual) {
userScopeCloseErrors.inc();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AttachableWrapper;
import datadog.trace.bootstrap.instrumentation.api.ScopeSource;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import javax.annotation.Nonnull;

class ContinuableScope implements AgentScope, AttachableWrapper {

// different sources of scopes
static final byte INSTRUMENTATION = 0;
static final byte MANUAL = 1;
static final byte ITERATION = 2;

private final ContinuableScopeManager scopeManager;

final AgentSpan span; // package-private so scopeManager can access it directly
Expand Down Expand Up @@ -58,8 +63,8 @@ public final void close() {
}

byte source = source();
scopeManager.healthMetrics.onScopeCloseError(source);
if (source == ScopeSource.MANUAL.id() && scopeManager.strictMode) {
scopeManager.healthMetrics.onScopeCloseError(source == MANUAL);
if (source == MANUAL && scopeManager.strictMode) {
throw new RuntimeException("Tried to close " + span + " scope when not on top");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import static datadog.trace.api.ConfigDefaults.DEFAULT_ASYNC_PROPAGATING;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopScope;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan;
import static datadog.trace.core.scopemanager.ContinuableScope.INSTRUMENTATION;
import static datadog.trace.core.scopemanager.ContinuableScope.ITERATION;
import static datadog.trace.core.scopemanager.ContinuableScope.MANUAL;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
Expand All @@ -16,7 +19,6 @@
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.ProfilerContext;
import datadog.trace.bootstrap.instrumentation.api.ProfilingContextIntegration;
import datadog.trace.bootstrap.instrumentation.api.ScopeSource;
import datadog.trace.bootstrap.instrumentation.api.ScopeState;
import datadog.trace.bootstrap.instrumentation.api.ScopeStateAware;
import datadog.trace.core.monitor.HealthMetrics;
Expand All @@ -39,6 +41,7 @@
* ScopeInterceptors to provide additional functionality.
*/
public final class ContinuableScopeManager implements ScopeStateAware {

static final Logger log = LoggerFactory.getLogger(ContinuableScopeManager.class);
static final RatelimitedLogger ratelimitedLog = new RatelimitedLogger(log, 1, MINUTES);
static final long iterationKeepAlive =
Expand Down Expand Up @@ -82,13 +85,12 @@ public ContinuableScopeManager(
this.profilingContextIntegration = profilingContextIntegration;
}

public AgentScope activate(final AgentSpan span, final ScopeSource source) {
return activate(span, source.id(), false, /* ignored */ false);
public AgentScope activateSpan(final AgentSpan span) {
return activate(span, INSTRUMENTATION, true, DEFAULT_ASYNC_PROPAGATING);
}

public AgentScope activate(
final AgentSpan span, final ScopeSource source, boolean isAsyncPropagating) {
return activate(span, source.id(), true, isAsyncPropagating);
public AgentScope activateManualSpan(final AgentSpan span) {
return activate(span, MANUAL, false, /* ignored */ false);
}

public AgentScope.Continuation captureActiveSpan() {
Expand All @@ -100,7 +102,11 @@ public AgentScope.Continuation captureActiveSpan() {
}
}

public AgentScope.Continuation captureSpan(final AgentSpan span, byte source) {
public AgentScope.Continuation captureSpan(final AgentSpan span) {
return captureSpan(span, INSTRUMENTATION);
}

private AgentScope.Continuation captureSpan(final AgentSpan span, byte source) {
ScopeContinuation continuation = new ScopeContinuation(this, span, source);
continuation.register();
healthMetrics.onCaptureContinuation();
Expand Down Expand Up @@ -193,7 +199,7 @@ public void closePrevious(final boolean finishSpan) {

// close any immediately previous iteration scope
final ContinuableScope top = scopeStack.top;
if (top != null && top.source() == ScopeSource.ITERATION.id()) {
if (top != null && top.source() == ITERATION) {
if (iterationKeepAlive > 0) { // skip depth check because cancelling is cheap
cancelRootIterationScopeCleanup(scopeStack, top);
}
Expand Down Expand Up @@ -223,8 +229,7 @@ public AgentScope activateNext(final AgentSpan span) {
boolean asyncPropagation = top != null ? top.isAsyncPropagating() : DEFAULT_ASYNC_PROPAGATING;

final ContinuableScope scope =
new ContinuableScope(
this, span, ScopeSource.ITERATION.id(), asyncPropagation, createScopeState(span));
new ContinuableScope(this, span, ITERATION, asyncPropagation, createScopeState(span));

if (iterationKeepAlive > 0 && currentDepth == 0) {
// no surrounding scope to aid cleanup, so use background task instead
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package datadog.trace.core.scopemanager;

import static datadog.trace.core.scopemanager.ContinuableScope.ITERATION;

import datadog.trace.bootstrap.instrumentation.api.ProfilingContextIntegration;
import datadog.trace.bootstrap.instrumentation.api.ScopeSource;
import java.util.ArrayDeque;

/**
Expand Down Expand Up @@ -77,7 +78,7 @@ boolean checkTop(final ContinuableScope expectedScope) {
*/
final boolean checkOverdueScopes(final ContinuableScope expectedScope) {
// we already know 'top' isn't the expected scope, so just need to check its source
if (top == null || top.source() != ScopeSource.ITERATION.id()) {
if (top == null || top.source() != ITERATION) {
return false;
}
// avoid calling close() as we're already in that method, instead just clear any
Expand All @@ -86,7 +87,7 @@ final boolean checkOverdueScopes(final ContinuableScope expectedScope) {
top.span.finishWithEndToEnd();
// now do the same for any previous iteration scopes ahead of the expected scope
for (ContinuableScope scope : stack) {
if (scope.source() != ScopeSource.ITERATION.id()) {
if (scope.source() != ITERATION) {
return expectedScope.equals(scope);
} else {
scope.clearReferences();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import datadog.trace.api.DDTraceId
import datadog.trace.api.flare.TracerFlare
import datadog.trace.api.time.SystemTimeSource
import datadog.trace.api.datastreams.NoopPathwayContext
import datadog.trace.bootstrap.instrumentation.api.ScopeSource
import datadog.trace.context.TraceScope
import datadog.trace.core.monitor.HealthMetrics
import datadog.trace.core.propagation.PropagationTags
Expand Down Expand Up @@ -495,8 +494,8 @@ class PendingTraceBufferTest extends DDSpecification {


def addContinuation(DDSpan span) {
def scope = scopeManager.activate(span, ScopeSource.INSTRUMENTATION, true)
continuations << scopeManager.captureSpan(span, ScopeSource.INSTRUMENTATION.id())
def scope = scopeManager.activateSpan(span)
continuations << scopeManager.captureSpan(span)
scope.close()
return span
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package datadog.trace.core.monitor

import datadog.trace.api.StatsDClient
import datadog.trace.api.sampling.PrioritySampling
import datadog.trace.bootstrap.instrumentation.api.ScopeSource
import datadog.trace.common.writer.RemoteApi
import datadog.trace.common.writer.RemoteWriter
import spock.lang.Ignore
Expand Down Expand Up @@ -365,21 +364,21 @@ class HealthMetricsTest extends Specification {
}
def "test onScopeCloseError"() {
setup:
def latch = new CountDownLatch(1 + (source == ScopeSource.MANUAL ? 1 : 0))
def latch = new CountDownLatch(1 + (manual ? 1 : 0))
def healthMetrics = new TracerHealthMetrics(new Latched(statsD, latch), 100, TimeUnit.MILLISECONDS)
healthMetrics.start()
when:
healthMetrics.onScopeCloseError(source.id())
healthMetrics.onScopeCloseError(manual)
latch.await(5, TimeUnit.SECONDS)
then:
1 * statsD.count("scope.close.error", 1, _)
if (source == ScopeSource.MANUAL) {
if (manual) {
1 * statsD.count("scope.user.close.error", 1, _)
}
cleanup:
healthMetrics.close()
where:
source << ScopeSource.values()
manual << [false, true]
}
def "test onScopeStackOverflow"() {
setup:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package datadog.trace.core.scopemanager
import datadog.trace.api.config.TracerConfig
import datadog.trace.bootstrap.instrumentation.api.AgentScope
import datadog.trace.bootstrap.instrumentation.api.AgentSpan
import datadog.trace.bootstrap.instrumentation.api.ScopeSource
import datadog.trace.common.writer.ListWriter
import datadog.trace.core.test.DDCoreSpecification

Expand Down Expand Up @@ -35,7 +34,7 @@ class ScopeManagerDepthTest extends DDCoreSpecification {
scope == noopScope()

when: "activate a noop scope over the limit"
scope = scopeManager.activate(noopSpan(), ScopeSource.MANUAL)
scope = scopeManager.activateManualSpan(noopSpan())

then: "still have a noop instance"
scope == noopScope()
Expand Down Expand Up @@ -77,7 +76,7 @@ class ScopeManagerDepthTest extends DDCoreSpecification {
scopeManager.scopeStack().depth() == defaultLimit + 1

when: "activate a noop span"
scope = scopeManager.activate(noopSpan(), ScopeSource.MANUAL)
scope = scopeManager.activateManualSpan(noopSpan())

then: "a real instance is still returned"
scope != noopScope()
Expand Down
Loading
Loading