-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathInstrumenterIndex.java
More file actions
351 lines (309 loc) · 11.9 KB
/
InstrumenterIndex.java
File metadata and controls
351 lines (309 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package datadog.trace.agent.tooling;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.singletonList;
import datadog.trace.agent.tooling.bytebuddy.SharedTypePools;
import datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Maintains an index of known {@link InstrumenterModule}s and their expected transformations.
*
* <p>This index is not thread-safe; it expects only one thread to iterate over it at a time. It
* also assumes indexed types have simple ASCII names which are less than 256 characters long.
*/
final class InstrumenterIndex {
private static final Logger log = LoggerFactory.getLogger(InstrumenterIndex.class);
private static final String INSTRUMENTER_INDEX_NAME = "instrumenter.index";
// Special memberCount that indicates a module contains itself as a transformation
private static final int SELF_MEMBERSHIP = 0xFF;
static final ClassLoader instrumenterClassLoader = Instrumenter.class.getClassLoader();
private final int instrumentationCount;
private final int transformationCount;
private final InstrumenterModule[] modules;
// packed sequence of module type names and their expected member names:
// module1, memberCount, memberA, memberB, module2, memberCount, memberC, ...
// (each string is encoded as its length plus that number of ASCII bytes)
private final byte[] packedNames;
private int nameIndex;
// current module details
private int instrumentationId = -1;
private String moduleName;
private int memberCount;
// current member details
private int transformationId = -1;
private String memberName;
private InstrumenterIndex(int instrumentationCount, int transformationCount, byte[] packedNames) {
this.modules = new InstrumenterModule[instrumentationCount];
this.instrumentationCount = instrumentationCount;
this.transformationCount = transformationCount;
this.packedNames = packedNames;
}
public Iterable<InstrumenterModule> modules() {
return ModuleIterator::new;
}
final class ModuleIterator implements Iterator<InstrumenterModule> {
private InstrumenterModule module;
ModuleIterator() {
restart();
}
@Override
public boolean hasNext() {
while (null == module && hasNextModule()) {
module = nextModule();
}
return null != module;
}
@Override
public InstrumenterModule next() {
if (hasNext()) {
InstrumenterModule result = module;
module = null;
return result;
} else {
throw new NoSuchElementException();
}
}
}
/** Maximum known count of {@link InstrumenterModule} instrumentations. */
public int instrumentationCount() {
return instrumentationCount;
}
/** Maximum known count of {@link Instrumenter} transformations. */
public int transformationCount() {
return transformationCount;
}
/** Returns the id allocated to the instrumentation; {@code -1} if unknown. */
public int instrumentationId(InstrumenterModule module) {
if (module.getClass().getName().equals(moduleName)) {
return instrumentationId;
}
return -1;
}
/** Returns the id allocated to the transformation; {@code -1} if unknown. */
public int transformationId(Instrumenter member) {
if (null == memberName && memberCount > 0) {
nextMember(); // move through expected members as transformations are applied
}
if (null != memberName && member.getClass().getName().endsWith(memberName)) {
memberName = null; // mark member as used for this iteration
return transformationId;
}
return -1;
}
/** Resets the iteration to the start of the index. */
void restart() {
nameIndex = 0;
instrumentationId = -1;
transformationId = -1;
memberCount = 0;
}
/** Is there another known {@link InstrumenterModule} left in the index? */
boolean hasNextModule() {
return instrumentationCount - instrumentationId > 1;
}
/** Returns the next {@link InstrumenterModule} in the index. */
InstrumenterModule nextModule() {
while (memberCount > 0) {
skipMember(); // skip past unmatched members from previous module
}
InstrumenterModule module = modules[++instrumentationId];
if (null != module) {
// use data from previously loaded module
moduleName = module.getClass().getName();
skipName();
} else {
moduleName = readName();
module = buildNodule();
modules[instrumentationId] = module;
}
memberCount = readNumber();
if (SELF_MEMBERSHIP == memberCount) {
transformationId++;
memberName = moduleName;
memberCount = 0;
} else {
memberName = null;
}
return module;
}
private InstrumenterModule buildNodule() {
try {
@SuppressWarnings({"rawtypes", "unchecked"})
Class<InstrumenterModule> nextType = (Class) instrumenterClassLoader.loadClass(moduleName);
return nextType.getConstructor().newInstance();
} catch (Throwable e) {
log.error("Failed to build instrumentation module {}", moduleName, e);
return null;
}
}
/** Moves onto the next member in the expected sequence. */
private void nextMember() {
memberCount--;
transformationId++;
memberName = readName();
}
/** Skips past the next member in the expected sequence. */
private void skipMember() {
memberCount--;
transformationId++;
skipName();
}
/** Reads a single-byte-encoded string from the packed name sequence. */
private String readName() {
int length = readNumber();
String name = new String(packedNames, nameIndex, length, ISO_8859_1);
nameIndex += length;
return name;
}
/** Skips a single-byte-encoded string from the packed name sequence. */
private void skipName() {
int length = readNumber();
nameIndex += length;
}
/** Reads an unsigned byte from the packed name sequence. */
private int readNumber() {
return 0xFF & (int) packedNames[nameIndex++];
}
public static InstrumenterIndex readIndex() {
URL indexResource = instrumenterClassLoader.getResource(INSTRUMENTER_INDEX_NAME);
if (null != indexResource) {
try (DataInputStream in =
new DataInputStream(new BufferedInputStream(indexResource.openStream()))) {
int instrumentationCount = in.readInt();
int transformationCount = in.readInt();
int packedNamesLength = in.readInt();
byte[] packedNames = new byte[packedNamesLength];
in.readFully(packedNames);
return new InstrumenterIndex(instrumentationCount, transformationCount, packedNames);
} catch (Throwable e) {
log.error("Problem reading {}", INSTRUMENTER_INDEX_NAME, e);
}
}
return buildIndex(); // fallback to runtime generation when testing
}
public static InstrumenterIndex buildIndex() {
IndexGenerator indexGenerator = new IndexGenerator();
indexGenerator.buildIndex();
// bypass writing to file, convert into structure expected at runtime
return new InstrumenterIndex(
indexGenerator.instrumentationCount,
indexGenerator.transformationCount,
indexGenerator.packedNames.toByteArray());
}
/** Loads instrumentation modules annotated with {@code @AutoService}. */
static List<InstrumenterModule> loadModules(ClassLoader loader) throws IOException {
List<InstrumenterModule> modules = new ArrayList<>();
for (String moduleName : loadModuleNames(loader)) {
try {
@SuppressWarnings({"rawtypes", "unchecked"})
Class<InstrumenterModule> moduleType = (Class) loader.loadClass(moduleName);
modules.add(moduleType.getConstructor().newInstance());
} catch (Throwable e) {
log.error("Failed to load instrumentation module {}", moduleName, e);
}
}
// enforce module ordering (lowest-value first) before indexing
modules.sort(Comparator.comparingInt(InstrumenterModule::order));
return modules;
}
/** Loads the type names of instrumentation modules annotated with {@code @AutoService}. */
private static String[] loadModuleNames(ClassLoader loader) throws IOException {
Set<String> lines = new LinkedHashSet<>();
Enumeration<URL> urls =
loader.getResources("META-INF/services/datadog.trace.agent.tooling.InstrumenterModule");
while (urls.hasMoreElements()) {
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(urls.nextElement().openStream(), UTF_8))) {
String line = reader.readLine();
while (line != null) {
lines.add(line);
line = reader.readLine();
}
}
}
return lines.toArray(new String[0]);
}
/** Generates an index from known {@link InstrumenterModule}s on the build class-path. */
static final class IndexGenerator {
final ByteArrayOutputStream packedNames = new ByteArrayOutputStream();
int instrumentationCount = 0;
int transformationCount = 0;
public void buildIndex() {
log.debug("Generating InstrumenterIndex");
try (DataOutputStream out = new DataOutputStream(packedNames)) {
for (InstrumenterModule module : loadModules(instrumenterClassLoader)) {
String moduleName = module.getClass().getName();
instrumentationCount++;
out.writeByte(moduleName.length());
out.writeBytes(moduleName);
try {
List<Instrumenter> members = module.typeInstrumentations();
if (members.equals(singletonList(module))) {
transformationCount++;
out.writeByte(SELF_MEMBERSHIP);
} else {
out.writeByte(members.size());
for (Instrumenter member : members) {
// we only need the simple name for matching purposes
String memberName = member.getClass().getSimpleName();
transformationCount++;
out.writeByte(memberName.length());
out.writeBytes(memberName);
}
}
} catch (Throwable e) {
log.error("Failed to index instrumentation module {}", moduleName, e);
}
}
} catch (IOException e) {
throw new UncheckedIOException("Problem generating InstrumenterIndex", e);
}
}
public void writeIndex(Path indexFile) throws IOException {
try (DataOutputStream out =
new DataOutputStream(new BufferedOutputStream(Files.newOutputStream(indexFile)))) {
out.writeInt(instrumentationCount);
out.writeInt(transformationCount);
out.writeInt(packedNames.size());
out.write(packedNames.toByteArray());
}
}
/**
* Called from 'generateInstrumenterIndex' task in 'dd-java-agent/instrumentation/build.gradle'.
*/
public static void main(String[] args) throws IOException {
if (args.length < 1) {
throw new IllegalArgumentException("Expected: index-dir");
}
Path indexDir = Paths.get(args[0]).toAbsolutePath();
// satisfy some instrumenters that cache matchers in initializers
HierarchyMatchers.registerIfAbsent(HierarchyMatchers.simpleChecks());
SharedTypePools.registerIfAbsent(SharedTypePools.simpleCache());
IndexGenerator indexGenerator = new IndexGenerator();
indexGenerator.buildIndex();
indexGenerator.writeIndex(indexDir.resolve(INSTRUMENTER_INDEX_NAME));
}
}
}