-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathOOMENotifierScriptInitializer.java
More file actions
183 lines (165 loc) · 6.23 KB
/
OOMENotifierScriptInitializer.java
File metadata and controls
183 lines (165 loc) · 6.23 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
package datadog.crashtracking;
import static datadog.crashtracking.Initializer.LOG;
import static datadog.crashtracking.Initializer.RWXRWXRWX;
import static datadog.crashtracking.Initializer.R_XR_XR_X;
import static datadog.crashtracking.Initializer.findAgentJar;
import static datadog.crashtracking.Initializer.getOomeNotifierTemplate;
import static datadog.crashtracking.Initializer.getScriptPathFromArg;
import static datadog.crashtracking.Initializer.pidFromSpecialFileName;
import static datadog.crashtracking.Initializer.writeConfig;
import static datadog.trace.api.telemetry.LogCollector.SEND_TELEMETRY;
import static java.nio.file.FileVisitResult.CONTINUE;
import static java.nio.file.attribute.PosixFilePermissions.asFileAttribute;
import static java.nio.file.attribute.PosixFilePermissions.fromString;
import datadog.trace.api.Config;
import datadog.trace.util.PidHelper;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Set;
import java.util.stream.Collectors;
public final class OOMENotifierScriptInitializer {
private static final String OOME_NOTIFIER_SCRIPT_PREFIX = "dd_oome_notifier.";
private OOMENotifierScriptInitializer() {}
// @VisibleForTests
static void initialize(String onOutOfMemoryVal) {
if (onOutOfMemoryVal == null || onOutOfMemoryVal.isEmpty()) {
LOG.debug(
SEND_TELEMETRY,
"'-XX:OnOutOfMemoryError' argument was not provided. OOME tracking is disabled.");
return;
}
Path scriptPath = getOOMEScriptPath(onOutOfMemoryVal);
if (scriptPath == null) {
LOG.error(
SEND_TELEMETRY,
"OOME notifier script value ({}) does not follow the expected format: <path>/dd_oome_notifier.(sh|bat) %p. OOME tracking is disabled.",
onOutOfMemoryVal);
return;
}
String agentJar = findAgentJar();
if (agentJar == null) {
LOG.warn(
SEND_TELEMETRY,
"Unable to locate the agent jar. OOME notification will not work properly.");
return;
}
if (!copyOOMEscript(scriptPath)) {
return;
}
String tags = getTags();
writeConfig(scriptPath, "agent", agentJar, "tags", tags);
}
private static String getTags() {
return Config.get().getMergedJmxTags().entrySet().stream()
.map(e -> e.getKey() + ":" + e.getValue())
.collect(Collectors.joining(","));
}
private static Path getOOMEScriptPath(String onOutOfMemoryVal) {
String path = getScriptPathFromArg(onOutOfMemoryVal, OOME_NOTIFIER_SCRIPT_PREFIX);
return path == null ? null : Paths.get(path);
}
private static boolean copyOOMEscript(Path scriptPath) {
Path scriptDirectory = scriptPath.getParent();
// cleanup all stale process-specific generated files in the parent folder of the given OOME
// notifier script
ScriptCleanupVisitor.run(scriptDirectory);
try {
if (Files.exists(scriptDirectory)) {
// can be safely ignored; if the folder exists we will just reuse it
if (!Files.isWritable(scriptDirectory)) {
LOG.warn(
SEND_TELEMETRY,
"Read only directory {}. OOME notification will not work properly.",
scriptDirectory);
return false;
}
} else {
Files.createDirectories(scriptDirectory, asFileAttribute(fromString(RWXRWXRWX)));
}
} catch (UnsupportedOperationException e) {
LOG.warn(
SEND_TELEMETRY,
"Unsupported permissions '{"
+ RWXRWXRWX
+ "' for {}. OOME notification will not work properly.",
scriptDirectory);
return false;
} catch (FileAlreadyExistsException ignored) {
LOG.warn(SEND_TELEMETRY, "Path {} already exists and is not a directory.", scriptDirectory);
return false;
} catch (IOException e) {
LOG.warn(
SEND_TELEMETRY,
"Failed to create writable OOME script folder {}. OOME notification will not work properly.",
scriptDirectory);
return false;
}
try {
// do not overwrite existing
if (!Files.exists(scriptPath)) {
Files.copy(getOomeNotifierTemplate(), scriptPath);
}
Files.setPosixFilePermissions(scriptPath, fromString(R_XR_XR_X));
} catch (IOException e) {
LOG.warn(
SEND_TELEMETRY,
"Failed to copy OOME script {}. OOME notification will not work properly.",
scriptPath);
return false;
}
return true;
}
private static class ScriptCleanupVisitor implements FileVisitor<Path> {
private Set<String> pidSet;
static void run(Path dir) {
try {
if (Files.exists(dir)) {
Files.walkFileTree(dir, new ScriptCleanupVisitor());
}
} catch (IOException e) {
if (LOG.isDebugEnabled()) {
LOG.info("Failed cleaning up process specific files in {}", dir, e);
} else {
LOG.info("Failed cleaning up process specific files in {}: {}", dir, e.toString());
}
}
}
private ScriptCleanupVisitor() {}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
return CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String fileName = file.getFileName().toString();
String pid = pidFromSpecialFileName(fileName);
if (pid != null && !pid.equals(PidHelper.getPid())) {
if (this.pidSet == null) {
// if pidSet is not initialized, initialize it
// this will fork jps to get the list of Java PIDs
this.pidSet = PidHelper.getJavaPids();
}
if (!this.pidSet.contains(pid)) {
LOG.debug("Cleaning process specific file {}", file);
Files.delete(file);
}
}
return CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
LOG.debug("Failed to delete file {}", file, exc);
return CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
return CONTINUE;
}
}
}