-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathInstrumenterModule.java
More file actions
351 lines (299 loc) · 11.8 KB
/
InstrumenterModule.java
File metadata and controls
351 lines (299 loc) · 11.8 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 datadog.trace.agent.tooling.bytebuddy.matcher.ClassLoaderMatchers.ANY_CLASS_LOADER;
import static java.util.Collections.addAll;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
import static net.bytebuddy.matcher.ElementMatchers.isSynthetic;
import datadog.trace.agent.tooling.iast.IastPostProcessorFactory;
import datadog.trace.agent.tooling.muzzle.Reference;
import datadog.trace.agent.tooling.muzzle.ReferenceMatcher;
import datadog.trace.agent.tooling.muzzle.ReferenceProvider;
import datadog.trace.api.InstrumenterConfig;
import datadog.trace.api.ProductActivation;
import datadog.trace.api.config.ProfilingConfig;
import datadog.trace.bootstrap.config.provider.ConfigProvider;
import datadog.trace.util.Strings;
import de.thetaphi.forbiddenapis.SuppressForbidden;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class InstrumenterModule implements Instrumenter {
/**
* Since several systems share the same instrumentation infrastructure in order to enable only the
* applicable {@link Instrumenter instrumenters} on startup each {@linkplain InstrumenterModule}
* must declare its target system. The systems currently supported include:
*
* <ul>
* <li>{@link TargetSystem#TRACING tracing}
* <li>{@link TargetSystem#PROFILING profiling}
* <li>{@link TargetSystem#APPSEC appsec}
* <li>{@link TargetSystem#IAST iast}
* <li>{@link TargetSystem#CIVISIBILITY ci-visibility}
* <li>{@link TargetSystem#USM usm}
* <li>{@link TargetSystem#CONTEXT_TRACKING context-tracking}
* <li>{@link TargetSystem#RASP rasp}
* </ul>
*/
public enum TargetSystem {
TRACING,
PROFILING,
APPSEC,
IAST,
CIVISIBILITY,
USM,
LLMOBS,
CONTEXT_TRACKING,
RASP,
}
private static final Logger log = LoggerFactory.getLogger(InstrumenterModule.class);
protected static final String[] NO_HELPERS = {};
private final List<String> instrumentationNames;
private final String instrumentationPrimaryName;
private final boolean enabled;
protected final String packageName = Strings.getPackageName(getClass().getName());
public InstrumenterModule(final String instrumentationName, final String... additionalNames) {
instrumentationNames = new ArrayList<>(1 + additionalNames.length);
instrumentationNames.add(instrumentationName);
addAll(instrumentationNames, additionalNames);
instrumentationPrimaryName = instrumentationName;
enabled = InstrumenterConfig.get().isIntegrationEnabled(instrumentationNames, defaultEnabled());
}
public String name() {
return instrumentationPrimaryName;
}
public Iterable<String> names() {
return instrumentationNames;
}
/** Modules with higher order values are applied <i>after</i> those with lower values. */
public int order() {
return 0;
}
public List<Instrumenter> typeInstrumentations() {
return singletonList(this);
}
public final ReferenceMatcher getInstrumentationMuzzle() {
return loadStaticMuzzleReferences(getClass().getClassLoader(), getClass().getName())
.withReferenceProvider(runtimeMuzzleReferences());
}
public static ReferenceMatcher loadStaticMuzzleReferences(
ClassLoader classLoader, String instrumentationClass) {
String muzzleClass = instrumentationClass + "$Muzzle";
try {
// Muzzle class contains static references captured at build-time
// see datadog.trace.agent.tooling.muzzle.MuzzleGenerator
return (ReferenceMatcher) classLoader.loadClass(muzzleClass).getMethod("create").invoke(null);
} catch (Throwable e) {
log.warn("Failed to load - muzzle.class={}", muzzleClass, e);
return ReferenceMatcher.NO_REFERENCES;
}
}
/**
* @return Class names of helpers to inject into the user's classloader.
* <p><b>NOTE:</b> The order of the returned helper classes matters. If a muzzle check fails
* with a NoClassDefFoundError, as logged in build/reports/muzzle-*.txt, it is likely that one
* helper class depends on another that appears later in the list. In this case, the returned
* list must be reordered so that the referred helper class appears before the one that refers
* to it.
*/
public String[] helperClassNames() {
return NO_HELPERS;
}
/**
* @return {@code true} if helper classes should be injected with the agent's {@link CodeSource}
*/
public boolean useAgentCodeSource() {
return false;
}
/** Override this to automatically inject all (non-bootstrap) helper dependencies. */
public boolean injectHelperDependencies() {
return false;
}
/** Classes that the muzzle plugin assumes will be injected */
public String[] muzzleIgnoredClassNames() {
return helperClassNames();
}
/** Override this to supply additional Muzzle references at build time. */
public Reference[] additionalMuzzleReferences() {
return null;
}
/** Override this to supply additional Muzzle references during startup. */
public ReferenceProvider runtimeMuzzleReferences() {
return null;
}
/** Override this to validate against a specific named MuzzleDirective. */
public String muzzleDirective() {
return null;
}
/** Override this to supply additional class-loader requirements. */
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
return ANY_CLASS_LOADER;
}
/**
* @return A type matcher used to ignore some methods when applying transformation.
*/
public ElementMatcher<? super MethodDescription> methodIgnoreMatcher() {
// By default ByteBuddy will skip all methods that are synthetic at the top level, but since
// we need to instrument some synthetic methods in Scala and changed that, we make the default
// here to ignore synthetic methods to not change the behavior for unaware instrumentations
return isSynthetic();
}
/** Override this to apply shading to method advice and injected helpers. */
public Map<String, String> adviceShading() {
return null;
}
/** Override this to post-process the operand stack of any transformed methods. */
public Advice.PostProcessor.Factory postProcessor() {
return null;
}
/**
* Context stores to define for this instrumentation. Are added to matching class loaders.
*
* <p>A map of {class-name -> context-class-name}. Keys (and their subclasses) will be associated
* with a context of the value.
*/
public Map<String, String> contextStore() {
return emptyMap();
}
protected boolean defaultEnabled() {
return InstrumenterConfig.get().isIntegrationsEnabled();
}
public boolean isEnabled() {
return enabled;
}
/**
* Indicates the applicability of an {@linkplain InstrumenterModule} to the given system.<br>
*
* @param enabledSystems a set of all the enabled target systems
* @return {@literal true} if the set of enabled systems contains all the ones required by this
* particular {@linkplain InstrumenterModule}
*/
public boolean isApplicable(Set<TargetSystem> enabledSystems) {
return false;
}
protected final boolean isShortcutMatchingEnabled(boolean defaultToShortcut) {
return InstrumenterConfig.get()
.isIntegrationShortcutMatchingEnabled(singletonList(name()), defaultToShortcut);
}
/** Parent class for all tracing related instrumentations */
public abstract static class Tracing extends InstrumenterModule {
public Tracing(String instrumentationName, String... additionalNames) {
super(instrumentationName, additionalNames);
}
@Override
public final boolean isApplicable(Set<TargetSystem> enabledSystems) {
return enabledSystems.contains(TargetSystem.TRACING);
}
}
/** Parent class for all profiling related instrumentations */
public abstract static class Profiling extends InstrumenterModule {
public Profiling(String instrumentationName, String... additionalNames) {
super(instrumentationName, additionalNames);
}
@Override
public final boolean isApplicable(Set<TargetSystem> enabledSystems) {
return enabledSystems.contains(TargetSystem.PROFILING);
}
@Override
public boolean isEnabled() {
return super.isEnabled()
&& !ConfigProvider.getInstance()
.getBoolean(ProfilingConfig.PROFILING_ULTRA_MINIMAL, false);
}
}
/** Parent class for all AppSec related instrumentations */
public abstract static class AppSec extends InstrumenterModule {
public AppSec(String instrumentationName, String... additionalNames) {
super(instrumentationName, additionalNames);
}
@Override
public final boolean isApplicable(Set<TargetSystem> enabledSystems) {
return enabledSystems.contains(TargetSystem.APPSEC);
}
}
/** Parent class for all IAST related instrumentations */
@SuppressForbidden
public abstract static class Iast extends InstrumenterModule {
public Iast(String instrumentationName, String... additionalNames) {
super(instrumentationName, additionalNames);
}
@Override
public List<Instrumenter> typeInstrumentations() {
preloadClassNames();
return super.typeInstrumentations();
}
@Override
public final boolean isApplicable(Set<TargetSystem> enabledSystems) {
if (enabledSystems.contains(TargetSystem.IAST)) {
return true;
}
final InstrumenterConfig cfg = InstrumenterConfig.get();
if (!isOptOutEnabled() || cfg.isIastFullyDisabled()) {
return false;
}
return cfg.getAppSecActivation() == ProductActivation.FULLY_ENABLED;
}
/**
* Force loading of classes that need to be instrumented, but are using during instrumentation.
*/
private void preloadClassNames() {
String[] list = getClassNamesToBePreloaded();
if (list != null) {
for (String clazz : list) {
try {
Class.forName(clazz);
} catch (Throwable t) {
log.debug("Error force loading {} class", clazz);
}
}
}
}
/** Get classes to force load* */
public String[] getClassNamesToBePreloaded() {
return null;
}
@Override
public Advice.PostProcessor.Factory postProcessor() {
return IastPostProcessorFactory.INSTANCE;
}
protected boolean isOptOutEnabled() {
return false;
}
}
/** Parent class for all USM related instrumentations */
public abstract static class Usm extends InstrumenterModule {
public Usm(String instrumentationName, String... additionalNames) {
super(instrumentationName, additionalNames);
}
@Override
public final boolean isApplicable(Set<TargetSystem> enabledSystems) {
return enabledSystems.contains(TargetSystem.USM);
}
}
/** Parent class for all CI related instrumentations */
public abstract static class CiVisibility extends InstrumenterModule {
public CiVisibility(String instrumentationName, String... additionalNames) {
super(instrumentationName, additionalNames);
}
@Override
public final boolean isApplicable(Set<TargetSystem> enabledSystems) {
return enabledSystems.contains(TargetSystem.CIVISIBILITY);
}
}
/** Parent class for all the context tracking instrumentations */
public abstract static class ContextTracking extends InstrumenterModule {
public ContextTracking(String instrumentationName, String... additionalNames) {
super(instrumentationName, additionalNames);
}
@Override
public final boolean isApplicable(Set<TargetSystem> enabledSystems) {
return enabledSystems.contains(TargetSystem.CONTEXT_TRACKING);
}
}
}