Skip to content

Commit

Permalink
fix(agent): Avoid using stdout to report error
Browse files Browse the repository at this point in the history
Using stdout instead of stderr breaks CLI tools used in shell sub-commands if anything goes wrong.
  • Loading branch information
PerfectSlayer committed Aug 14, 2024
1 parent a28fb20 commit 7f9d97e
Showing 1 changed file with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static boolean exceptionCauseChainContains(Throwable ex, String exClassName) {

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

private static boolean lessThanJava8() {
return lessThanJava8(System.getProperty("java.version"), System.out);
return lessThanJava8(System.getProperty("java.version"), System.err);
}

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

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

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

private static File getAgentFileFromJavaagentArg(List<File> agentFiles) {
if (agentFiles.isEmpty()) {
System.out.println("Could not get bootstrap jar from -javaagent arg: no argument specified");
System.err.println("Could not get bootstrap jar from -javaagent arg: no argument specified");
return null;
} else if (agentFiles.size() > 1) {
System.out.println(
System.err.println(
"Could not get bootstrap jar from -javaagent arg: multiple javaagents specified");
return null;
} else {
Expand All @@ -324,7 +324,7 @@ private static List<File> getAgentFilesFromVMArguments() {
if (agentFile.exists() && agentFile.isFile()) {
agentFiles.add(agentFile);
} else {
System.out.println(
System.err.println(
"Could not get bootstrap jar from -javaagent arg: unable to find javaagent file: "
+ agentFile);
}
Expand Down Expand Up @@ -395,13 +395,13 @@ private static List<String> getVMArgumentsThroughReflection() {

// Fallback to default
try {
System.out.println(
System.err.println(
"WARNING: Unable to get VM args through reflection. A custom java.util.logging.LogManager may not work correctly");
return ManagementFactory.getRuntimeMXBean().getInputArguments();
} catch (final Throwable t) {
// Throws InvocationTargetException on modularized applications
// with non-opened java.management module
System.out.println("WARNING: Unable to get VM args using managed beans");
System.err.println("WARNING: Unable to get VM args using managed beans");
}
return Collections.emptyList();
}
Expand Down

0 comments on commit 7f9d97e

Please sign in to comment.