-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathAgentCLI.java
More file actions
178 lines (162 loc) · 6.14 KB
/
AgentCLI.java
File metadata and controls
178 lines (162 loc) · 6.14 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
package datadog.trace.agent.tooling;
import datadog.crashtracking.CrashUploader;
import datadog.crashtracking.OOMENotifier;
import datadog.trace.agent.tooling.bytebuddy.SharedTypePools;
import datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers;
import datadog.trace.agent.tooling.profiler.EnvironmentChecker;
import datadog.trace.bootstrap.Agent;
import datadog.trace.bootstrap.InitializationTelemetry;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import de.thetaphi.forbiddenapis.SuppressForbidden;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
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.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Consumer;
import java.util.jar.JarFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** CLI methods, used when running the agent as a sample application with -jar. */
public final class AgentCLI {
private static final Logger log = LoggerFactory.getLogger(AgentCLI.class);
static {
SharedTypePools.registerIfAbsent(SharedTypePools.simpleCache());
HierarchyMatchers.registerIfAbsent(HierarchyMatchers.simpleChecks());
}
/** Prints all known integrations in alphabetical order. */
@SuppressForbidden
public static void printIntegrationNames() {
Set<String> names = new TreeSet<>();
for (InstrumenterModule module : InstrumenterIndex.readIndex().modules()) {
names.add(module.name());
}
for (String name : names) {
System.out.println(name);
}
}
/**
* Sends sample traces at a regular interval for diagnostic purposes.
*
* @param count how many traces to send, negative means send forever
* @param interval the interval (in seconds) to wait for each trace
*/
@SuppressForbidden
public static void sendSampleTraces(final int count, final double interval) throws Exception {
Agent.startDatadogTracer(InitializationTelemetry.noOpInstance());
int numTraces = 0;
while (++numTraces <= count || count < 0) {
AgentSpan span = AgentTracer.startSpan("sample");
try {
Thread.sleep(Math.max((long) (1000.0 * interval), 1L));
} catch (InterruptedException ignore) {
} finally {
span.finish();
}
if (count < 0) {
System.out.print("... completed " + numTraces + (numTraces < 2 ? " trace\r" : " traces\r"));
} else {
System.out.print("... completed " + numTraces + "/" + count + " traces\r");
}
}
}
public static void uploadCrash(final String[] args) throws Exception {
CrashUploader uploader = new CrashUploader();
List<Path> files = new ArrayList<>(args.length);
for (String arg : args) {
Path path = Paths.get(arg);
if (!Files.exists(path)) {
log.error("Crash log {} does not exist", arg);
System.exit(1);
}
files.add(Paths.get(arg));
}
uploader.upload(files);
}
public static void sendOomeEvent(String taglist) throws Exception {
OOMENotifier.sendOomeEvent(taglist);
}
@SuppressForbidden
public static void scanDependencies(final String[] args) throws Exception {
Class depClass =
Class.forName(
"datadog.telemetry.dependency.DependencyService",
true,
AgentCLI.class.getClassLoader());
Object depService = depClass.getConstructor().newInstance();
Method addUrlMethod = depService.getClass().getMethod("addURL", URL.class);
Method resolveOne = depService.getClass().getMethod("resolveOneDependency");
Consumer<File> invoker =
(file) -> {
try {
addUrlMethod.invoke(depService, file.toURI().toURL());
resolveOne.invoke(depService);
} catch (Exception e) {
log.error("Error invoking dependencies service", e);
}
};
File origin = new File(args[0]);
if (origin.isFile()) {
recursiveDependencySearch(invoker, origin);
} else if (origin.isDirectory()) {
File[] files = origin.listFiles();
for (File file : files) {
recursiveDependencySearch(invoker, file);
}
} else {
System.err.println("Invalid path found:" + origin.getAbsolutePath());
}
System.out.println("Scan finished");
}
public static void checkProfilerEnv(String temp) {
if (!EnvironmentChecker.checkEnvironment(temp)) {
System.exit(1);
}
}
private static void recursiveDependencySearch(Consumer<File> invoker, File origin)
throws IOException {
invoker.accept(origin);
unzipJar(invoker, origin);
}
private static void unzipJar(Consumer<File> invoker, File file) throws IOException {
try (JarFile jar = new JarFile(file)) {
log.debug("Finding entries in file: {}", file.getName());
jar.stream()
.forEach(
e -> {
if (e.getName().endsWith(".jar") || e.getName().endsWith(".war")) {
try {
log.debug("Jar entry found in file: {} entry: {}", file.getName(), e.getName());
File temp = File.createTempFile("internal", ".jar");
try (InputStream is = jar.getInputStream(e);
OutputStream out = new FileOutputStream(temp)) {
int read;
while ((read = is.read()) != -1) {
out.write(read);
}
}
log.debug("Adding new jar: {}", temp.getAbsolutePath());
recursiveDependencySearch(invoker, temp);
if (!temp.delete()) {
log.error("Error deleting temp file: {}", temp.getAbsolutePath());
}
} catch (Exception ex) {
log.error("Error unzipping file", ex);
}
} else {
log.debug("Entry: {} ignored in file: {}", e.getName(), file.getAbsolutePath());
}
});
}
}
}