Get rid of DDAdvice
Newer ByteBuddy api simplifies things.
DELETE
dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/DDAdvice.java
-package datadog.trace.agent.tooling;
-
-import net.bytebuddy.agent.builder.AgentBuilder;
-import net.bytebuddy.agent.builder.AgentBuilder.LocationStrategy;
-import net.bytebuddy.dynamic.ClassFileLocator;
-
-/** A bytebuddy advice builder with default DataDog settings. */
-public class DDAdvice extends AgentBuilder.Transformer.ForAdvice {
- private static final ClassFileLocator AGENT_CLASS_LOCATOR =
- ClassFileLocator.ForClassLoader.of(Utils.getAgentClassLoader());
- /** Location strategy for resolving classes in the agent's jar. */
- private static final LocationStrategy AGENT_CLASS_LOCATION_STRATEGY =
- new AgentBuilder.LocationStrategy.Simple(AGENT_CLASS_LOCATOR);
-
- /**
- * Create bytebuddy advice with default datadog settings.
- *
- * @return the bytebuddy advice
- */
- public static AgentBuilder.Transformer.ForAdvice create() {
- return create(true);
- }
-
- public static AgentBuilder.Transformer.ForAdvice create(final boolean includeExceptionHandler) {
- ForAdvice advice = new DDAdvice().with(AGENT_CLASS_LOCATION_STRATEGY);
- if (includeExceptionHandler) {
- advice = advice.withExceptionHandler(ExceptionHandlers.defaultExceptionHandler());
- }
- return advice;
- }
-
- private DDAdvice() {}
-}

MODIFY
dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/HelperInjector.java
dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/HelperInjector.java
@@ -30,9 +30,10 @@ public class HelperInjector implements Transformer {
* Construct HelperInjector.
*
* @param helperClassNames binary names of the helper classes to inject. These class names must be
- * resolvable by the classloader returned by DDAdvice#getAgentClassLoader(). Classes are
- * injected in the order provided. This is important if there is interdependency between
- * helper classes that requires them to be injected in a specific order.
+ * resolvable by the classloader returned by
+ * datadog.trace.agent.tooling.Utils#getAgentClassLoader(). Classes are injected in the order
+ * provided. This is important if there is interdependency between helper classes that
+ * requires them to be injected in a specific order.
*/
public HelperInjector(final String... helperClassNames) {
this.helperClassNames = new LinkedHashSet<>(Arrays.asList(helperClassNames));

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
@@ -72,17 +72,46 @@ public interface Instrumenter {
}
@Override
- public AgentBuilder instrument(final AgentBuilder agentBuilder) {
+ public AgentBuilder instrument(final AgentBuilder parentAgentBuilder) {
if (!enabled) {
log.debug("Instrumentation {} is disabled", this);
- return agentBuilder;
+ return parentAgentBuilder;
}
- AgentBuilder.Identified.Extendable advice =
- agentBuilder
+ AgentBuilder.Identified.Extendable agentBuilder =
+ parentAgentBuilder
.type(typeMatcher(), classLoaderMatcher())
- .and(
- new AgentBuilder.RawMatcher() {
+ .and(new MuzzleMatcher())
+ .transform(DDTransformers.defaultTransformers());
+ agentBuilder = injectHelperClasses(agentBuilder);
+ agentBuilder = applyInstrumentationTransformers(agentBuilder);
+ return agentBuilder.asDecorator();
+ }
+
+ private AgentBuilder.Identified.Extendable injectHelperClasses(
+ AgentBuilder.Identified.Extendable agentBuilder) {
+ final String[] helperClassNames = helperClassNames();
+ if (helperClassNames.length > 0) {
+ agentBuilder = agentBuilder.transform(new HelperInjector(helperClassNames));
+ }
+ return agentBuilder;
+ }
+
+ private AgentBuilder.Identified.Extendable applyInstrumentationTransformers(
+ AgentBuilder.Identified.Extendable agentBuilder) {
+ for (final Map.Entry<ElementMatcher, String> entry : transformers().entrySet()) {
+ agentBuilder =
+ agentBuilder.transform(
+ new AgentBuilder.Transformer.ForAdvice()
+ .include(Utils.getAgentClassLoader())
+ .withExceptionHandler(ExceptionHandlers.defaultExceptionHandler())
+ .advice(entry.getKey(), entry.getValue()));
+ }
+ return agentBuilder;
+ }
+
+ /** Matches classes for which instrumentation is not muzzled. */
+ private class MuzzleMatcher implements AgentBuilder.RawMatcher {
@Override
public boolean matches(
final TypeDescription typeDescription,
@@ -112,16 +141,6 @@ public interface Instrumenter {
}
return true;
}
- })
- .transform(DDTransformers.defaultTransformers());
- final String[] helperClassNames = helperClassNames();
- if (helperClassNames.length > 0) {
- advice = advice.transform(new HelperInjector(helperClassNames));
- }
- for (final Map.Entry<ElementMatcher, String> entry : transformers().entrySet()) {
- advice = advice.transform(DDAdvice.create().advice(entry.getKey(), entry.getValue()));
- }
- return advice.asDecorator();
}
/**

Get rid of DDAdvice

Newer ByteBuddy api simplifies things.