Move default instrumentation declarations to Instrumenter.Default
MODIFY
dd-java-agent-ittests/src/test/groovy/datadog/trace/agent/integration/muzzle/MuzzleBytecodeTransformTest.groovy
dd-java-agent-ittests/src/test/groovy/datadog/trace/agent/integration/muzzle/MuzzleBytecodeTransformTest.groovy
@@ -3,7 +3,6 @@ package datadog.trace.agent.integration.muzzle
import datadog.trace.agent.test.IntegrationTestUtils
import spock.lang.Specification
-import java.lang.instrument.Instrumentation
import java.lang.reflect.Field
import java.lang.reflect.Method

MODIFY
dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/Instrumenter.java
dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/Instrumenter.java
@@ -28,7 +28,6 @@ import net.bytebuddy.utility.JavaModule;
* directly.
*/
public interface Instrumenter {
-
/**
* Add this instrumentation to an AgentBuilder.
*
@@ -37,17 +36,6 @@ public interface Instrumenter {
*/
AgentBuilder instrument(AgentBuilder agentBuilder);
- /** @return A type matcher used to match the class under transform. */
- ElementMatcher typeMatcher();
-
- /** @return A type matcher used to match the classloader under transform */
- ElementMatcher<? super ClassLoader> classLoaderMatcher();
-
- /** @return Class names of helpers to inject into the user's classloader */
- String[] helperClassNames();
-
- Map<? extends ElementMatcher, String> transformers();
-
@Slf4j
abstract class Default implements Instrumenter {
private final Set<String> instrumentationNames;
@@ -178,20 +166,20 @@ public interface Instrumenter {
return null;
}
- @Override
+ /** @return Class names of helpers to inject into the user's classloader */
public String[] helperClassNames() {
return new String[0];
}
- @Override
+ /** @return A type matcher used to match the classloader under transform */
public ElementMatcher<ClassLoader> classLoaderMatcher() {
return any();
}
- @Override
+ /** @return A type matcher used to match the class under transform. */
public abstract ElementMatcher<? super TypeDescription> typeMatcher();
- @Override
+ /** @return A map of matcher->advice */
public abstract Map<? extends ElementMatcher, String> transformers();
/**

MODIFY
dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/muzzle/MuzzleVersionScanPlugin.java
dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/muzzle/MuzzleVersionScanPlugin.java
@@ -100,11 +100,12 @@ public class MuzzleVersionScanPlugin {
if (!(instrumenter instanceof Instrumenter.Default)) {
continue;
}
+ Instrumenter.Default defaultInstrumenter = (Instrumenter.Default) instrumenter;
try {
// verify helper injector works
- final String[] helperClassNames = instrumenter.helperClassNames();
+ final String[] helperClassNames = defaultInstrumenter.helperClassNames();
if (helperClassNames.length > 0) {
- new HelperInjector(createHelperMap(instrumenter))
+ new HelperInjector(createHelperMap(defaultInstrumenter))
.transform(null, null, userClassLoader, null);
}
} catch (final Exception e) {
@@ -116,7 +117,8 @@ public class MuzzleVersionScanPlugin {
}
}
- private static Map<String, byte[]> createHelperMap(Instrumenter instrumenter) throws IOException {
+ private static Map<String, byte[]> createHelperMap(Instrumenter.Default instrumenter)
+ throws IOException {
final Map<String, byte[]> helperMap =
new LinkedHashMap<>(instrumenter.helperClassNames().length);
for (final String helperName : instrumenter.helperClassNames()) {

MODIFY
dd-java-agent/instrumentation/jre-logging/src/main/java/datadog/trace/instrumentation/jre/logging/LoggerInstrumentation.java
dd-java-agent/instrumentation/jre-logging/src/main/java/datadog/trace/instrumentation/jre/logging/LoggerInstrumentation.java
package datadog.trace.instrumentation.jre.logging;
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.failSafe;
-import static net.bytebuddy.matcher.ElementMatchers.any;
import static net.bytebuddy.matcher.ElementMatchers.named;
import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.ClassLoaderMatcher;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.Utils;
-import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.asm.AsmVisitorWrapper;
@@ -29,9 +27,12 @@ import net.bytebuddy.pool.TypePool;
import net.bytebuddy.utility.JavaModule;
/**
- * This instrumentation patches java.util.logging.Logger to return a "safe" logger which doesn't touch the global log manager when static log creators (e.g. getLogger) are invoked under datadog threads.
+ * This instrumentation patches java.util.logging.Logger to return a "safe" logger which doesn't
+ * touch the global log manager when static log creators (e.g. getLogger) are invoked under datadog
+ * threads.
*
- * Our PatchLogger solution is not enough here because it's possible for dd-threads to use bootstrap utils which in turn use call Logger.getLogger(...)
+ * <p>Our PatchLogger solution is not enough here because it's possible for dd-threads to use
+ * bootstrap utils which in turn use call Logger.getLogger(...)
*/
@Slf4j
@AutoService(Instrumenter.class)
@@ -47,13 +48,18 @@ public class LoggerInstrumentation implements Instrumenter {
public AgentBuilder instrument(AgentBuilder agentBuilder) {
return agentBuilder
.type(
- failSafe(named(loggerClassName), "Instrumentation type matcher unexpected exception: " + getClass().getName()),
- failSafe(new ElementMatcher<ClassLoader>() {
+ failSafe(
+ named(loggerClassName),
+ "Instrumentation type matcher unexpected exception: " + getClass().getName()),
+ failSafe(
+ new ElementMatcher<ClassLoader>() {
@Override
public boolean matches(ClassLoader target) {
return target == ClassLoaderMatcher.BOOTSTRAP_CLASSLOADER;
}
- }, "Instrumentation class loader matcher unexpected exception: " + getClass().getName()))
+ },
+ "Instrumentation class loader matcher unexpected exception: "
+ + getClass().getName()))
.transform(
new AgentBuilder.Transformer() {
@Override
@@ -173,25 +179,4 @@ public class LoggerInstrumentation implements Instrumenter {
};
}
}
-
- // TODO: move these matcher methods to the Instrumenter.Default class to avoid awkward stubs
- @Override
- public ElementMatcher<? super TypeDescription> typeMatcher() {
- throw new RuntimeException("Should not be invoked");
- }
-
- @Override
- public ElementMatcher<? super ClassLoader> classLoaderMatcher() {
- throw new RuntimeException("Should not be invoked");
- }
-
- @Override
- public String[] helperClassNames() {
- throw new RuntimeException("Should not be invoked");
- }
-
- @Override
- public Map<? extends ElementMatcher, String> transformers() {
- throw new RuntimeException("Should not be invoked");
- }
}

MODIFY
dd-java-agent/instrumentation/trace-annotation/src/main/java/datadog/trace/instrumentation/trace_annotation/TraceConfigInstrumentation.java
dd-java-agent/instrumentation/trace-annotation/src/main/java/datadog/trace/instrumentation/trace_annotation/TraceConfigInstrumentation.java
@@ -136,24 +136,4 @@ public class TraceConfigInstrumentation implements Instrumenter {
return transformers;
}
}
-
- @Override
- public ElementMatcher<TypeDescription> typeMatcher() {
- throw new RuntimeException("TracerConfigInstrumentation must not use TypeMatcher");
- }
-
- @Override
- public ElementMatcher<ClassLoader> classLoaderMatcher() {
- throw new RuntimeException("TracerConfigInstrumentation must not use classLoaderMatcher");
- }
-
- @Override
- public String[] helperClassNames() {
- throw new RuntimeException("TracerConfigInstrumentation must not use helperClassNames");
- }
-
- @Override
- public Map<ElementMatcher, String> transformers() {
- throw new RuntimeException("TracerConfigInstrumentation must not use transformers.");
- }
}

Move default instrumentation declarations to Instrumenter.Default