Skip to content

Commit f17f23d

Browse files
committed
feat(environment): Add new API to EnvironmentVariables and SystemProperties
1 parent 6be4a1d commit f17f23d

File tree

8 files changed

+92
-41
lines changed

8 files changed

+92
-41
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package datadog.environment;
22

3+
import static java.util.Collections.emptyMap;
4+
import static java.util.Collections.unmodifiableMap;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
38
import javax.annotation.Nullable;
49

510
/**
@@ -42,4 +47,18 @@ public static String getOrDefault(String name, String defaultValue) {
4247
return defaultValue;
4348
}
4449
}
50+
51+
/**
52+
* Gets all environment variables.
53+
*
54+
* @return All environment variables captured in an unmodifiable {@link Map}, or an empty {@link
55+
* Map} if they can't be retrieved.
56+
*/
57+
public static Map<String, String> getAll() {
58+
try {
59+
return unmodifiableMap(new HashMap<>(System.getenv()));
60+
} catch (SecurityException e) {
61+
return emptyMap();
62+
}
63+
}
4564
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package datadog.environment;
22

3+
import static java.util.Collections.emptyMap;
4+
import static java.util.Collections.unmodifiableMap;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
38
import javax.annotation.Nullable;
49

510
/**
@@ -42,6 +47,24 @@ public static String getOrDefault(String property, String defaultValue) {
4247
}
4348
}
4449

50+
/**
51+
* Convert system properties to an unmodifiable {@link Map}.
52+
*
53+
* @return All system properties captured in an unmodifiable {@link Map}, or an empty {@link Map}
54+
* if they can't be retrieved.
55+
*/
56+
public static Map<String, String> asStringMap() {
57+
try {
58+
Map<String, String> map = new HashMap<>();
59+
for (String propertyName : System.getProperties().stringPropertyNames()) {
60+
map.put(propertyName, System.getProperty(propertyName));
61+
}
62+
return unmodifiableMap(map);
63+
} catch (SecurityException ignored) {
64+
return emptyMap();
65+
}
66+
}
67+
4568
/**
4669
* Sets a system property value.
4770
*

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
44
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
56
import static org.junit.jupiter.api.Assertions.assertNotNull;
67
import static org.junit.jupiter.api.Assertions.assertNull;
8+
import static org.junit.jupiter.api.Assertions.assertThrows;
79

10+
import java.util.Map;
811
import org.junit.jupiter.api.Test;
912

1013
class EnvironmentVariablesTest {
@@ -36,4 +39,13 @@ void testGetOrDefault() {
3639
assertDoesNotThrow(() -> EnvironmentVariables.getOrDefault(MISSING_ENV_VAR, null));
3740
assertNull(EnvironmentVariables.getOrDefault(MISSING_ENV_VAR, null));
3841
}
42+
43+
@Test
44+
void testGetAll() {
45+
Map<String, String> all = EnvironmentVariables.getAll();
46+
assertNotNull(all);
47+
assertFalse(all.isEmpty());
48+
// Unmodifiable collection
49+
assertThrows(UnsupportedOperationException.class, all::clear);
50+
}
3951
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.jupiter.api.Assertions.*;
44
import static org.junit.jupiter.api.Assumptions.assumeTrue;
55

6+
import java.util.Map;
67
import org.junit.jupiter.api.Test;
78

89
class SystemPropertiesTest {
@@ -63,4 +64,15 @@ void testClear() {
6364
assertDoesNotThrow(() -> SystemProperties.clear(null));
6465
assertNull(SystemProperties.clear(null));
6566
}
67+
68+
@Test
69+
void testAsStringMap() {
70+
Map<String, String> stringMap = SystemProperties.asStringMap();
71+
assertNotNull(stringMap);
72+
assertFalse(stringMap.isEmpty());
73+
// Unmodifiable collection
74+
assertThrows(
75+
UnsupportedOperationException.class,
76+
() -> stringMap.put(MISSING_SYSTEM_PROPERTY, DEFAULT_VALUE));
77+
}
6678
}

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/ProcessHierarchy.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
import datadog.trace.bootstrap.instrumentation.api.AgentPropagation;
1010
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext;
1111
import java.net.InetSocketAddress;
12-
import java.util.Properties;
12+
import java.util.Map;
1313
import javax.annotation.Nullable;
1414

1515
public class ProcessHierarchy {
1616

1717
private static final class SystemPropertiesPropagationGetter
18-
implements AgentPropagation.ContextVisitor<Properties> {
19-
static final AgentPropagation.ContextVisitor<Properties> INSTANCE =
18+
implements AgentPropagation.ContextVisitor<Map<String, String>> {
19+
static final AgentPropagation.ContextVisitor<Map<String, String>> INSTANCE =
2020
new SystemPropertiesPropagationGetter();
2121

2222
private SystemPropertiesPropagationGetter() {}
2323

2424
@Override
25-
public void forEachKey(Properties carrier, AgentPropagation.KeyClassifier classifier) {
26-
for (String propertyName : carrier.stringPropertyNames()) {
27-
if (!classifier.accept(propertyName, carrier.getProperty(propertyName))) {
25+
public void forEachKey(Map<String, String> carrier, AgentPropagation.KeyClassifier classifier) {
26+
for (Map.Entry<String, String> property : carrier.entrySet()) {
27+
if (!classifier.accept(property.getKey(), property.getValue())) {
2828
return;
2929
}
3030
}
@@ -36,7 +36,7 @@ public void forEachKey(Properties carrier, AgentPropagation.KeyClassifier classi
3636
ProcessHierarchy() {
3737
parentProcessModuleContext =
3838
extractContextAndGetSpanContext(
39-
System.getProperties(), SystemPropertiesPropagationGetter.INSTANCE);
39+
SystemProperties.asStringMap(), SystemPropertiesPropagationGetter.INSTANCE);
4040
}
4141

4242
/**

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/ci/env/CiEnvironmentImpl.java

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

3-
import java.util.Collections;
3+
import datadog.environment.EnvironmentVariables;
44
import java.util.Map;
55

66
public class CiEnvironmentImpl implements CiEnvironment {
@@ -12,13 +12,7 @@ public CiEnvironmentImpl(Map<String, String> env) {
1212
}
1313

1414
public static CiEnvironment local() {
15-
Map<String, String> env;
16-
try {
17-
env = System.getenv();
18-
} catch (SecurityException e) {
19-
env = Collections.emptyMap();
20-
}
21-
return new CiEnvironmentImpl(env);
15+
return new CiEnvironmentImpl(EnvironmentVariables.getAll());
2216
}
2317

2418
@Override

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/domain/buildsystem/BuildSystemModuleImpl.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import datadog.communication.ddagent.TracerVersion;
66
import datadog.context.propagation.CarrierSetter;
7+
import datadog.environment.SystemProperties;
78
import datadog.trace.api.Config;
89
import datadog.trace.api.DDTags;
910
import datadog.trace.api.civisibility.domain.BuildModuleLayout;
@@ -37,7 +38,6 @@
3738
import java.util.HashMap;
3839
import java.util.List;
3940
import java.util.Map;
40-
import java.util.Properties;
4141
import java.util.concurrent.atomic.LongAdder;
4242
import java.util.function.Consumer;
4343
import javax.annotation.Nullable;
@@ -137,18 +137,14 @@ private Map<String, String> getPropertiesPropagatedToChildProcess(
137137
ExecutionSettings executionSettings,
138138
BuildSessionSettings sessionSettings) {
139139
Map<String, String> propagatedSystemProperties = new HashMap<>();
140-
try {
141-
Properties systemProperties = System.getProperties();
142-
for (Map.Entry<Object, Object> e : systemProperties.entrySet()) {
143-
String propertyName = (String) e.getKey();
144-
Object propertyValue = e.getValue();
145-
if ((propertyName.startsWith(Config.PREFIX)
146-
|| propertyName.startsWith("datadog.slf4j.simpleLogger.defaultLogLevel"))
147-
&& propertyValue != null) {
148-
propagatedSystemProperties.put(propertyName, propertyValue.toString());
149-
}
140+
for (Map.Entry<String, String> p : SystemProperties.asStringMap().entrySet()) {
141+
String propertyName = p.getKey();
142+
String propertyValue = p.getValue();
143+
if ((propertyName.startsWith(Config.PREFIX)
144+
|| propertyName.startsWith("datadog.slf4j.simpleLogger.defaultLogLevel"))
145+
&& propertyValue != null) {
146+
propagatedSystemProperties.put(propertyName, propertyValue);
150147
}
151-
} catch (SecurityException ignored) {
152148
}
153149

154150
propagatedSystemProperties.put(

dd-java-agent/instrumentation/gradle-8.3/src/main/groovy/datadog/trace/instrumentation/gradle/GradleDaemonInjectionUtils.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.nio.file.Path;
1010
import java.util.HashMap;
1111
import java.util.Map;
12-
import java.util.Properties;
1312

1413
public class GradleDaemonInjectionUtils {
1514

@@ -28,20 +27,16 @@ public static Map<String, String> addJavaagentToGradleDaemonProperties(
2827
Path agentJarPath = agentJar.toPath();
2928
StringBuilder agentArg = new StringBuilder("-javaagent:").append(agentJarPath).append('=');
3029

31-
try {
32-
Properties systemProperties = System.getProperties();
33-
for (Map.Entry<Object, Object> e : systemProperties.entrySet()) {
34-
String propertyName = (String) e.getKey();
35-
Object propertyValue = e.getValue();
36-
if (propertyName.startsWith(Config.PREFIX)) {
37-
agentArg
38-
.append(propertyName)
39-
.append("='")
40-
.append(String.valueOf(propertyValue).replace("'", "'\\''"))
41-
.append("',");
42-
}
30+
for (Map.Entry<String, String> p : SystemProperties.asStringMap().entrySet()) {
31+
String propertyName = p.getKey();
32+
String propertyValue = p.getValue();
33+
if (propertyName.startsWith(Config.PREFIX)) {
34+
agentArg
35+
.append(propertyName)
36+
.append("='")
37+
.append(propertyValue.replace("'", "'\\''"))
38+
.append("',");
4339
}
44-
} catch (SecurityException ignored) {
4540
}
4641

4742
// creating a new map in case jvmOptions is immutable

0 commit comments

Comments
 (0)