Skip to content

Add system property to force injection of the tracing library even though multiple javaagents have been detected #8697

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

Merged
merged 3 commits into from
Apr 18, 2025
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
@@ -1,5 +1,6 @@
package datadog.trace.bootstrap;

import static datadog.trace.bootstrap.SystemUtils.getPropertyOrEnvVar;
import static java.nio.charset.StandardCharsets.UTF_8;

import datadog.cli.CLIHelper;
Expand Down Expand Up @@ -44,8 +45,8 @@
* </ul>
*/
public final class AgentBootstrap {
static final String LIB_INJECTION_ENABLED_FLAG = "DD_INJECTION_ENABLED";
static final String LIB_INJECTION_FORCE_FLAG = "DD_INJECT_FORCE";
static final String LIB_INJECTION_ENABLED_ENV_VAR = "DD_INJECTION_ENABLED";
static final String LIB_INJECTION_FORCE_SYS_PROP = "dd.inject.force";

private static final Class<?> thisClass = AgentBootstrap.class;
private static final int MAX_EXCEPTION_CHAIN_LENGTH = 99;
Expand Down Expand Up @@ -155,11 +156,13 @@ private static void agentmainImpl(

static boolean getConfig(String configName) {
switch (configName) {
case LIB_INJECTION_ENABLED_FLAG:
return System.getenv(LIB_INJECTION_ENABLED_FLAG) != null;
case LIB_INJECTION_FORCE_FLAG:
String libInjectionForceFlag = System.getenv(LIB_INJECTION_FORCE_FLAG);
return "true".equalsIgnoreCase(libInjectionForceFlag) || "1".equals(libInjectionForceFlag);
case LIB_INJECTION_ENABLED_ENV_VAR:
return System.getenv(LIB_INJECTION_ENABLED_ENV_VAR) != null;
case LIB_INJECTION_FORCE_SYS_PROP:
{
String injectionForceFlag = getPropertyOrEnvVar(LIB_INJECTION_FORCE_SYS_PROP);
return "true".equalsIgnoreCase(injectionForceFlag) || "1".equals(injectionForceFlag);
}
default:
return false;
}
Expand Down Expand Up @@ -285,8 +288,9 @@ static int parseJavaMajorVersion(String version) {

static boolean shouldAbortDueToOtherJavaAgents() {
// Simply considering having multiple agents
if (getConfig(LIB_INJECTION_ENABLED_FLAG)
&& !getConfig(LIB_INJECTION_FORCE_FLAG)

if (getConfig(LIB_INJECTION_ENABLED_ENV_VAR)
&& !getConfig(LIB_INJECTION_FORCE_SYS_PROP)
&& getAgentFilesFromVMArguments().size() > 1) {
// Formatting agent file list, Java 7 style
StringBuilder agentFiles = new StringBuilder();
Expand All @@ -305,7 +309,7 @@ && getAgentFilesFromVMArguments().size() > 1) {
"Info: multiple JVM agents detected, found "
+ agentFiles
+ ". Loading multiple APM/Tracing agent is not a recommended or supported configuration."
+ "Please set the DD_INJECT_FORCE configuration to TRUE to load Datadog APM/Tracing agent.");
+ "Please set the environment variable DD_INJECT_FORCE or the system property dd.inject.force to TRUE to load Datadog APM/Tracing agent.");
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ public static String getPropertyOrDefault(String property, String defaultValue)
return defaultValue;
}
}

private static String toEnvVar(String string) {
return string.replace('.', '_').replace('-', '_').toUpperCase();
}

public static String getPropertyOrEnvVar(String property) {
String envVarValue = System.getenv(toEnvVar(property));
if (envVarValue != null) {
return envVarValue;
}
return System.getProperty(property);
}
}
Loading