Skip to content

Commit 24db888

Browse files
Migrate Config system to environment component (#9191)
* feat(env): Migrate ConfigProvider to environment component * feat(env): Migrate agent bootstrap to environment component * feat(env): Improve null values handling
1 parent 76639fb commit 24db888

File tree

16 files changed

+120
-69
lines changed

16 files changed

+120
-69
lines changed

components/environment/src/main/java/datadog/environment/EnvironmentVariables.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package datadog.environment;
22

3-
import javax.annotation.Nonnull;
43
import javax.annotation.Nullable;
54

65
/**
@@ -32,7 +31,7 @@ private EnvironmentVariables() {}
3231
* @return The environment variable value, {@code defaultValue} if missing, can't be retrieved or
3332
* the environment variable name is {@code null}.
3433
*/
35-
public static String getOrDefault(@Nonnull String name, String defaultValue) {
34+
public static String getOrDefault(String name, String defaultValue) {
3635
if (name == null) {
3736
return defaultValue;
3837
}

components/environment/src/main/java/datadog/environment/SystemProperties.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package datadog.environment;
22

3-
import javax.annotation.Nonnull;
3+
import javax.annotation.Nullable;
44

55
/**
66
* Safely queries system properties against security manager.
@@ -18,7 +18,7 @@ private SystemProperties() {}
1818
* @return The system property value, {@code null} if missing, can't be retrieved, or the system
1919
* property name is {@code null}.
2020
*/
21-
public static String get(String property) {
21+
public static @Nullable String get(String property) {
2222
return getOrDefault(property, null);
2323
}
2424

@@ -31,7 +31,7 @@ public static String get(String property) {
3131
* @return The system property value, {@code defaultValue} if missing, can't be retrieved, or the
3232
* system property name is {@code null}.
3333
*/
34-
public static String getOrDefault(@Nonnull String property, String defaultValue) {
34+
public static String getOrDefault(String property, String defaultValue) {
3535
if (property == null) {
3636
return defaultValue;
3737
}
@@ -50,11 +50,32 @@ public static String getOrDefault(@Nonnull String property, String defaultValue)
5050
* @return {@code true} if the system property was successfully set, {@code} false otherwise.
5151
*/
5252
public static boolean set(String property, String value) {
53+
if (property == null || value == null) {
54+
return false;
55+
}
5356
try {
5457
System.setProperty(property, value);
5558
return true;
5659
} catch (SecurityException ignored) {
5760
return false;
5861
}
5962
}
63+
64+
/**
65+
* Clears a system property.
66+
*
67+
* @param property The system property name to clear.
68+
* @return The previous value of the system property, {@code null} if there was no prior property
69+
* and property can't be cleared.
70+
*/
71+
public static @Nullable String clear(String property) {
72+
if (property == null) {
73+
return null;
74+
}
75+
try {
76+
return System.clearProperty(property);
77+
} catch (SecurityException ignored) {
78+
return null;
79+
}
80+
}
6081
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@ParametersAreNonnullByDefault
2+
package datadog.environment;
3+
4+
import javax.annotation.ParametersAreNonnullByDefault;

components/environment/src/test/java/datadog/environment/SystemPropertiesTest.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,29 @@ void testGetOrDefault() {
3838

3939
@Test
4040
void testSet() {
41-
String testProperty = "test.property";
42-
String testValue = "test-value";
43-
assumeTrue(SystemProperties.get(testProperty) == null);
44-
41+
String testProperty = "test.set.property";
42+
String testValue = "test.set.value";
43+
assertNull(SystemProperties.get(testProperty));
4544
assertTrue(SystemProperties.set(testProperty, testValue));
4645
assertEquals(testValue, SystemProperties.get(testProperty));
46+
// Null values
47+
assertDoesNotThrow(() -> SystemProperties.set(testProperty, null));
48+
assertFalse(SystemProperties.set(testProperty, null));
49+
assertDoesNotThrow(() -> SystemProperties.set(null, testValue));
50+
assertFalse(SystemProperties.set(null, testValue));
51+
}
52+
53+
@Test
54+
void testClear() {
55+
String testProperty = "test.clear.property";
56+
String testValue = "test.clear.value";
57+
assertNull(SystemProperties.get(testProperty));
58+
assertNull(SystemProperties.clear(testProperty));
59+
assumeTrue(SystemProperties.set(testProperty, testValue));
60+
assertEquals(testValue, SystemProperties.clear(testProperty));
61+
assertNull(SystemProperties.clear(testProperty));
62+
// Null values
63+
assertDoesNotThrow(() -> SystemProperties.clear(null));
64+
assertNull(SystemProperties.clear(null));
4765
}
4866
}

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/benchmark/StaticEventLogger.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datadog.trace.bootstrap.benchmark;
22

3+
import datadog.environment.SystemProperties;
34
import java.io.*;
45
import java.nio.charset.StandardCharsets;
56
import java.util.Objects;
@@ -18,8 +19,8 @@ public class StaticEventLogger {
1819
static {
1920
BufferedWriter writer = null;
2021

21-
if ("true".equalsIgnoreCase(System.getProperty("dd.benchmark.enabled"))) {
22-
String dir = System.getProperty("dd.benchmark.output.dir");
22+
if ("true".equalsIgnoreCase(SystemProperties.get("dd.benchmark.enabled"))) {
23+
String dir = SystemProperties.get("dd.benchmark.output.dir");
2324
dir = (dir != null ? dir + File.separator : "");
2425
String fileName = dir + "startup_" + System.currentTimeMillis() + ".csv";
2526

dd-java-agent/agent-builder/src/main/java/datadog/trace/agent/tooling/AgentInstaller.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static datadog.trace.agent.tooling.bytebuddy.matcher.GlobalIgnoresMatcher.globalIgnoresMatcher;
66
import static net.bytebuddy.matcher.ElementMatchers.isDefaultFinalizer;
77

8+
import datadog.environment.SystemProperties;
89
import datadog.trace.agent.tooling.bytebuddy.SharedTypePools;
910
import datadog.trace.agent.tooling.bytebuddy.iast.TaintableRedefinitionStrategyListener;
1011
import datadog.trace.agent.tooling.bytebuddy.matcher.DDElementMatchers;
@@ -318,18 +319,17 @@ public static Set<InstrumenterModule.TargetSystem> getEnabledSystems() {
318319
}
319320

320321
private static void addByteBuddyRawSetting() {
321-
final String savedPropertyValue = System.getProperty(TypeDefinition.RAW_TYPES_PROPERTY);
322-
try {
323-
System.setProperty(TypeDefinition.RAW_TYPES_PROPERTY, "true");
324-
final boolean rawTypes = TypeDescription.AbstractBase.RAW_TYPES;
325-
if (!rawTypes && DEBUG) {
326-
log.debug("Too late to enable {}", TypeDefinition.RAW_TYPES_PROPERTY);
327-
}
328-
} finally {
322+
final String savedPropertyValue = SystemProperties.get(TypeDefinition.RAW_TYPES_PROPERTY);
323+
final boolean overridden = SystemProperties.set(TypeDefinition.RAW_TYPES_PROPERTY, "true");
324+
final boolean rawTypes = TypeDescription.AbstractBase.RAW_TYPES;
325+
if (!rawTypes && DEBUG) {
326+
log.debug("Too late to enable {}", TypeDefinition.RAW_TYPES_PROPERTY);
327+
}
328+
if (overridden) {
329329
if (savedPropertyValue == null) {
330-
System.clearProperty(TypeDefinition.RAW_TYPES_PROPERTY);
330+
SystemProperties.clear(TypeDefinition.RAW_TYPES_PROPERTY);
331331
} else {
332-
System.setProperty(TypeDefinition.RAW_TYPES_PROPERTY, savedPropertyValue);
332+
SystemProperties.set(TypeDefinition.RAW_TYPES_PROPERTY, savedPropertyValue);
333333
}
334334
}
335335
}

dd-java-agent/src/test/groovy/datadog/trace/agent/InitializationTelemetryTest.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ class InitializationTelemetryTest extends Specification {
5858
})
5959
def "incomplete agent start-up"() {
6060
// In this case, the SecurityManager blocks a custom permission that is checked by bytebuddy causing
61-
// agent initialization to fail. However, we should catch the exception allowing the application
61+
// agent initialization to fail. However, we should catch the exception allowing the application
6262
// to run normally.
6363
when:
6464
def result = InitializationTelemetryCheck.runTestJvm(InitializationTelemetryCheck.BlockByteBuddy)
6565

66-
then:
66+
then: 'should complete successfully and catch error'
6767
result.exitCode == 0
6868
!result.telemetryJson.contains('library_entrypoint.complete')
69-
result.telemetryJson.contains('error_type:java.lang.IllegalStateException')
69+
result.telemetryJson.contains('error_type:')
7070
}
7171

7272
@IgnoreIf(reason = "SecurityManager is permanently disabled as of JDK 24", value = {

internal-api/src/main/java/datadog/trace/api/Config.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,10 @@
632632
import static datadog.trace.util.CollectionUtils.tryMakeImmutableSet;
633633
import static datadog.trace.util.Strings.propertyNameToEnvironmentVariableName;
634634

635+
import datadog.environment.EnvironmentVariables;
635636
import datadog.environment.JavaVirtualMachine;
636637
import datadog.environment.OperatingSystem;
638+
import datadog.environment.SystemProperties;
637639
import datadog.trace.api.civisibility.CiVisibilityWellKnownTags;
638640
import datadog.trace.api.config.GeneralConfig;
639641
import datadog.trace.api.config.ProfilingConfig;
@@ -1239,7 +1241,7 @@ private Config(final ConfigProvider configProvider, final InstrumenterConfig ins
12391241
configFileStatus = configProvider.getConfigFileStatus();
12401242
runtimeIdEnabled =
12411243
configProvider.getBoolean(RUNTIME_ID_ENABLED, true, RUNTIME_METRICS_RUNTIME_ID_ENABLED);
1242-
runtimeVersion = System.getProperty("java.version", "unknown");
1244+
runtimeVersion = SystemProperties.getOrDefault("java.version", "unknown");
12431245

12441246
// Note: We do not want APiKey to be loaded from property for security reasons
12451247
// Note: we do not use defined default here
@@ -3409,7 +3411,7 @@ public static boolean isDatadogProfilerSafeInCurrentEnvironment() {
34093411
// don't want to put this logic (which will evolve) in the public ProfilingConfig, and can't
34103412
// access Platform there
34113413
if (!JavaVirtualMachine.isJ9() && isJavaVersion(8)) {
3412-
String arch = System.getProperty("os.arch");
3414+
String arch = SystemProperties.get("os.arch");
34133415
if ("aarch64".equalsIgnoreCase(arch) || "arm64".equalsIgnoreCase(arch)) {
34143416
return false;
34153417
}
@@ -4453,12 +4455,12 @@ public CiVisibilityWellKnownTags getCiVisibilityWellKnownTags() {
44534455
getRuntimeId(),
44544456
getEnv(),
44554457
LANGUAGE_TAG_VALUE,
4456-
System.getProperty("java.runtime.name"),
4457-
System.getProperty("java.version"),
4458-
System.getProperty("java.vendor"),
4459-
System.getProperty("os.arch"),
4460-
System.getProperty("os.name"),
4461-
System.getProperty("os.version"),
4458+
SystemProperties.get("java.runtime.name"),
4459+
SystemProperties.get("java.version"),
4460+
SystemProperties.get("java.vendor"),
4461+
SystemProperties.get("os.arch"),
4462+
SystemProperties.get("os.name"),
4463+
SystemProperties.get("os.version"),
44624464
isServiceNameSetByUser() ? "true" : "false");
44634465
}
44644466

@@ -5197,7 +5199,7 @@ private static boolean isWindowsOS() {
51975199
}
51985200

51995201
private static String getEnv(String name) {
5200-
String value = System.getenv(name);
5202+
String value = EnvironmentVariables.get(name);
52015203
if (value != null) {
52025204
ConfigCollector.get().put(name, value, ConfigOrigin.ENV);
52035205
}
@@ -5220,7 +5222,7 @@ private static String getProp(String name) {
52205222
}
52215223

52225224
private static String getProp(String name, String def) {
5223-
String value = System.getProperty(name, def);
5225+
String value = SystemProperties.getOrDefault(name, def);
52245226
if (value != null) {
52255227
ConfigCollector.get().put(name, value, ConfigOrigin.JVM_PROP);
52265228
}

internal-api/src/main/java/datadog/trace/api/Platform.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import static datadog.environment.JavaVirtualMachine.isJavaVersionAtLeast;
66
import static datadog.environment.JavaVirtualMachine.isOracleJDK8;
77

8+
import datadog.environment.SystemProperties;
9+
810
/**
911
* This class is used early on during premain; it must not touch features like JMX or JUL in case
1012
* they trigger early loading/binding.
@@ -51,7 +53,7 @@ private static boolean checkForJfr() {
5153

5254
private static boolean checkForNativeImageBuilder() {
5355
try {
54-
return "org.graalvm.nativeimage.builder".equals(System.getProperty("jdk.module.main"));
56+
return "org.graalvm.nativeimage.builder".equals(SystemProperties.get("jdk.module.main"));
5557
} catch (Throwable e) {
5658
return false;
5759
}

internal-api/src/main/java/datadog/trace/api/env/CapturedEnvironment.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datadog.trace.api.env;
22

3+
import datadog.environment.EnvironmentVariables;
34
import datadog.environment.JavaVirtualMachine;
45
import datadog.trace.api.config.GeneralConfig;
56
import java.io.File;
@@ -77,8 +78,8 @@ static void useFixedProcessInfo(final ProcessInfo processInfo) {
7778
* autodetection will return either the JAR filename or the java main class.
7879
*/
7980
private String autodetectServiceName() {
80-
String inAas = System.getenv("DD_AZURE_APP_SERVICES");
81-
String siteName = System.getenv("WEBSITE_SITE_NAME");
81+
String inAas = EnvironmentVariables.get("DD_AZURE_APP_SERVICES");
82+
String siteName = EnvironmentVariables.get("WEBSITE_SITE_NAME");
8283

8384
if (("true".equalsIgnoreCase(inAas) || "1".equals(inAas)) && siteName != null) {
8485
return siteName;

0 commit comments

Comments
 (0)