-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathInitializer.java
More file actions
356 lines (319 loc) · 12.1 KB
/
Initializer.java
File metadata and controls
356 lines (319 loc) · 12.1 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
352
353
354
355
356
package datadog.crashtracking;
import static datadog.trace.api.telemetry.LogCollector.SEND_TELEMETRY;
import static datadog.trace.util.AgentThreadFactory.AGENT_THREAD_GROUP;
import static java.util.Comparator.reverseOrder;
import static java.util.Locale.ROOT;
import com.datadoghq.profiler.JVMAccess;
import com.sun.management.HotSpotDiagnosticMXBean;
import datadog.environment.OperatingSystem;
import datadog.environment.SystemProperties;
import datadog.libs.ddprof.DdprofLibraryLoader;
import datadog.trace.util.PidHelper;
import datadog.trace.util.TempLocationManager;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.lang.management.ManagementFactory;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.StringTokenizer;
import java.util.function.Predicate;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class Initializer {
static final Logger LOG = LoggerFactory.getLogger(Initializer.class);
static final String PID_PREFIX = "_pid";
static final String RWXRWXRWX = "rwxrwxrwx";
static final String R_XR_XR_X = "r-xr-xr-x";
private interface FlagAccess {
String getValue(String flagName);
boolean setValue(String flagName, String value);
}
private static final class JVMFlagAccess implements FlagAccess {
private final JVMAccess.Flags flags;
JVMFlagAccess(JVMAccess.Flags flags) {
this.flags = flags;
}
@Override
public String getValue(String flagName) {
return flags.getStringFlag(flagName);
}
@Override
public boolean setValue(String flagName, String value) {
flags.setStringFlag(flagName, value);
return flags.getStringFlag(flagName).equals(value);
}
}
private static final class JMXFlagAccess implements FlagAccess {
private final HotSpotDiagnosticMXBean diagBean;
JMXFlagAccess(HotSpotDiagnosticMXBean diagBean) {
this.diagBean = diagBean;
}
@Override
public String getValue(String flagName) {
return diagBean.getVMOption(flagName).getValue();
}
@Override
public boolean setValue(String flagName, String value) {
// cannot really set the underlying JVM flag value
// let's pretend everything went just fine
return true;
}
}
public static boolean initialize(boolean forceJmx) {
try {
FlagAccess access = null;
if (forceJmx) {
access =
new JMXFlagAccess(ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class));
} else {
DdprofLibraryLoader.JVMAccessHolder jvmAccessHolder = DdprofLibraryLoader.jvmAccess();
Throwable reasonNotLoaded = jvmAccessHolder.getReasonNotLoaded();
if (reasonNotLoaded != null) {
LOG.debug(
SEND_TELEMETRY,
"Failed to load JVM access library: "
+ jvmAccessHolder.getReasonNotLoaded().getMessage()
+ ". Crash tracking will need to rely on user provided JVM arguments.");
return false;
} else {
JVMAccess.Flags flags = jvmAccessHolder.getComponent().flags();
access = new JVMFlagAccess(flags);
}
}
initializeCrashUploader(access);
initializeOOMENotifier(access);
return true;
} catch (Throwable t) {
LOG.debug(SEND_TELEMETRY, "Failed to initialize crash tracking: " + t.getMessage(), t);
}
return false;
}
static InputStream getCrashUploaderTemplate() {
String name = OperatingSystem.isWindows() ? "upload_crash.bat" : "upload_crash.sh";
return CrashUploader.class.getResourceAsStream(name);
}
static InputStream getOomeNotifierTemplate() {
String name = OperatingSystem.isWindows() ? "notify_oome.bat" : "notify_oome.sh";
return OOMENotifier.class.getResourceAsStream(name);
}
static String findAgentJar() {
String agentPath = null;
String classResourceName = CrashUploader.class.getName().replace('.', '/') + ".class";
URL classResource = CrashUploader.class.getClassLoader().getResource(classResourceName);
String selfClass = classResource == null ? "null" : classResource.toString();
if (selfClass.startsWith("jar:file:")) {
int idx = selfClass.lastIndexOf(".jar");
if (idx > -1) {
agentPath = selfClass.substring(9, idx + 4);
}
}
// test harness env is different; use the known project structure to locate the agent jar
else if (selfClass.startsWith("file:")) {
int idx = selfClass.lastIndexOf("dd-java-agent");
if (idx > -1) {
Path libsPath = Paths.get(selfClass.substring(5, idx + 13), "build", "libs");
try (Stream<Path> files = Files.walk(libsPath)) {
Predicate<Path> isJarFile =
p -> p.getFileName().toString().toLowerCase(ROOT).endsWith(".jar");
agentPath =
files
.sorted(reverseOrder())
.filter(isJarFile)
.findFirst()
.map(Path::toString)
.orElse(null);
} catch (IOException ignored) {
// Ignore failure to get agent path
}
}
}
return agentPath;
}
static void writeConfig(Path scriptPath, String... entries) {
String cfgFileName = getBaseName(scriptPath) + PID_PREFIX + PidHelper.getPid() + ".cfg";
Path cfgPath = scriptPath.resolveSibling(cfgFileName);
LOG.debug("Writing config file: {}", cfgPath);
try (BufferedWriter bw = Files.newBufferedWriter(cfgPath)) {
for (int i = 0; i < entries.length; i += 2) {
bw.write(entries[i]);
bw.write('=');
bw.write(entries[i + 1]);
bw.newLine();
}
bw.write("java_home=" + SystemProperties.get("java.home"));
bw.newLine();
Runtime.getRuntime()
.addShutdownHook(
new Thread(
AGENT_THREAD_GROUP,
() -> {
try {
LOG.debug("Deleting config file: {}", cfgPath);
Files.deleteIfExists(cfgPath);
} catch (IOException e) {
LOG.warn(SEND_TELEMETRY, "Failed deleting config file: {}", cfgPath, e);
}
}));
LOG.debug("Config file written: {}", cfgPath);
} catch (IOException e) {
LOG.warn(SEND_TELEMETRY, "Failed writing config file: {}", cfgPath);
try {
Files.deleteIfExists(cfgPath);
} catch (IOException ignored) {
// ignore
}
}
}
static String pidFromSpecialFileName(String fileName) {
if (fileName == null || fileName.isEmpty()) {
return null;
}
int index = fileName.indexOf(PID_PREFIX);
if (index < 0) {
return null; // not a process specific file
}
int pos = index + PID_PREFIX.length();
int startPos = pos;
// check if the file name contains a PID
if (fileName.length() <= pos) {
return null; // no PID in the file name
}
// extract the PID from the file name
// eg. pid_12345.log -> 12345
while (pos < fileName.length() && Character.isDigit(fileName.charAt(pos))) {
pos++;
}
return fileName.substring(startPos, pos);
}
static String getScriptPathFromArg(String arg, String scriptNamePrefix) {
if (arg == null || arg.isEmpty()) {
return null;
}
arg = arg.toLowerCase();
int idx = arg.indexOf(scriptNamePrefix);
if (idx < 0) {
// the script name is not present in the value, so we cannot extract the path
return null;
}
// the script name is present, so we can extract the path
char ch;
idx += scriptNamePrefix.length();
while (idx < arg.length() && (ch = arg.charAt(idx)) != ' ' && ch != ';') {
idx++;
}
String path = arg.substring(0, idx);
idx = path.lastIndexOf(';'); // the arg may contain multiple commands separated by semicolons
if (idx >= 0) {
// if there is a semicolon, we take the part after it and trim it
path = path.substring(idx + 1).trim();
}
return path;
}
private static String getBaseName(Path path) {
String filename = path.getFileName().toString();
int dotIndex = filename.lastIndexOf('.');
if (dotIndex == -1) {
return filename;
}
return filename.substring(0, dotIndex);
}
/**
* If the value of `-XX:OnError` JVM argument is referring to `dd_crash_uploader.sh` or
* `dd_crash_uploader.bat` and the script does not exist it will be created and prefilled with
* code ensuring the error log upload will be triggered on JVM crash.
*/
private static void initializeCrashUploader(FlagAccess flags) {
try {
String onErrorVal = flags.getValue("OnError");
String onErrorFile = flags.getValue("ErrorFile");
String uploadScript = getScript("dd_crash_uploader");
if (onErrorVal == null || onErrorVal.isEmpty()) {
onErrorVal = uploadScript;
} else if (!onErrorVal.contains("dd_crash_uploader")) {
// we can chain scripts so let's preserve the original value in addition to our crash
// uploader
onErrorVal = uploadScript + "; " + onErrorVal;
} else {
StringTokenizer st = new StringTokenizer(onErrorVal, ";");
while (st.hasMoreTokens()) {
String part = st.nextToken();
if (part.trim().contains("dd_crash_uploader")) {
// reuse the existing script name
uploadScript = part.trim().replace(" %p", "");
break;
}
}
}
// set the JVM flag
boolean rslt = flags.setValue("OnError", onErrorVal);
if (!rslt && LOG.isDebugEnabled()) {
LOG.debug(
SEND_TELEMETRY,
"Unable to set OnError flag to " + onErrorVal + ". Crash-tracking may not work.");
}
CrashUploaderScriptInitializer.initialize(uploadScript, onErrorFile);
} catch (Throwable t) {
logInitializationError(
"Unexpected exception while creating custom crash upload script. Crash tracking will not work properly.",
t);
}
}
private static void initializeOOMENotifier(FlagAccess flags) {
try {
String onOutOfMemoryVal = flags.getValue("OnOutOfMemoryError");
String notifierScript = getScript("dd_oome_notifier");
if (onOutOfMemoryVal == null || onOutOfMemoryVal.isEmpty()) {
onOutOfMemoryVal = notifierScript;
} else if (!onOutOfMemoryVal.contains("dd_oome_notifier")) {
// we can chain scripts so let's preserve the original value in addition to our oome tracker
onOutOfMemoryVal = notifierScript + "; " + onOutOfMemoryVal;
} else {
StringTokenizer st = new StringTokenizer(onOutOfMemoryVal, ";");
while (st.hasMoreTokens()) {
String part = st.nextToken();
if (part.trim().contains("dd_oome_notifier")) {
// reuse the existing script name
notifierScript = part.trim();
break;
}
}
}
// set the JVM flag
boolean rslt = flags.setValue("OnOutOfMemoryError", onOutOfMemoryVal);
if (!rslt && LOG.isDebugEnabled()) {
LOG.debug(
SEND_TELEMETRY,
"Unable to set OnOutOfMemoryError flag to "
+ onOutOfMemoryVal
+ ". OOME tracking may not work.");
}
OOMENotifierScriptInitializer.initialize(notifierScript);
} catch (Throwable t) {
logInitializationError(
"Unexpected exception while initializing OOME notifier. OOMEs will not be tracked.", t);
}
}
private static String getScript(String scriptName) {
return TempLocationManager.getInstance().getTempDir().toString()
+ "/"
+ getScriptFileName(scriptName)
+ " %p";
}
private static String getScriptFileName(String scriptName) {
return scriptName + "." + (OperatingSystem.isWindows() ? "bat" : "sh");
}
private static void logInitializationError(String msg, Throwable t) {
if (LOG.isDebugEnabled()) {
LOG.warn(SEND_TELEMETRY, msg, t);
} else {
LOG.warn(
SEND_TELEMETRY,
msg + " [{}] (Change the logging level to debug to see the full stacktrace)",
t.getMessage());
}
}
}