Skip to content

Commit 60d0cb5

Browse files
committed
fix(environment): Revert agent bootstrap migration
1 parent 8e89900 commit 60d0cb5

File tree

8 files changed

+71
-13
lines changed

8 files changed

+71
-13
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public final class Constants {
1515
*/
1616
public static final String[] BOOTSTRAP_PACKAGE_PREFIXES = {
1717
"datadog.slf4j",
18+
"datadog.cli",
1819
"datadog.context",
1920
"datadog.environment",
2021
"datadog.json",

dd-java-agent/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ tasks.withType(GenerateMavenPom).configureEach { task ->
240240

241241
dependencies {
242242
implementation project(path: ':components:json')
243+
implementation project(path: ':components:cli')
243244
implementation project(path: ':components:environment')
244245
modules {
245246
module("com.squareup.okio:okio") {

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package datadog.trace.bootstrap;
22

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

5-
import datadog.environment.EnvironmentVariables;
6-
import datadog.environment.JavaVirtualMachine;
6+
import datadog.cli.CLIHelper;
77
import datadog.environment.SystemProperties;
88
import de.thetaphi.forbiddenapis.SuppressForbidden;
99
import java.io.BufferedReader;
@@ -64,6 +64,10 @@ public static void premain(final String agentArgs, final Instrumentation inst) {
6464
public static void agentmain(final String agentArgs, final Instrumentation inst) {
6565
BootstrapInitializationTelemetry initTelemetry;
6666

67+
// TODO Example calls that break Gradle smoke tests
68+
String javaVersion = SystemProperties.get("java.version");
69+
// String forwarderPath = EnvironmentVariables.get("DD_TELEMETRY_FORWARDER_PATH");
70+
6771
try {
6872
initTelemetry = createInitializationTelemetry();
6973
} catch (Throwable t) {
@@ -91,7 +95,7 @@ public static void agentmain(final String agentArgs, final Instrumentation inst)
9195
}
9296

9397
private static BootstrapInitializationTelemetry createInitializationTelemetry() {
94-
String forwarderPath = EnvironmentVariables.get("DD_TELEMETRY_FORWARDER_PATH");
98+
String forwarderPath = SystemUtils.tryGetEnv("DD_TELEMETRY_FORWARDER_PATH");
9599
if (forwarderPath == null) {
96100
return BootstrapInitializationTelemetry.noOpInstance();
97101
}
@@ -101,7 +105,7 @@ private static BootstrapInitializationTelemetry createInitializationTelemetry()
101105
initTelemetry.initMetaInfo("runtime_name", "jvm");
102106
initTelemetry.initMetaInfo("language_name", "jvm");
103107

104-
String javaVersion = SystemProperties.get("java.version");
108+
String javaVersion = SystemUtils.tryGetProperty("java.version");
105109
if (javaVersion != null) {
106110
initTelemetry.initMetaInfo("runtime_version", javaVersion);
107111
initTelemetry.initMetaInfo("language_version", javaVersion);
@@ -164,12 +168,7 @@ static boolean getConfig(String configName) {
164168
return System.getenv(LIB_INJECTION_ENABLED_ENV_VAR) != null;
165169
case LIB_INJECTION_FORCE_SYS_PROP:
166170
{
167-
String envVarName =
168-
LIB_INJECTION_FORCE_SYS_PROP.replace('.', '_').replace('-', '_').toUpperCase();
169-
String injectionForceFlag = EnvironmentVariables.get(envVarName);
170-
if (injectionForceFlag == null) {
171-
injectionForceFlag = SystemProperties.get(LIB_INJECTION_FORCE_SYS_PROP);
172-
}
171+
String injectionForceFlag = getPropertyOrEnvVar(LIB_INJECTION_FORCE_SYS_PROP);
173172
return "true".equalsIgnoreCase(injectionForceFlag) || "1".equals(injectionForceFlag);
174173
}
175174
default:
@@ -178,7 +177,7 @@ static boolean getConfig(String configName) {
178177
}
179178

180179
private static void recordInstrumentationSource(String source) {
181-
SystemProperties.set(LIB_INSTRUMENTATION_SOURCE_SYS_PROP, source);
180+
SystemUtils.trySetProperty(LIB_INSTRUMENTATION_SOURCE_SYS_PROP, source);
182181
}
183182

184183
static boolean exceptionCauseChainContains(Throwable ex, String exClassName) {
@@ -206,7 +205,7 @@ private static boolean alreadyInitialized() {
206205
}
207206

208207
private static boolean isJdkTool() {
209-
String moduleMain = SystemProperties.get("jdk.module.main");
208+
String moduleMain = SystemUtils.tryGetProperty("jdk.module.main");
210209
if (null != moduleMain && !moduleMain.isEmpty() && moduleMain.charAt(0) == 'j') {
211210
switch (moduleMain) {
212211
case "java.base": // keytool
@@ -358,7 +357,7 @@ private static List<File> getAgentFilesFromVMArguments() {
358357
// - On IBM-based JDKs since at least 1.7
359358
// This prevents custom log managers from working correctly
360359
// Use reflection to bypass the loading of the class~
361-
for (final String argument : JavaVirtualMachine.getVmOptions()) {
360+
for (final String argument : CLIHelper.getVmArgs()) {
362361
if (argument.startsWith(JAVA_AGENT_ARGUMENT)) {
363362
int index = argument.indexOf('=', JAVA_AGENT_ARGUMENT.length());
364363
String agentPathname =
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package datadog.trace.bootstrap;
2+
3+
public final class SystemUtils {
4+
private SystemUtils() {}
5+
6+
public static String tryGetEnv(String envVar) {
7+
return getEnvOrDefault(envVar, null);
8+
}
9+
10+
public static String getEnvOrDefault(String envVar, String defaultValue) {
11+
try {
12+
return System.getenv(envVar);
13+
} catch (SecurityException e) {
14+
return defaultValue;
15+
}
16+
}
17+
18+
public static String tryGetProperty(String property) {
19+
try {
20+
return System.getProperty(property);
21+
} catch (SecurityException e) {
22+
return null;
23+
}
24+
}
25+
26+
public static String trySetProperty(String property, String value) {
27+
try {
28+
return System.setProperty(property, value);
29+
} catch (SecurityException e) {
30+
return null;
31+
}
32+
}
33+
34+
public static String getPropertyOrDefault(String property, String defaultValue) {
35+
try {
36+
return System.getProperty(property, defaultValue);
37+
} catch (SecurityException e) {
38+
return defaultValue;
39+
}
40+
}
41+
42+
private static String toEnvVar(String string) {
43+
return string.replace('.', '_').replace('-', '_').toUpperCase();
44+
}
45+
46+
public static String getPropertyOrEnvVar(String property) {
47+
String envVarValue = System.getenv(toEnvVar(property));
48+
if (envVarValue != null) {
49+
return envVarValue;
50+
}
51+
return System.getProperty(property);
52+
}
53+
}

dd-java-agent/testing/src/main/groovy/datadog/trace/agent/test/SpockRunner.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class SpockRunner extends JUnitPlatform {
3838
*/
3939
public static final String[] BOOTSTRAP_PACKAGE_PREFIXES_COPY = {
4040
"datadog.slf4j",
41+
"datadog.cli",
4142
"datadog.context",
4243
"datadog.environment",
4344
"datadog.json",

gradle/dependencies.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ final class CachedData {
2020
exclude(project(':components:environment'))
2121
exclude(project(':components:json'))
2222
exclude(project(':components:yaml'))
23+
exclude(project(':components:cli'))
2324
exclude(project(':remote-config:remote-config-api'))
2425
exclude(project(':remote-config:remote-config-core'))
2526
exclude(project(':telemetry'))

internal-api/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ dependencies {
243243
// references TraceScope and Continuation from public api
244244
api project(':dd-trace-api')
245245
api libs.slf4j
246+
api project(':components:cli')
246247
api project(':components:context')
247248
api project(':components:environment')
248249
api project(':components:yaml')

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ include ':dd-java-agent:agent-otel:otel-shim'
8686
include ':dd-java-agent:agent-otel:otel-tooling'
8787

8888
include ':communication'
89+
include ':components:cli'
8990
include ':components:context'
9091
include ':components:environment'
9192
include ':components:json'

0 commit comments

Comments
 (0)