Skip to content

Commit b4db634

Browse files
fix(agent): Avoid using stdout to report error (#7432)
Using stdout instead of stderr breaks CLI tools used in shell sub-commands if anything goes wrong.
1 parent 0e6de70 commit b4db634

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

dd-java-agent/src/main/java/datadog/trace/bootstrap/AgentBootstrap.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static boolean exceptionCauseChainContains(Throwable ex, String exClassName) {
116116

117117
private static boolean alreadyInitialized() {
118118
if (initialized) {
119-
System.out.println(
119+
System.err.println(
120120
"Warning: dd-java-agent is being initialized more than once. Please check that you are defining -javaagent:dd-java-agent.jar only once.");
121121
return true;
122122
}
@@ -125,7 +125,7 @@ private static boolean alreadyInitialized() {
125125
}
126126

127127
private static boolean lessThanJava8() {
128-
return lessThanJava8(System.getProperty("java.version"), System.out);
128+
return lessThanJava8(System.getProperty("java.version"), System.err);
129129
}
130130

131131
// Reachable for testing
@@ -234,7 +234,7 @@ && getAgentFilesFromVMArguments().size() > 1) {
234234
agentFiles.append(agentFile.getAbsolutePath());
235235
agentFiles.append('"');
236236
}
237-
System.out.println(
237+
System.err.println(
238238
"Info: multiple JVM agents detected, found "
239239
+ agentFiles
240240
+ ". Loading multiple APM/Tracing agent is not a recommended or supported configuration."
@@ -267,14 +267,14 @@ private static synchronized URL installAgentJar(final Instrumentation inst)
267267
}
268268
}
269269

270-
System.out.println("Could not get bootstrap jar from code source, using -javaagent arg");
270+
System.err.println("Could not get bootstrap jar from code source, using -javaagent arg");
271271
File javaagentFile = getAgentFileFromJavaagentArg(getAgentFilesFromVMArguments());
272272
if (javaagentFile != null) {
273273
URL ddJavaAgentJarURL = javaagentFile.toURI().toURL();
274274
return appendAgentToBootstrapClassLoaderSearch(inst, ddJavaAgentJarURL, javaagentFile);
275275
}
276276

277-
System.out.println(
277+
System.err.println(
278278
"Could not get agent jar from -javaagent arg, using ClassLoader#getResource");
279279
javaagentFile = getAgentFileUsingClassLoaderLookup();
280280
if (!javaagentFile.isDirectory()) {
@@ -295,10 +295,10 @@ private static URL appendAgentToBootstrapClassLoaderSearch(
295295

296296
private static File getAgentFileFromJavaagentArg(List<File> agentFiles) {
297297
if (agentFiles.isEmpty()) {
298-
System.out.println("Could not get bootstrap jar from -javaagent arg: no argument specified");
298+
System.err.println("Could not get bootstrap jar from -javaagent arg: no argument specified");
299299
return null;
300300
} else if (agentFiles.size() > 1) {
301-
System.out.println(
301+
System.err.println(
302302
"Could not get bootstrap jar from -javaagent arg: multiple javaagents specified");
303303
return null;
304304
} else {
@@ -324,7 +324,7 @@ private static List<File> getAgentFilesFromVMArguments() {
324324
if (agentFile.exists() && agentFile.isFile()) {
325325
agentFiles.add(agentFile);
326326
} else {
327-
System.out.println(
327+
System.err.println(
328328
"Could not get bootstrap jar from -javaagent arg: unable to find javaagent file: "
329329
+ agentFile);
330330
}
@@ -395,13 +395,13 @@ private static List<String> getVMArgumentsThroughReflection() {
395395

396396
// Fallback to default
397397
try {
398-
System.out.println(
398+
System.err.println(
399399
"WARNING: Unable to get VM args through reflection. A custom java.util.logging.LogManager may not work correctly");
400400
return ManagementFactory.getRuntimeMXBean().getInputArguments();
401401
} catch (final Throwable t) {
402402
// Throws InvocationTargetException on modularized applications
403403
// with non-opened java.management module
404-
System.out.println("WARNING: Unable to get VM args using managed beans");
404+
System.err.println("WARNING: Unable to get VM args using managed beans");
405405
}
406406
return Collections.emptyList();
407407
}

test-published-dependencies/agent-logs-on-java-7/src/test/java/StartWithAgentTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ void ensureThatApplicationStartsWithAgentOnJava7() throws InterruptedException,
2727
logProcessOutput(output, errors);
2828
assertEquals(0, exitCode, "Command failed with unexpected exit code");
2929
assertTrue(output.contains(expectedMessage), "Output does not contain '" + expectedMessage + "'");
30-
assertTrue(output.stream().anyMatch(WARNING_PATTERN.asPredicate()), "Output does not contain line matching '" + WARNING_PATTERN + "'");
31-
assertTrue(output.contains(UPGRADE_MESSAGE), "Output does not contain '" + UPGRADE_MESSAGE + "'");
30+
assertTrue(errors.stream().anyMatch(WARNING_PATTERN.asPredicate()), "Output does not contain line matching '" + WARNING_PATTERN + "'");
31+
assertTrue(errors.contains(UPGRADE_MESSAGE), "Output does not contain '" + UPGRADE_MESSAGE + "'");
3232
}
3333

3434
@Test
@@ -50,8 +50,8 @@ private static void ensureThatApplicationStartsWithoutWarning(String version) th
5050
logProcessOutput(output, errors);
5151
assertEquals(0, exitCode, "Command failed with unexpected exit code");
5252
assertTrue(output.contains(expectedMessage), "Output does not contain '" + expectedMessage + "'");
53-
assertFalse(output.stream().anyMatch(WARNING_PATTERN.asPredicate()), "Output contains unexpected line matching '" + WARNING_PATTERN + "'");
54-
assertFalse(output.contains(UPGRADE_MESSAGE), "Output contains unexpected line '" + UPGRADE_MESSAGE + "'");
53+
assertFalse(errors.stream().anyMatch(WARNING_PATTERN.asPredicate()), "Output contains unexpected line matching '" + WARNING_PATTERN + "'");
54+
assertFalse(errors.contains(UPGRADE_MESSAGE), "Output contains unexpected line '" + UPGRADE_MESSAGE + "'");
5555
}
5656

5757
private static Process startAndWaitForJvmWithAgentForJava(String javaHomeEnv, String message) throws IOException {

0 commit comments

Comments
 (0)