Skip to content

Commit e8df8a0

Browse files
authored
Move HasMethodAdvice aspect to instrumentations, away from modules (#8139)
* Load 'AbcModule' definitions from the instrumentation class-loader so they can be unloaded after startup * Move postProcessor option to InstrumenterModule, so it can be shared across the grouped instrumentations
1 parent c99c2c2 commit e8df8a0

File tree

761 files changed

+919
-820
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

761 files changed

+919
-820
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/DatadogClassLoader.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ protected Enumeration<URL> findResources(String name) {
9393
@Override
9494
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
9595
if (name.startsWith("datadog.trace.instrumentation.")
96-
&& (name.endsWith("$Muzzle") || name.endsWith("Instrumentation"))) {
96+
&& (name.endsWith("$Muzzle")
97+
|| name.endsWith("Instrumentation")
98+
|| name.endsWith("Module"))) {
9799
InstrumentationClassLoader cl;
98100
if (null == (cl = instrumentationClassLoader.get())) {
99101
synchronized (instrumentationClassLoaderLock) {

dd-java-agent/agent-builder/src/main/java/datadog/trace/agent/tooling/CombiningTransformerBuilder.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.namedOneOf;
1010
import static net.bytebuddy.matcher.ElementMatchers.not;
1111

12-
import datadog.trace.agent.tooling.Instrumenter.WithPostProcessor;
1312
import datadog.trace.agent.tooling.bytebuddy.ExceptionHandlers;
1413
import datadog.trace.agent.tooling.context.FieldBackedContextInjector;
1514
import datadog.trace.agent.tooling.context.FieldBackedContextMatcher;
@@ -73,17 +72,12 @@ public final class CombiningTransformerBuilder
7372
private Map<String, String> contextStore;
7473
private AgentBuilder.Transformer contextRequestRewriter;
7574
private HelperTransformer helperTransformer;
75+
private Advice.PostProcessor.Factory postProcessor;
7676
private MuzzleCheck muzzle;
7777

7878
// temporary buffer for collecting advice; reset for each instrumenter
7979
private final List<AgentBuilder.Transformer> advice = new ArrayList<>();
8080

81-
/**
82-
* Post processor to be applied to instrumenter advices if they implement {@link
83-
* WithPostProcessor}
84-
*/
85-
private Advice.PostProcessor.Factory postProcessor;
86-
8781
public CombiningTransformerBuilder(
8882
AgentBuilder agentBuilder, InstrumenterIndex instrumenterIndex) {
8983
this.agentBuilder = agentBuilder;
@@ -134,6 +128,8 @@ private void prepareInstrumentation(InstrumenterModule module, int instrumentati
134128
module.useAgentCodeSource(), module.getClass().getSimpleName(), helperClassNames)
135129
: null;
136130

131+
postProcessor = module.postProcessor();
132+
137133
muzzle = new MuzzleCheck(module, instrumentationId);
138134
}
139135

@@ -208,9 +204,6 @@ private void buildTypeMatcher(Instrumenter member, int transformationId) {
208204

209205
private void buildTypeAdvice(Instrumenter member, int transformationId) {
210206

211-
postProcessor =
212-
member instanceof WithPostProcessor ? ((WithPostProcessor) member).postProcessor() : null;
213-
214207
if (null != helperTransformer) {
215208
advice.add(helperTransformer);
216209
}

dd-java-agent/agent-builder/src/test/groovy/datadog/trace/agent/test/DefaultInstrumenterForkedTest.groovy

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,5 @@ class DefaultInstrumenterForkedTest extends DDSpecification {
141141
TestDefaultInstrumenter(String instrumentationName, String additionalName) {
142142
super(instrumentationName, [additionalName] as String[])
143143
}
144-
145-
@Override
146-
void methodAdvice(MethodTransformer transformer) {}
147144
}
148145
}

dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/Instrumenter.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import java.security.ProtectionDomain;
77
import java.util.Collection;
8-
import net.bytebuddy.asm.Advice;
98
import net.bytebuddy.asm.AsmVisitorWrapper;
109
import net.bytebuddy.description.method.MethodDescription;
1110
import net.bytebuddy.description.type.TypeDescription;
@@ -82,11 +81,6 @@ interface WithTypeStructure {
8281
ElementMatcher<TypeDescription> structureMatcher();
8382
}
8483

85-
/** Instrumentation that wants to apply additional structure checks after type matching. */
86-
interface WithPostProcessor {
87-
Advice.PostProcessor.Factory postProcessor();
88-
}
89-
9084
/** Instrumentation that provides advice which affects the whole type. */
9185
interface HasTypeAdvice extends Instrumenter {
9286
/**

dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/InstrumenterModule.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ public ElementMatcher<? super MethodDescription> methodIgnoreMatcher() {
156156
return isSynthetic();
157157
}
158158

159+
/** Override this to post-process the operand stack of any transformed methods. */
160+
public Advice.PostProcessor.Factory postProcessor() {
161+
return null;
162+
}
163+
159164
/**
160165
* Context stores to define for this instrumentation. Are added to matching class loaders.
161166
*
@@ -191,7 +196,7 @@ protected final boolean isShortcutMatchingEnabled(boolean defaultToShortcut) {
191196
}
192197

193198
/** Parent class for all tracing related instrumentations */
194-
public abstract static class Tracing extends InstrumenterModule implements HasMethodAdvice {
199+
public abstract static class Tracing extends InstrumenterModule {
195200
public Tracing(String instrumentationName, String... additionalNames) {
196201
super(instrumentationName, additionalNames);
197202
}
@@ -203,7 +208,7 @@ public boolean isApplicable(Set<TargetSystem> enabledSystems) {
203208
}
204209

205210
/** Parent class for all profiling related instrumentations */
206-
public abstract static class Profiling extends InstrumenterModule implements HasMethodAdvice {
211+
public abstract static class Profiling extends InstrumenterModule {
207212
public Profiling(String instrumentationName, String... additionalNames) {
208213
super(instrumentationName, additionalNames);
209214
}
@@ -222,7 +227,7 @@ public boolean isEnabled() {
222227
}
223228

224229
/** Parent class for all AppSec related instrumentations */
225-
public abstract static class AppSec extends InstrumenterModule implements HasMethodAdvice {
230+
public abstract static class AppSec extends InstrumenterModule {
226231
public AppSec(String instrumentationName, String... additionalNames) {
227232
super(instrumentationName, additionalNames);
228233
}
@@ -235,8 +240,7 @@ public boolean isApplicable(Set<TargetSystem> enabledSystems) {
235240

236241
/** Parent class for all IAST related instrumentations */
237242
@SuppressForbidden
238-
public abstract static class Iast extends InstrumenterModule
239-
implements HasMethodAdvice, WithPostProcessor {
243+
public abstract static class Iast extends InstrumenterModule {
240244
public Iast(String instrumentationName, String... additionalNames) {
241245
super(instrumentationName, additionalNames);
242246
}
@@ -291,7 +295,7 @@ protected boolean isOptOutEnabled() {
291295
}
292296

293297
/** Parent class for all USM related instrumentations */
294-
public abstract static class Usm extends InstrumenterModule implements HasMethodAdvice {
298+
public abstract static class Usm extends InstrumenterModule {
295299
public Usm(String instrumentationName, String... additionalNames) {
296300
super(instrumentationName, additionalNames);
297301
}
@@ -303,7 +307,7 @@ public boolean isApplicable(Set<TargetSystem> enabledSystems) {
303307
}
304308

305309
/** Parent class for all CI related instrumentations */
306-
public abstract static class CiVisibility extends InstrumenterModule implements HasMethodAdvice {
310+
public abstract static class CiVisibility extends InstrumenterModule {
307311
public CiVisibility(String instrumentationName, String... additionalNames) {
308312
super(instrumentationName, additionalNames);
309313
}

dd-java-agent/instrumentation/aerospike-4/src/main/java/datadog/trace/instrumentation/aerospike4/AerospikeClientInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
@AutoService(InstrumenterModule.class)
1919
public final class AerospikeClientInstrumentation extends InstrumenterModule.Tracing
20-
implements Instrumenter.ForSingleType {
20+
implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice {
2121
public AerospikeClientInstrumentation() {
2222
super("aerospike");
2323
}

dd-java-agent/instrumentation/aerospike-4/src/main/java/datadog/trace/instrumentation/aerospike4/CommandInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
@AutoService(InstrumenterModule.class)
2121
public final class CommandInstrumentation extends InstrumenterModule.Tracing
22-
implements Instrumenter.ForSingleType {
22+
implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice {
2323
public CommandInstrumentation() {
2424
super("aerospike");
2525
}

dd-java-agent/instrumentation/aerospike-4/src/main/java/datadog/trace/instrumentation/aerospike4/NioEventLoopInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
@AutoService(InstrumenterModule.class)
1818
public final class NioEventLoopInstrumentation extends InstrumenterModule.Tracing
19-
implements Instrumenter.ForSingleType {
19+
implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice {
2020
public NioEventLoopInstrumentation() {
2121
super("aerospike", "java_concurrent");
2222
}

dd-java-agent/instrumentation/aerospike-4/src/main/java/datadog/trace/instrumentation/aerospike4/PartitionInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
@AutoService(InstrumenterModule.class)
2222
public final class PartitionInstrumentation extends InstrumenterModule.Tracing
23-
implements Instrumenter.ForSingleType {
23+
implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice {
2424
public PartitionInstrumentation() {
2525
super("aerospike");
2626
}

dd-java-agent/instrumentation/akka-concurrent/src/main/java/datadog/trace/instrumentation/akka/concurrent/AkkaActorCellInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
@AutoService(InstrumenterModule.class)
2323
public class AkkaActorCellInstrumentation extends InstrumenterModule.Tracing
24-
implements Instrumenter.ForSingleType {
24+
implements Instrumenter.ForSingleType, Instrumenter.HasMethodAdvice {
2525

2626
public AkkaActorCellInstrumentation() {
2727
super("akka_actor_receive", "akka_actor", "akka_concurrent", "java_concurrent");

0 commit comments

Comments
 (0)