Remove opentracing from TestRunner fields to support single test run
MODIFY
dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JerseyTest.groovy
dd-java-agent/instrumentation/jax-rs-annotations/src/test/groovy/JerseyTest.groovy
@@ -18,7 +18,7 @@ class JerseyTest extends AgentTestRunner {
def "test resource"() {
setup:
// start a trace because the test doesn't go through any servlet or other instrumentation.
- def scope = TEST_TRACER.buildSpan("test.span").startActive(true)
+ def scope = getTestTracer().buildSpan("test.span").startActive(true)
def response = resources.client().resource("/test/hello/bob").post(String)
scope.close()

MODIFY
dd-java-agent/instrumentation/kafka-streams-0.11/src/test/groovy/KafkaStreamsTest.groovy
dd-java-agent/instrumentation/kafka-streams-0.11/src/test/groovy/KafkaStreamsTest.groovy
@@ -62,7 +62,7 @@ class KafkaStreamsTest extends AgentTestRunner {
@Override
void onMessage(ConsumerRecord<String, String> record) {
WRITER_PHASER.arriveAndAwaitAdvance() // ensure consistent ordering of traces
- TEST_TRACER.activeSpan().setTag("testing", 123)
+ getTestTracer().activeSpan().setTag("testing", 123)
records.add(record)
}
})
@@ -81,7 +81,7 @@ class KafkaStreamsTest extends AgentTestRunner {
@Override
String apply(String textLine) {
WRITER_PHASER.arriveAndAwaitAdvance() // ensure consistent ordering of traces
- TEST_TRACER.activeSpan().setTag("asdf", "testing")
+ getTestTracer().activeSpan().setTag("asdf", "testing")
return textLine.toLowerCase()
}
})

MODIFY
dd-java-agent/testing/src/main/java/datadog/trace/agent/test/AgentTestRunner.java
dd-java-agent/testing/src/main/java/datadog/trace/agent/test/AgentTestRunner.java
@@ -7,6 +7,7 @@ import datadog.opentracing.DDTracer;
import datadog.trace.agent.tooling.AgentInstaller;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.common.writer.ListWriter;
+import datadog.trace.common.writer.Writer;
import io.opentracing.Tracer;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
@@ -52,7 +53,10 @@ public abstract class AgentTestRunner extends Specification {
*/
public static final ListWriter TEST_WRITER;
- protected static final Tracer TEST_TRACER;
+ // having a reference to io.opentracing.Tracer in test field
+ // loads opentracing before bootstrap classpath is setup
+ // so we declare tracer as an object and cast when needed.
+ private static final Object TEST_TRACER;
private static final AtomicInteger INSTRUMENTATION_ERROR_COUNT = new AtomicInteger();
private static final Instrumentation instrumentation;
@@ -77,7 +81,15 @@ public abstract class AgentTestRunner extends Specification {
}
};
TEST_TRACER = new DDTracer(TEST_WRITER);
- TestUtils.registerOrReplaceGlobalTracer(TEST_TRACER);
+ TestUtils.registerOrReplaceGlobalTracer((Tracer) TEST_TRACER);
+ }
+
+ protected static Tracer getTestTracer() {
+ return (Tracer) TEST_TRACER;
+ }
+
+ protected static Writer getTestWriter() {
+ return TEST_WRITER;
}
@BeforeClass
@@ -100,7 +112,7 @@ public abstract class AgentTestRunner extends Specification {
public void beforeTest() {
TEST_WRITER.start();
INSTRUMENTATION_ERROR_COUNT.set(0);
- assert (TEST_TRACER).activeSpan() == null;
+ assert getTestTracer().activeSpan() == null;
}
@After

MODIFY
dd-java-agent/testing/src/test/groovy/AgentTestRunnerTest.groovy
dd-java-agent/testing/src/test/groovy/AgentTestRunnerTest.groovy
import datadog.trace.agent.test.TestUtils
+import io.opentracing.Tracer
import java.lang.reflect.Field
@@ -8,6 +9,8 @@ class AgentTestRunnerTest extends AgentTestRunner {
private static final ClassLoader BOOTSTRAP_CLASSLOADER = null
private static final ClassLoader OT_LOADER
private static final boolean AGENT_INSTALLED_IN_CLINIT
+ // having opentracing class in test field should not cause problems
+ private static final Tracer A_TRACER = null
static {
// when test class initializes, opentracing should be set up, but not the agent.
@@ -17,9 +20,10 @@ class AgentTestRunnerTest extends AgentTestRunner {
def "classpath setup"() {
expect:
+ A_TRACER == null
OT_LOADER == BOOTSTRAP_CLASSLOADER
!AGENT_INSTALLED_IN_CLINIT
- TEST_TRACER == TestUtils.getUnderlyingGlobalTracer()
+ getTestTracer() == TestUtils.getUnderlyingGlobalTracer()
getAgentTransformer() != null
datadog.trace.api.Trace.getClassLoader() == BOOTSTRAP_CLASSLOADER
}

Remove opentracing from TestRunner fields to support single test run