Skip to content

Commit 94a8ddf

Browse files
committed
Iterate over InstrumenterModule services instead of Instrumenters
1 parent a786410 commit 94a8ddf

File tree

729 files changed

+898
-899
lines changed

Some content is hidden

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

729 files changed

+898
-899
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/java/concurrent/ExcludeFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private static EnumSet<ExcludeType> exclude(Class<?> clazz) {
117117

118118
/**
119119
* This should only be called during startup to initialize this based on information gathered from
120-
* the {@code Instrumenter} instances.
120+
* the loaded {@code InstrumenterModule}s.
121121
*
122122
* @param excludeTypes the types to exclude
123123
*/

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static void installBytebuddyAgent(final Instrumentation inst) {
8989
}
9090

9191
/**
92-
* Install the core bytebuddy agent along with all implementations of {@link Instrumenter}.
92+
* Install the core bytebuddy agent along with all registered {@link InstrumenterModule}s.
9393
*
9494
* @return the agent's class transformer
9595
*/
@@ -167,24 +167,23 @@ public static ClassFileTransformer installBytebuddyAgent(
167167
agentBuilder = agentBuilder.with(listener);
168168
}
169169

170-
Instrumenters instrumenters = Instrumenters.load(AgentInstaller.class.getClassLoader());
171-
int maxInstrumentationId = instrumenters.maxInstrumentationId();
170+
InstrumenterModules modules = InstrumenterModules.load();
171+
int maxInstrumentationId = modules.maxInstrumentationId();
172172

173173
// pre-size state before registering instrumentations to reduce number of allocations
174174
InstrumenterState.setMaxInstrumentationId(maxInstrumentationId);
175175

176-
// This needs to be a separate loop through all the instrumenters before we start adding
176+
// This needs to be a separate loop through all instrumentations before we start adding
177177
// advice so that we can exclude field injection, since that will try to check exclusion
178178
// immediately and we don't have the ability to express dependencies between different
179-
// instrumenters to control the load order.
180-
for (Instrumenter instrumenter : instrumenters) {
181-
if (instrumenter instanceof ExcludeFilterProvider) {
182-
ExcludeFilterProvider provider = (ExcludeFilterProvider) instrumenter;
179+
// instrumentations to control the load order.
180+
for (InstrumenterModule module : modules) {
181+
if (module instanceof ExcludeFilterProvider) {
182+
ExcludeFilterProvider provider = (ExcludeFilterProvider) module;
183183
ExcludeFilter.add(provider.excludedClasses());
184184
if (DEBUG) {
185185
log.debug(
186-
"Adding filtered classes - instrumentation.class={}",
187-
instrumenter.getClass().getName());
186+
"Adding filtered classes - instrumentation.class={}", module.getClass().getName());
188187
}
189188
}
190189
}
@@ -193,23 +192,21 @@ public static ClassFileTransformer installBytebuddyAgent(
193192
new CombiningTransformerBuilder(agentBuilder, maxInstrumentationId);
194193

195194
int installedCount = 0;
196-
for (Instrumenter instrumenter : instrumenters) {
197-
if (instrumenter instanceof InstrumenterModule
198-
&& !((InstrumenterModule) instrumenter).isApplicable(enabledSystems)) {
195+
for (InstrumenterModule module : modules) {
196+
if (!module.isApplicable(enabledSystems)) {
199197
if (DEBUG) {
200-
log.debug("Not applicable - instrumentation.class={}", instrumenter.getClass().getName());
198+
log.debug("Not applicable - instrumentation.class={}", module.getClass().getName());
201199
}
202200
continue;
203201
}
204202
if (DEBUG) {
205-
log.debug("Loading - instrumentation.class={}", instrumenter.getClass().getName());
203+
log.debug("Loading - instrumentation.class={}", module.getClass().getName());
206204
}
207205
try {
208-
transformerBuilder.applyInstrumentation(instrumenter);
206+
transformerBuilder.applyInstrumentation(module);
209207
installedCount++;
210208
} catch (Exception | LinkageError e) {
211-
log.error(
212-
"Failed to load - instrumentation.class={}", instrumenter.getClass().getName(), e);
209+
log.error("Failed to load - instrumentation.class={}", module.getClass().getName(), e);
213210
}
214211
}
215212
if (DEBUG) {

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,12 @@ public CombiningTransformerBuilder(AgentBuilder agentBuilder, int maxInstrumenta
7474
this.nextSupplementaryId = maxInstrumentationId + 1;
7575
}
7676

77-
public void applyInstrumentation(Instrumenter instrumenter) {
78-
if (instrumenter instanceof InstrumenterModule) {
79-
InstrumenterModule module = (InstrumenterModule) instrumenter;
80-
if (module.isEnabled()) {
81-
InstrumenterState.registerInstrumentation(module);
82-
for (Instrumenter member : module.typeInstrumentations()) {
83-
buildInstrumentation(module, member);
84-
}
77+
public void applyInstrumentation(InstrumenterModule module) {
78+
if (module.isEnabled()) {
79+
InstrumenterState.registerInstrumentation(module);
80+
for (Instrumenter member : module.typeInstrumentations()) {
81+
buildInstrumentation(module, member);
8582
}
86-
} else {
87-
throw new IllegalArgumentException("Unexpected Instrumenter type");
8883
}
8984
}
9085

@@ -184,7 +179,7 @@ public void applyAdvice(Instrumenter.TransformingAdvice typeAdvice) {
184179
}
185180

186181
@Override
187-
public void applyAdvice(ElementMatcher<? super MethodDescription> matcher, String className) {
182+
public void applyAdvice(ElementMatcher<? super MethodDescription> matcher, String adviceClass) {
188183
Advice.WithCustomMapping customMapping = Advice.withCustomMapping();
189184
if (postProcessor != null) {
190185
customMapping = customMapping.with(postProcessor);
@@ -193,7 +188,7 @@ public void applyAdvice(ElementMatcher<? super MethodDescription> matcher, Strin
193188
new AgentBuilder.Transformer.ForAdvice(customMapping)
194189
.include(Utils.getBootstrapProxy(), Utils.getAgentClassLoader())
195190
.withExceptionHandler(ExceptionHandlers.defaultExceptionHandler())
196-
.advice(not(ignoredMethods).and(matcher), className));
191+
.advice(not(ignoredMethods).and(matcher), adviceClass));
197192
}
198193

199194
/** Counts the number of distinct context store injections registered with this builder. */

dd-java-agent/agent-builder/src/main/java/datadog/trace/agent/tooling/bytebuddy/matcher/MuzzleMatcher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
public final class MuzzleMatcher implements AgentBuilder.RawMatcher {
1111
private final MuzzleCheck muzzleCheck;
1212

13-
public MuzzleMatcher(InstrumenterModule instrumenter) {
14-
this.muzzleCheck = new MuzzleCheck(instrumenter);
13+
public MuzzleMatcher(InstrumenterModule module) {
14+
this.muzzleCheck = new MuzzleCheck(module);
1515
}
1616

1717
@Override

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ public final class AgentCLI {
3737
/** Prints all known integrations in alphabetical order. */
3838
public static void printIntegrationNames() {
3939
Set<String> names = new TreeSet<>();
40-
for (Instrumenter instrumenter : Instrumenters.load(Instrumenter.class.getClassLoader())) {
41-
if (instrumenter instanceof InstrumenterModule) {
42-
names.add(((InstrumenterModule) instrumenter).name());
43-
}
40+
for (InstrumenterModule module : InstrumenterModules.load()) {
41+
names.add(module.name());
4442
}
4543
for (String name : names) {
4644
System.out.println(name);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import java.util.Set;
77

88
/**
9-
* Used to allow an {@link Instrumenter} to opt out of broad instrumentations like {@link Runnable}.
9+
* Allows an {@link InstrumenterModule} to opt out of broad instrumentations like {@link Runnable}.
1010
*
11-
* <p>These are looked up in a separate pass before the {@link Instrumenter} is allowed to add
11+
* <p>These are looked up in a separate pass before the {@link InstrumenterModule} is allowed to add
1212
* instrumentations, to be able to opt out of field injection which will need to check for
1313
* exclusions immediately.
1414
*/

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@
1313
import net.bytebuddy.matcher.ElementMatcher;
1414
import net.bytebuddy.utility.JavaModule;
1515

16-
/**
17-
* Built-in bytebuddy-based instrumentation for the datadog javaagent.
18-
*
19-
* <p>It is strongly recommended to extend {@link InstrumenterModule} rather than implement this
20-
* interface directly.
21-
*/
16+
/** Declares bytebuddy-based type instrumentation for the datadog javaagent. */
2217
public interface Instrumenter {
2318

2419
/** Instrumentation that only matches a single named type. */
@@ -39,6 +34,9 @@ interface ForTypeHierarchy {
3934
ElementMatcher<TypeDescription> hierarchyMatcher();
4035
}
4136

37+
/** Instrumentation that transforms types on the bootstrap class-path. */
38+
interface ForBootstrap {}
39+
4240
/**
4341
* Instrumentation that matches a series of types configured at runtime. This is used for last
4442
* minute additions in the field such as testing a new JDBC driver that is not yet in the allowed
@@ -107,9 +105,7 @@ interface HasMethodAdvice extends Instrumenter {
107105
void methodAdvice(MethodTransformer transformer);
108106
}
109107

110-
/** Instrumentation that transforms types on the bootstrap class-path. */
111-
interface ForBootstrap {}
112-
108+
/** Applies type advice from an instrumentation that {@link HasTypeAdvice}. */
113109
interface TypeTransformer {
114110
void applyAdvice(TransformingAdvice typeAdvice);
115111

@@ -118,10 +114,12 @@ default void applyAdvice(AsmVisitorWrapper typeVisitor) {
118114
}
119115
}
120116

117+
/** Applies method advice from an instrumentation that {@link HasMethodAdvice}. */
121118
interface MethodTransformer {
122-
void applyAdvice(ElementMatcher<? super MethodDescription> matcher, String className);
119+
void applyAdvice(ElementMatcher<? super MethodDescription> matcher, String adviceClass);
123120
}
124121

122+
/** Contributes a transformation step to the dynamic type builder. */
125123
interface TransformingAdvice {
126124
DynamicType.Builder<?> transform(
127125
DynamicType.Builder<?> builder,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public enum TargetSystem {
6060
protected final String packageName = Strings.getPackageName(getClass().getName());
6161

6262
public InstrumenterModule(final String instrumentationName, final String... additionalNames) {
63-
instrumentationId = Instrumenters.currentInstrumentationId();
63+
instrumentationId = InstrumenterModules.currentInstrumentationId();
6464
instrumentationNames = new ArrayList<>(1 + additionalNames.length);
6565
instrumentationNames.add(instrumentationName);
6666
addAll(instrumentationNames, additionalNames);

dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/Instrumenters.java renamed to dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/InstrumenterModules.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,64 +13,65 @@
1313
import org.slf4j.LoggerFactory;
1414

1515
/**
16-
* Provides a stable sequence of {@link Instrumenters} registered as META-INF services. The id of
17-
* the {@link Instrumenter} currently being installed is available during iteration by calling
18-
* {@link #currentInstrumentationId()}.
16+
* Provides a stable sequence of {@link InstrumenterModule}s registered as META-INF services. The id
17+
* of the instrumentation currently being installed is available during iteration by calling {@link
18+
* #currentInstrumentationId()}.
1919
*
20-
* <p>Note: it is expected that only one thread will iterate over instrumenters at a time.
20+
* <p>Note: it is expected that only one thread will iterate over this sequence at a time.
2121
*/
22-
public final class Instrumenters implements Iterable<Instrumenter> {
23-
static final Logger log = LoggerFactory.getLogger(Instrumenters.class);
22+
public final class InstrumenterModules implements Iterable<InstrumenterModule> {
23+
static final Logger log = LoggerFactory.getLogger(InstrumenterModules.class);
2424

2525
final ClassLoader loader;
2626
final String[] names;
2727

28-
final Instrumenter[] instrumenters;
28+
final InstrumenterModule[] modules;
2929
static int currentInstrumentationId;
3030

31-
public static Instrumenters load(ClassLoader loader) {
32-
return new Instrumenters(loader, loadInstrumenterNames(loader));
31+
public static InstrumenterModules load() {
32+
ClassLoader loader = InstrumenterModule.class.getClassLoader();
33+
return new InstrumenterModules(loader, loadModuleNames(loader));
3334
}
3435

35-
Instrumenters(ClassLoader loader, String[] names) {
36+
InstrumenterModules(ClassLoader loader, String[] names) {
3637
this.loader = loader;
3738
this.names = names;
3839

39-
this.instrumenters = new Instrumenter[names.length];
40+
this.modules = new InstrumenterModule[names.length];
4041
}
4142

4243
public int maxInstrumentationId() {
43-
return instrumenters.length;
44+
return modules.length;
4445
}
4546

46-
/** Returns the id of the {@link Instrumenter} currently being installed. */
47+
/** Returns the id of the instrumentation currently being installed. */
4748
public static int currentInstrumentationId() {
4849
return currentInstrumentationId;
4950
}
5051

5152
@Override
52-
public Iterator<Instrumenter> iterator() {
53-
return new Iterator<Instrumenter>() {
53+
public Iterator<InstrumenterModule> iterator() {
54+
return new Iterator<InstrumenterModule>() {
5455
private int index = 0;
5556

5657
@Override
5758
public boolean hasNext() {
58-
while (index < instrumenters.length) {
59-
if (null != instrumenters[index]) {
59+
while (index < modules.length) {
60+
if (null != modules[index]) {
6061
currentInstrumentationId = index;
6162
return true;
6263
}
6364
String nextName = names[index];
6465
if (null != nextName) {
6566
try {
66-
currentInstrumentationId = index; // set before loading instrumenter
67+
currentInstrumentationId = index; // set before loading the next module
6768
@SuppressWarnings({"rawtypes", "unchecked"})
68-
Class<Instrumenter> nextType = (Class) loader.loadClass(nextName);
69-
instrumenters[index] = nextType.getConstructor().newInstance();
69+
Class<InstrumenterModule> nextType = (Class) loader.loadClass(nextName);
70+
modules[index] = nextType.getConstructor().newInstance();
7071
return true;
7172
} catch (Throwable e) {
7273
log.error("Failed to load - instrumentation.class={}", nextName, e);
73-
names[index] = null; // only attempt to load instrumenter once
74+
names[index] = null; // only attempt to load each module once
7475
}
7576
}
7677
index++;
@@ -79,9 +80,9 @@ public boolean hasNext() {
7980
}
8081

8182
@Override
82-
public Instrumenter next() {
83+
public InstrumenterModule next() {
8384
if (hasNext()) {
84-
return instrumenters[index++];
85+
return modules[index++];
8586
} else {
8687
throw new NoSuchElementException();
8788
}
@@ -94,11 +95,11 @@ public void remove() {
9495
};
9596
}
9697

97-
private static String[] loadInstrumenterNames(ClassLoader loader) {
98+
private static String[] loadModuleNames(ClassLoader loader) {
9899
Set<String> lines = new LinkedHashSet<>();
99100
try {
100101
Enumeration<URL> urls =
101-
loader.getResources("META-INF/services/datadog.trace.agent.tooling.Instrumenter");
102+
loader.getResources("META-INF/services/datadog.trace.agent.tooling.InstrumenterModule");
102103
while (urls.hasMoreElements()) {
103104
try (BufferedReader reader =
104105
new BufferedReader(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.slf4j.Logger;
99
import org.slf4j.LoggerFactory;
1010

11-
/** Tracks {@link Instrumenter} state, such as where it was applied and where it was blocked. */
11+
/** Tracks instrumentation state, such as where it was applied and where it was blocked. */
1212
public final class InstrumenterState {
1313
private static final Logger log = LoggerFactory.getLogger(InstrumenterState.class);
1414

0 commit comments

Comments
 (0)