Skip to content
Open
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 @@ -46,4 +46,13 @@ static void register(ContextBinder binder) {
static boolean allowTesting() {
return TestContextBinder.register();
}

/**
* Clears any custom {@link ContextBinder} registered with {@link #register(ContextBinder)}.
*
* @see #allowTesting()
*/
static void resetForTesting() {
ContextProviders.customBinder = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,13 @@ static void register(ContextManager manager) {
static boolean allowTesting() {
return TestContextManager.register();
}

/**
* Clears any custom {@link ContextManager} registered with {@link #register(ContextManager)}.
*
* @see #allowTesting()
*/
static void resetForTesting() {
ContextProviders.customManager = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Context detachFrom(Object carrier) {

private static ContextBinder delegate() {
ContextBinder delegate = ContextProviders.customBinder;
if (delegate == TEST_INSTANCE) {
if (delegate == TEST_INSTANCE || delegate == null) {
// fall back to default context binder
return WeakMapContextBinder.INSTANCE;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static void clearListeners() {

private static ContextManager delegate() {
ContextManager delegate = ContextProviders.customManager;
if (delegate == TEST_INSTANCE) {
if (delegate == TEST_INSTANCE || delegate == null) {
// fall back to default context manager
return ThreadLocalContextManager.INSTANCE;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,90 @@ public void addListener(@Nonnull ContextListener listener) {}
// NOOP manager, no events emitted
listener.assertNoEvents();
}

@Test
void testResetBinder() {
assertTrue(ContextBinder.allowTesting());

Context context = root().with(STRING_KEY, "value");
Object carrier = new Object();

// register a NOOP context binder
ContextBinder.register(
new ContextBinder() {
@Override
public Context from(@Nonnull Object carrier) {
return root();
}

@Override
public void attachTo(@Nonnull Object carrier, @Nonnull Context context) {
// no-op
}

@Override
public Context detachFrom(@Nonnull Object carrier) {
return root();
}
});

// NOOP binder, context will always be root
context.attachTo(carrier);
assertSame(root(), Context.from(carrier));

// reset back to the default binder
ContextBinder.resetForTesting();

context.attachTo(carrier);
assertSame(context, Context.from(carrier));
assertSame(context, Context.detachFrom(carrier));
assertSame(root(), Context.from(carrier));
}

@Test
void testResetManager() {
assertTrue(ContextManager.allowTesting());

Context context = root().with(STRING_KEY, "value");

// register a NOOP context manager
ContextManager.register(
new ContextManager() {
@Override
public Context current() {
return root();
}

@Override
public ContextScope attach(@Nonnull Context context) {
return new NoopContextScope(root());
}

@Override
public Context swap(@Nonnull Context context) {
return root();
}

@Override
public ContextContinuation capture(@Nonnull Context context) {
return new NoopContextContinuation(root());
}

@Override
public void addListener(@Nonnull ContextListener listener) {}
});

// NOOP manager, context will always be root
try (ContextScope scope = context.attach()) {
assertSame(root(), Context.current());
}

// reset back to the default manager
ContextManager.resetForTesting();

try (ContextScope scope = context.attach()) {
assertSame(context, scope.context());
assertSame(context, Context.current());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import datadog.instrument.utils.ClassLoaderValue;
import datadog.metrics.api.statsd.StatsDClientManager;
import datadog.trace.api.Config;
import datadog.trace.api.InstrumenterConfig;
import datadog.trace.api.Platform;
import datadog.trace.api.WithGlobalTracer;
import datadog.trace.api.appsec.AppSecEventTracker;
Expand Down Expand Up @@ -337,6 +338,10 @@ public static void start(
StaticEventLogger.end("crashtracking");
}

if (InstrumenterConfig.get().isLegacyContextManagerEnabled()) {
AgentTracer.installLegacyContextManager();
}
Comment thread
mcculls marked this conversation as resolved.

startDatadogAgent(initTelemetry, inst);

final EnumSet<Library> libraries = detectLibraries(log);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import datadog.trace.agent.tooling.MeterInstaller;
import datadog.trace.agent.tooling.ProfilerInstaller;
import datadog.trace.agent.tooling.TracerInstaller;
import datadog.trace.api.InstrumenterConfig;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.ProfilingContextIntegration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -18,6 +20,10 @@ public final class TracerActivation {

public static void activate() {
try {
// Bind legacy context manager (if enabled)
if (InstrumenterConfig.get().isLegacyContextManagerEnabled()) {
AgentTracer.installLegacyContextManager();
}
// Initialize meter
MeterInstaller.installMeter();
// Initialize tracer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.datadog.debugger.sink.DebuggerSink
import com.datadog.debugger.sink.ProbeStatusSink
import com.google.common.collect.Sets
import datadog.communication.ddagent.DDAgentFeaturesDiscovery
import datadog.context.ContextManager
import datadog.instrument.classinject.ClassInjector
import datadog.metrics.agent.AgentMeter
import datadog.metrics.api.Monitoring
Expand All @@ -43,6 +44,7 @@ import datadog.trace.agent.tooling.bytebuddy.matcher.ClassLoaderMatchers
import datadog.trace.agent.tooling.bytebuddy.matcher.GlobalIgnores
import datadog.trace.api.Config
import datadog.trace.api.IdGenerationStrategy
import datadog.trace.api.InstrumenterConfig
import datadog.trace.api.Pair
import datadog.trace.api.ProcessTags
import datadog.trace.api.TraceConfig
Expand Down Expand Up @@ -351,18 +353,24 @@ abstract class InstrumentationSpecification extends DDSpecification implements A
void setupSpec() {
InstrumentationErrors.resetErrors()

AgentMeter.registerIfAbsent(
STATS_D_CLIENT,
new MonitoringImpl(STATS_D_CLIENT, 10, TimeUnit.SECONDS),
DDSketchHistograms.FACTORY
)

// If this fails, it's likely the result of another test loading Config before it can be
// injected into the bootstrap classpath. If one test extends AgentTestRunner in a module, all tests must extend
assert Config.getClassLoader() == null: "Config must load on the bootstrap classpath."

configurePreAgent()

if (InstrumenterConfig.get().isLegacyContextManagerEnabled()) {
AgentTracer.installLegacyContextManager()
Comment thread
mcculls marked this conversation as resolved.
} else {
ContextManager.resetForTesting()
}
Comment thread
mcculls marked this conversation as resolved.

AgentMeter.registerIfAbsent(
STATS_D_CLIENT,
new MonitoringImpl(STATS_D_CLIENT, 10, TimeUnit.SECONDS),
DDSketchHistograms.FACTORY
)

TEST_DATA_STREAMS_WRITER = new RecordingDatastreamsPayloadWriter()
DDAgentFeaturesDiscovery features = new MockFeaturesDiscovery(true)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import datadog.context.ContextManager;
import datadog.instrument.classinject.ClassInjector;
import datadog.trace.agent.test.assertions.TraceAssertions;
import datadog.trace.agent.test.assertions.TraceMatcher;
Expand All @@ -13,8 +14,10 @@
import datadog.trace.agent.tooling.bytebuddy.matcher.ClassLoaderMatchers;
import datadog.trace.api.Config;
import datadog.trace.api.IdGenerationStrategy;
import datadog.trace.api.InstrumenterConfig;
import datadog.trace.bootstrap.InstrumentationErrors;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer.TracerAPI;
import datadog.trace.common.writer.ListWriter;
import datadog.trace.core.CoreTracer;
Expand Down Expand Up @@ -75,6 +78,12 @@ static void initAll() {
// injected into the bootstrap classpath.
assertNull(Config.class.getClassLoader(), "Config must load on the bootstrap classpath.");

if (InstrumenterConfig.get().isLegacyContextManagerEnabled()) {
AgentTracer.installLegacyContextManager();
} else {
ContextManager.resetForTesting();
}

// Create shared test writer and tracer
writer = new ListWriter();
CoreTracer coreTracer =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider;
import software.amazon.awssdk.core.SdkBytes;
Expand Down Expand Up @@ -529,6 +530,7 @@ boolean isDataStreamsEnabled() {
}
}

@Disabled("TODO(APMLP-829): non-legacy ContextManager migration still in-progress")
@WithConfig(key = GeneralConfig.DATA_STREAMS_ENABLED, value = "false")
@WithConfig(key = TraceInstrumentationConfig.LEGACY_CONTEXT_MANAGER_ENABLED, value = "false")
class SqsClientV0ContextSwapForkedTest extends SqsClientV0TestBase {}
Expand Down Expand Up @@ -756,6 +758,7 @@ class SqsClientV0ReceiveIterationTest extends SqsClientV0ReceiveIterationTestBas
@WithConfig(key = GeneralConfig.DATA_STREAMS_ENABLED, value = "true")
class SqsClientV0DataStreamsReceiveIterationTest extends SqsClientV0ReceiveIterationTestBase {}

@Disabled("TODO(APMLP-829): non-legacy ContextManager migration still in-progress")
@WithConfig(key = GeneralConfig.DATA_STREAMS_ENABLED, value = "false")
@WithConfig(key = TraceInstrumentationConfig.LEGACY_CONTEXT_MANAGER_ENABLED, value = "false")
class SqsClientV0ContextSwapReceiveIterationForkedTest extends SqsClientV0ReceiveIterationTestBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import javax.jms.TopicSession
import jms10mock.Jms10ConnectionFactory
import org.apache.activemq.command.ActiveMQTextMessage
import org.apache.activemq.junit.EmbeddedActiveMQBroker
import spock.lang.Ignore
import spock.lang.Shared

abstract class JMS1Test extends VersionedNamingTestBase {
Expand Down Expand Up @@ -1061,6 +1062,7 @@ class JMS1V0Test extends JMS1Test {
}
}

@Ignore("TODO(APMLP-829): non-legacy ContextManager migration still in-progress")
class JMSContextSwapForkedTest extends JMS1V0Test {
@Override
protected void configurePreAgent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import org.springframework.kafka.listener.MessageListener
import org.springframework.kafka.test.rule.KafkaEmbedded
import org.springframework.kafka.test.utils.ContainerTestUtils
import org.springframework.kafka.test.utils.KafkaTestUtils
import spock.lang.Ignore
import spock.lang.Shared

import java.util.concurrent.ExecutionException
Expand Down Expand Up @@ -1545,6 +1546,7 @@ class KafkaClientDataStreamsDisabledForkedTest extends KafkaClientTestBase {
}
}

@Ignore("TODO(APMLP-829): non-legacy ContextManager migration still in-progress")
class KafkaClientContextSwapForkedTest extends KafkaClientV0ForkedTest {
void configurePreAgent() {
super.configurePreAgent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.isAsyncPropagationEnabled

import spock.lang.Ignore

abstract class KafkaClientTestBase extends VersionedNamingTestBase {
static final SHARED_TOPIC = "shared.topic"

Expand Down Expand Up @@ -1211,6 +1213,7 @@ class KafkaClientDataStreamsDisabledForkedTest extends KafkaClientTestBase {
}
}

@Ignore("TODO(APMLP-829): non-legacy ContextManager migration still in-progress")
class KafkaClientContextSwapForkedTest extends KafkaClientV0ForkedTest {
void configurePreAgent() {
super.configurePreAgent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,34 @@ public void setup() {
}

static ContextManager createManager(String type) {
return "Continuable".equals(type)
? new ContinuableScopeManager(0, false)
: ThreadLocalContextManager.INSTANCE;
if ("Continuable".equals(type)) {
ContinuableScopeManager csm = new ContinuableScopeManager(0, false);
return new ContextManager() {
@Override
public Context current() {
return csm.current();
}

@Override
public ContextScope attach(Context ctx) {
return csm.attach(ctx);
}

@Override
public Context swap(Context ctx) {
return csm.swap(ctx);
}

@Override
public ContextContinuation capture(Context ctx) {
return csm.capture(ctx);
}

@Override
public void addListener(ContextListener l) {}
};
}
return ThreadLocalContextManager.INSTANCE;
}

static Context[] createContexts() {
Expand Down
Loading