-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathAgentPreCheck.java
More file actions
204 lines (178 loc) · 6.2 KB
/
AgentPreCheck.java
File metadata and controls
204 lines (178 loc) · 6.2 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
package datadog.trace.bootstrap;
import de.thetaphi.forbiddenapis.SuppressForbidden;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
/** Special lightweight pre-main class that skips installation on incompatible JVMs. */
public class AgentPreCheck {
public static final int MIN_JAVA_VERSION = 8;
public static void premain(final String agentArgs, final Instrumentation inst) {
agentmain(agentArgs, inst);
}
@SuppressForbidden
public static void agentmain(final String agentArgs, final Instrumentation inst) {
try {
if (compatible()) {
continueBootstrap(agentArgs, inst);
}
} catch (Throwable e) {
// If agent failed we should not fail the application.
// We don't have a log manager here, so just print.
System.err.println("ERROR: " + e.getMessage());
}
}
private static void reportIncompatibleJava(
String javaVersion, String javaHome, String agentVersion, PrintStream output) {
output.println(
"Warning: "
+ (agentVersion == null ? "This version" : "Version " + agentVersion)
+ " of dd-java-agent is not compatible with Java "
+ javaVersion
+ " found at '"
+ javaHome
+ "' and is effectively disabled.");
output.println("Please upgrade your Java version to 8+");
}
static void sendTelemetry(String forwarderPath, String javaVersion, String agentVersion) {
// Hardcoded payload for unsupported Java version.
String payload =
"{\"metadata\":{"
+ "\"runtime_name\":\"jvm\","
+ "\"language_name\":\"jvm\","
+ "\"runtime_version\":\""
+ javaVersion
+ "\","
+ "\"language_version\":\""
+ javaVersion
+ "\","
+ "\"tracer_version\":\""
+ agentVersion
+ "\","
+ "\"result\":\"abort\","
+ "\"result_class\":\"unknown\","
+ "\"result_reason\":\"incompatible_runtime\""
+ "},"
+ "\"points\":[{"
+ "\"name\":\"library_entrypoint.abort\","
+ "\"tags\":[\"reason:incompatible_runtime\"]"
+ "}]"
+ "}";
ForwarderJsonSenderThread t = new ForwarderJsonSenderThread(forwarderPath, payload);
t.setDaemon(true);
t.start();
}
private static String tryGetProperty(String property) {
try {
return System.getProperty(property);
} catch (SecurityException e) {
return null;
}
}
@SuppressForbidden
private static boolean compatible() {
String javaVersion = tryGetProperty("java.version");
String javaHome = tryGetProperty("java.home");
return compatible(javaVersion, javaHome, System.err);
}
// Reachable for testing
// System.getenv usage is necessary since class is designed to be Java 6 compatible, while
// Environment component is for Java 8+
@SuppressForbidden
static boolean compatible(String javaVersion, String javaHome, PrintStream output) {
int majorJavaVersion = parseJavaMajorVersion(javaVersion);
if (majorJavaVersion >= MIN_JAVA_VERSION) {
return true;
}
String agentVersion = getAgentVersion();
reportIncompatibleJava(javaVersion, javaHome, agentVersion, output);
String forwarderPath = System.getenv("DD_TELEMETRY_FORWARDER_PATH");
if (forwarderPath != null) {
sendTelemetry(forwarderPath, javaVersion, agentVersion);
}
return false;
}
// Reachable for testing
static int parseJavaMajorVersion(String javaVersion) {
int major = 0;
if (javaVersion == null || javaVersion.isEmpty()) {
return major;
}
int start = 0;
if (javaVersion.charAt(0) == '1'
&& javaVersion.length() >= 3
&& javaVersion.charAt(1) == '.'
&& Character.isDigit(javaVersion.charAt(2))) {
start = 2;
}
// Parse the major digit and be a bit lenient, allowing digits followed by any non digit
for (int i = start; i < javaVersion.length(); i++) {
char c = javaVersion.charAt(i);
if (Character.isDigit(c)) {
major *= 10;
major += Character.digit(c, 10);
} else {
break;
}
}
return major;
}
private static String getAgentVersion() {
try {
// Get the resource as an InputStream
InputStream is = AgentPreCheck.class.getResourceAsStream("/dd-java-agent.version");
if (is == null) {
return null;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
final StringBuilder sb = new StringBuilder();
for (int c = reader.read(); c != -1; c = reader.read()) {
sb.append((char) c);
}
reader.close();
return sb.toString().trim();
} catch (Throwable e) {
return null;
}
}
@SuppressForbidden
private static void continueBootstrap(final String agentArgs, final Instrumentation inst)
throws Exception {
Class<?> clazz = Class.forName("datadog.trace.bootstrap.AgentBootstrap");
Method agentMain = clazz.getMethod("agentmain", String.class, Instrumentation.class);
agentMain.invoke(null, agentArgs, inst);
}
public static final class ForwarderJsonSenderThread extends Thread {
private final String forwarderPath;
private final String payload;
public ForwarderJsonSenderThread(String forwarderPath, String payload) {
super("dd-forwarder-json-sender");
setDaemon(true);
this.forwarderPath = forwarderPath;
this.payload = payload;
}
@SuppressForbidden
@Override
public void run() {
ProcessBuilder builder = new ProcessBuilder(forwarderPath, "library_entrypoint");
try {
Process process = builder.start();
OutputStream out = null;
try {
out = process.getOutputStream();
out.write(payload.getBytes(StandardCharsets.UTF_8));
} finally {
if (out != null) {
out.close();
}
}
} catch (Throwable e) {
System.err.println("Failed to send telemetry: " + e.getMessage());
}
}
}
}