Skip to content

Commit 0604f66

Browse files
cecile75Cecile Terpin
and
Cecile Terpin
authored
Add system property to force injection of the tracing library even though multiple javaagents have been detected (#8697)
* add system property for inject force --------- Co-authored-by: Cecile Terpin <“[email protected]”>
1 parent 0382750 commit 0604f66

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

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

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datadog.trace.bootstrap;
22

3+
import static datadog.trace.bootstrap.SystemUtils.getPropertyOrEnvVar;
34
import static java.nio.charset.StandardCharsets.UTF_8;
45

56
import datadog.cli.CLIHelper;
@@ -44,8 +45,8 @@
4445
* </ul>
4546
*/
4647
public final class AgentBootstrap {
47-
static final String LIB_INJECTION_ENABLED_FLAG = "DD_INJECTION_ENABLED";
48-
static final String LIB_INJECTION_FORCE_FLAG = "DD_INJECT_FORCE";
48+
static final String LIB_INJECTION_ENABLED_ENV_VAR = "DD_INJECTION_ENABLED";
49+
static final String LIB_INJECTION_FORCE_SYS_PROP = "dd.inject.force";
4950

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

156157
static boolean getConfig(String configName) {
157158
switch (configName) {
158-
case LIB_INJECTION_ENABLED_FLAG:
159-
return System.getenv(LIB_INJECTION_ENABLED_FLAG) != null;
160-
case LIB_INJECTION_FORCE_FLAG:
161-
String libInjectionForceFlag = System.getenv(LIB_INJECTION_FORCE_FLAG);
162-
return "true".equalsIgnoreCase(libInjectionForceFlag) || "1".equals(libInjectionForceFlag);
159+
case LIB_INJECTION_ENABLED_ENV_VAR:
160+
return System.getenv(LIB_INJECTION_ENABLED_ENV_VAR) != null;
161+
case LIB_INJECTION_FORCE_SYS_PROP:
162+
{
163+
String injectionForceFlag = getPropertyOrEnvVar(LIB_INJECTION_FORCE_SYS_PROP);
164+
return "true".equalsIgnoreCase(injectionForceFlag) || "1".equals(injectionForceFlag);
165+
}
163166
default:
164167
return false;
165168
}
@@ -285,8 +288,9 @@ static int parseJavaMajorVersion(String version) {
285288

286289
static boolean shouldAbortDueToOtherJavaAgents() {
287290
// Simply considering having multiple agents
288-
if (getConfig(LIB_INJECTION_ENABLED_FLAG)
289-
&& !getConfig(LIB_INJECTION_FORCE_FLAG)
291+
292+
if (getConfig(LIB_INJECTION_ENABLED_ENV_VAR)
293+
&& !getConfig(LIB_INJECTION_FORCE_SYS_PROP)
290294
&& getAgentFilesFromVMArguments().size() > 1) {
291295
// Formatting agent file list, Java 7 style
292296
StringBuilder agentFiles = new StringBuilder();
@@ -305,7 +309,7 @@ && getAgentFilesFromVMArguments().size() > 1) {
305309
"Info: multiple JVM agents detected, found "
306310
+ agentFiles
307311
+ ". Loading multiple APM/Tracing agent is not a recommended or supported configuration."
308-
+ "Please set the DD_INJECT_FORCE configuration to TRUE to load Datadog APM/Tracing agent.");
312+
+ "Please set the environment variable DD_INJECT_FORCE or the system property dd.inject.force to TRUE to load Datadog APM/Tracing agent.");
309313
return true;
310314
}
311315
return false;

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

+12
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,16 @@ public static String getPropertyOrDefault(String property, String defaultValue)
3030
return defaultValue;
3131
}
3232
}
33+
34+
private static String toEnvVar(String string) {
35+
return string.replace('.', '_').replace('-', '_').toUpperCase();
36+
}
37+
38+
public static String getPropertyOrEnvVar(String property) {
39+
String envVarValue = System.getenv(toEnvVar(property));
40+
if (envVarValue != null) {
41+
return envVarValue;
42+
}
43+
return System.getProperty(property);
44+
}
3345
}

0 commit comments

Comments
 (0)