-
Notifications
You must be signed in to change notification settings - Fork 300
/
Copy pathInstrumenterModule.java
295 lines (250 loc) · 10 KB
/
InstrumenterModule.java
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
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.config.ProfilingConfig;
import datadog.trace.bootstrap.config.provider.ConfigProvider;
import datadog.trace.util.Strings;
import de.thetaphi.forbiddenapis.SuppressForbidden;
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}
* </ul>
*/
public enum TargetSystem {
TRACING,
PROFILING,
APPSEC,
IAST,
CIVISIBILITY,
USM
}
private static final Logger log = LoggerFactory.getLogger(InstrumenterModule.class);
private final int instrumentationId;
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) {
instrumentationId = Instrumenters.currentInstrumentationId();
instrumentationNames = new ArrayList<>(1 + additionalNames.length);
instrumentationNames.add(instrumentationName);
addAll(instrumentationNames, additionalNames);
instrumentationPrimaryName = instrumentationName;
enabled = InstrumenterConfig.get().isIntegrationEnabled(instrumentationNames, defaultEnabled());
}
public int instrumentationId() {
return instrumentationId;
}
public String name() {
return instrumentationPrimaryName;
}
public Iterable<String> names() {
return instrumentationNames;
}
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 */
public String[] helperClassNames() {
return new String[0];
}
/** 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<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();
}
/**
* 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 implements HasMethodAdvice {
public Tracing(String instrumentationName, String... additionalNames) {
super(instrumentationName, additionalNames);
}
@Override
public boolean isApplicable(Set<TargetSystem> enabledSystems) {
return enabledSystems.contains(TargetSystem.TRACING);
}
}
/** Parent class for all profiling related instrumentations */
public abstract static class Profiling extends InstrumenterModule implements HasMethodAdvice {
public Profiling(String instrumentationName, String... additionalNames) {
super(instrumentationName, additionalNames);
}
@Override
public 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 implements HasMethodAdvice {
public AppSec(String instrumentationName, String... additionalNames) {
super(instrumentationName, additionalNames);
}
@Override
public 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
implements HasMethodAdvice, WithPostProcessor {
public Iast(String instrumentationName, String... additionalNames) {
super(instrumentationName, additionalNames);
}
@Override
public List<Instrumenter> typeInstrumentations() {
preloadClassNames();
return super.typeInstrumentations();
}
@Override
public boolean isApplicable(Set<TargetSystem> enabledSystems) {
return enabledSystems.contains(TargetSystem.IAST);
}
/**
* 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;
}
}
/** Parent class for all USM related instrumentations */
public abstract static class Usm extends InstrumenterModule implements HasMethodAdvice {
public Usm(String instrumentationName, String... additionalNames) {
super(instrumentationName, additionalNames);
}
@Override
public boolean isApplicable(Set<TargetSystem> enabledSystems) {
return enabledSystems.contains(TargetSystem.USM);
}
}
/** Parent class for all CI related instrumentations */
public abstract static class CiVisibility extends InstrumenterModule implements HasMethodAdvice {
public CiVisibility(String instrumentationName, String... additionalNames) {
super(instrumentationName, additionalNames);
}
@Override
public boolean isApplicable(Set<TargetSystem> enabledSystems) {
return enabledSystems.contains(TargetSystem.CIVISIBILITY);
}
}
}