Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[🍒 7432] Avoid using stdout to report bootstrapping errors #7433

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ void ensureThatApplicationStartsWithAgentOnJava7() throws InterruptedException,
logProcessOutput(output, errors);
assertEquals(0, exitCode, "Command failed with unexpected exit code");
assertTrue(output.contains(expectedMessage), "Output does not contain '" + expectedMessage + "'");
assertTrue(output.stream().anyMatch(WARNING_PATTERN.asPredicate()), "Output does not contain line matching '" + WARNING_PATTERN + "'");
assertTrue(output.contains(UPGRADE_MESSAGE), "Output does not contain '" + UPGRADE_MESSAGE + "'");
assertTrue(errors.stream().anyMatch(WARNING_PATTERN.asPredicate()), "Output does not contain line matching '" + WARNING_PATTERN + "'");
assertTrue(errors.contains(UPGRADE_MESSAGE), "Output does not contain '" + UPGRADE_MESSAGE + "'");
}

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

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