Improve AgentRunner and ConfigUtils class based on CR comments
MODIFY
dd-java-agent/instrumentation/trace-annotation/src/test/groovy/TraceAnnotationsTest.groovy
dd-java-agent/instrumentation/trace-annotation/src/test/groovy/TraceAnnotationsTest.groovy
@@ -13,7 +13,6 @@ class TraceAnnotationsTest extends AgentTestRunner {
ConfigUtils.updateConfig {
System.clearProperty("dd.trace.annotations")
}
- refreshTestTracer()
}
def "test simple case annotations"() {

MODIFY
dd-java-agent/testing/src/main/groovy/datadog/trace/agent/test/AgentTestRunner.java
dd-java-agent/testing/src/main/groovy/datadog/trace/agent/test/AgentTestRunner.java
@@ -64,12 +64,12 @@ public abstract class AgentTestRunner extends Specification {
*
* <p>Before the start of each test the reported traces will be reset.
*/
- public static ListWriter TEST_WRITER;
+ public static final ListWriter TEST_WRITER;
// 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.
- protected static Object TEST_TRACER;
+ protected static final Object TEST_TRACER;
protected static final Set<String> TRANSFORMED_CLASSES = Sets.newConcurrentHashSet();
private static final AtomicInteger INSTRUMENTATION_ERROR_COUNT = new AtomicInteger();
@@ -85,10 +85,6 @@ public abstract class AgentTestRunner extends Specification {
((Logger) LoggerFactory.getLogger("datadog")).setLevel(Level.DEBUG);
ConfigUtils.makeConfigInstanceModifiable();
- refreshTestTracer();
- }
-
- public static void refreshTestTracer() {
TEST_WRITER =
new ListWriter() {
@Override

MODIFY
dd-java-agent/testing/src/main/groovy/datadog/trace/agent/test/utils/ConfigUtils.groovy
dd-java-agent/testing/src/main/groovy/datadog/trace/agent/test/utils/ConfigUtils.groovy
@@ -56,11 +56,9 @@ class ConfigUtils {
}
/**
- * Calling will reset the runtimeId too, so it might cause problems around runtimeId verification.
- * If you are testing runtimeId provide <code>preserveRuntimeId = false</code> to copy the previous runtimeId
- * to the new config instance.
+ * Reset the global configuration. Please note that Runtime ID is preserved to the pre-existing value.
*/
- static void resetConfig(preserveRuntimeId = true) {
+ static void resetConfig() {
// Ensure the class was re-transformed properly in AgentTestRunner.makeConfigInstanceModifiable()
assert Modifier.isPublic(ConfigInstance.FIELD.getModifiers())
assert Modifier.isStatic(ConfigInstance.FIELD.getModifiers())
@@ -75,12 +73,19 @@ class ConfigUtils {
def previousConfig = ConfigInstance.FIELD.get(null)
def newConfig = new Config()
ConfigInstance.FIELD.set(null, newConfig)
- if (previousConfig != null && preserveRuntimeId) {
+ if (previousConfig != null) {
ConfigInstance.RUNTIME_ID_FIELD.set(newConfig, ConfigInstance.RUNTIME_ID_FIELD.get(previousConfig))
}
}
+ // Keep track of config instance already made modifiable
+ private static isConfigInstanceModifiable = false
+
static void makeConfigInstanceModifiable() {
+ if (isConfigInstanceModifiable) {
+ return
+ }
+
def instrumentation = ByteBuddyAgent.install()
final transformer =
new AgentBuilder.Default()
@@ -90,7 +95,7 @@ class ConfigUtils {
.with(
new AgentBuilder.LocationStrategy.Simple(
ClassFileLocator.ForClassLoader.ofSystemLoader()))
- .ignore(none()) // Allow transforming boostrap classes
+ .ignore(none()) // Allow transforming bootstrap classes
.type(named("datadog.trace.api.Config"))
.transform { builder, typeDescription, classLoader, module ->
builder
@@ -104,6 +109,7 @@ class ConfigUtils {
.transform(Transformer.ForField.withModifiers(PUBLIC, VOLATILE))
}
.installOn(instrumentation)
+ isConfigInstanceModifiable = true
final field = ConfigInstance.FIELD
assert Modifier.isPublic(field.getModifiers())

Improve AgentRunner and ConfigUtils class based on CR comments