Skip to content

Commit 22f46ba

Browse files
committed
Experiment with recording targetSystem in the instrumenter index
1 parent 0e4a616 commit 22f46ba

2 files changed

Lines changed: 38 additions & 15 deletions

File tree

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ public static ClassFileTransformer installBytebuddyAgent(
178178
InstrumenterState.initialize(instrumenterIndex.instrumentationCount());
179179

180180
// combine known modules indexed at build-time with extensions contributed at run-time
181-
Iterable<InstrumenterModule> instrumenterModules = withExtensions(instrumenterIndex.modules());
181+
Iterable<InstrumenterModule> instrumenterModules =
182+
withExtensions(instrumenterIndex.modules(enabledSystems));
182183

183184
// This needs to be a separate loop through all instrumentations before we start adding
184185
// advice so that we can exclude field injection, since that will try to check exclusion
@@ -200,12 +201,6 @@ public static ClassFileTransformer installBytebuddyAgent(
200201

201202
int installedCount = 0;
202203
for (InstrumenterModule module : instrumenterModules) {
203-
if (!enabledSystems.contains(module.targetSystem())) {
204-
if (DEBUG) {
205-
log.debug("Not applicable - instrumentation.class={}", module.getClass().getName());
206-
}
207-
continue;
208-
}
209204
if (!module.isEnabled(enabledSystems)) {
210205
if (DEBUG) {
211206
log.debug("Not enabled - instrumentation.class={}", module.getClass().getName());

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

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static java.nio.charset.StandardCharsets.UTF_8;
55
import static java.util.Collections.singletonList;
66

7+
import datadog.trace.agent.tooling.InstrumenterModule.TargetSystem;
78
import datadog.trace.agent.tooling.bytebuddy.SharedTypePools;
89
import datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers;
910
import java.io.BufferedInputStream;
@@ -51,15 +52,18 @@ final class InstrumenterIndex {
5152

5253
private final InstrumenterModule[] modules;
5354

54-
// packed sequence of module type names and their expected member names:
55-
// module1, memberCount, memberA, memberB, module2, memberCount, memberC, ...
55+
// packed sequence of module type names, their target systems, and their expected member names:
56+
// module1, targetSystemId, memberCount, memberA, memberB,
57+
// module2, targetSystemId, memberCount, memberC, ...
5658
// (each string is encoded as its length plus that number of ASCII bytes)
5759
private final byte[] packedNames;
5860
private int nameIndex;
5961

6062
// current module details
6163
private int instrumentationId = -1;
6264
private String moduleName;
65+
private int targetSystemId;
66+
6367
private int memberCount;
6468

6569
// current member details
@@ -74,20 +78,34 @@ private InstrumenterIndex(int instrumentationCount, int transformationCount, byt
7478
}
7579

7680
public Iterable<InstrumenterModule> modules() {
77-
return ModuleIterator::new;
81+
return () -> new ModuleIterator(-1);
82+
}
83+
84+
public Iterable<InstrumenterModule> modules(Set<TargetSystem> enabledSystems) {
85+
return () -> new ModuleIterator(systemMask(enabledSystems));
86+
}
87+
88+
private int systemMask(Set<TargetSystem> enabledSystems) {
89+
int systemMask = 0;
90+
for (TargetSystem system : enabledSystems) {
91+
systemMask |= (1 << system.ordinal());
92+
}
93+
return systemMask;
7894
}
7995

8096
final class ModuleIterator implements Iterator<InstrumenterModule> {
97+
private int systemMask;
8198
private InstrumenterModule module;
8299

83-
ModuleIterator() {
100+
ModuleIterator(int systemMask) {
101+
this.systemMask = systemMask;
84102
restart();
85103
}
86104

87105
@Override
88106
public boolean hasNext() {
89107
while (null == module && hasNextModule()) {
90-
module = nextModule();
108+
module = nextModule(systemMask);
91109
}
92110
return null != module;
93111
}
@@ -148,7 +166,7 @@ boolean hasNextModule() {
148166
}
149167

150168
/** Returns the next {@link InstrumenterModule} in the index. */
151-
InstrumenterModule nextModule() {
169+
InstrumenterModule nextModule(int systemMask) {
152170
while (memberCount > 0) {
153171
skipMember(); // skip past unmatched members from previous module
154172
}
@@ -157,10 +175,14 @@ InstrumenterModule nextModule() {
157175
// use data from previously loaded module
158176
moduleName = module.getClass().getName();
159177
skipName();
178+
skipNumber();
160179
} else {
161180
moduleName = readName();
162-
module = buildNodule();
163-
modules[instrumentationId] = module;
181+
targetSystemId = readNumber();
182+
if ((systemMask & (1 << targetSystemId)) != 0) {
183+
module = buildNodule();
184+
modules[instrumentationId] = module;
185+
}
164186
}
165187
memberCount = readNumber();
166188
if (SELF_MEMBERSHIP == memberCount) {
@@ -217,6 +239,11 @@ private int readNumber() {
217239
return 0xFF & (int) packedNames[nameIndex++];
218240
}
219241

242+
/** Skips an unsigned byte from the packed name sequence. */
243+
private void skipNumber() {
244+
nameIndex++;
245+
}
246+
220247
public static InstrumenterIndex readIndex() {
221248
URL indexResource = instrumenterClassLoader.getResource(INSTRUMENTER_INDEX_NAME);
222249
if (null != indexResource) {
@@ -295,6 +322,7 @@ public void buildIndex() {
295322
instrumentationCount++;
296323
out.writeByte(moduleName.length());
297324
out.writeBytes(moduleName);
325+
out.writeByte(module.targetSystem().ordinal());
298326
try {
299327
List<Instrumenter> members = module.typeInstrumentations();
300328
if (members.equals(singletonList(module))) {

0 commit comments

Comments
 (0)