Skip to content

Commit 8155c58

Browse files
committed
feat(env): Migrate ConfigProvider to environment component
1 parent 9b8db74 commit 8155c58

File tree

9 files changed

+54
-46
lines changed

9 files changed

+54
-46
lines changed

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

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

633+
import datadog.environment.EnvironmentVariables;
633634
import datadog.environment.JavaVirtualMachine;
634635
import datadog.environment.OperatingSystem;
636+
import datadog.environment.SystemProperties;
635637
import datadog.trace.api.civisibility.CiVisibilityWellKnownTags;
636638
import datadog.trace.api.config.GeneralConfig;
637639
import datadog.trace.api.config.ProfilingConfig;
@@ -1236,7 +1238,7 @@ private Config(final ConfigProvider configProvider, final InstrumenterConfig ins
12361238
configFileStatus = configProvider.getConfigFileStatus();
12371239
runtimeIdEnabled =
12381240
configProvider.getBoolean(RUNTIME_ID_ENABLED, true, RUNTIME_METRICS_RUNTIME_ID_ENABLED);
1239-
runtimeVersion = System.getProperty("java.version", "unknown");
1241+
runtimeVersion = SystemProperties.getOrDefault("java.version", "unknown");
12401242

12411243
// Note: We do not want APiKey to be loaded from property for security reasons
12421244
// Note: we do not use defined default here
@@ -3397,7 +3399,7 @@ public static boolean isDatadogProfilerSafeInCurrentEnvironment() {
33973399
// don't want to put this logic (which will evolve) in the public ProfilingConfig, and can't
33983400
// access Platform there
33993401
if (!JavaVirtualMachine.isJ9() && isJavaVersion(8)) {
3400-
String arch = System.getProperty("os.arch");
3402+
String arch = SystemProperties.get("os.arch");
34013403
if ("aarch64".equalsIgnoreCase(arch) || "arm64".equalsIgnoreCase(arch)) {
34023404
return false;
34033405
}
@@ -4441,12 +4443,12 @@ public CiVisibilityWellKnownTags getCiVisibilityWellKnownTags() {
44414443
getRuntimeId(),
44424444
getEnv(),
44434445
LANGUAGE_TAG_VALUE,
4444-
System.getProperty("java.runtime.name"),
4445-
System.getProperty("java.version"),
4446-
System.getProperty("java.vendor"),
4447-
System.getProperty("os.arch"),
4448-
System.getProperty("os.name"),
4449-
System.getProperty("os.version"),
4446+
SystemProperties.get("java.runtime.name"),
4447+
SystemProperties.get("java.version"),
4448+
SystemProperties.get("java.vendor"),
4449+
SystemProperties.get("os.arch"),
4450+
SystemProperties.get("os.name"),
4451+
SystemProperties.get("os.version"),
44504452
isServiceNameSetByUser() ? "true" : "false");
44514453
}
44524454

@@ -5185,7 +5187,7 @@ private static boolean isWindowsOS() {
51855187
}
51865188

51875189
private static String getEnv(String name) {
5188-
String value = System.getenv(name);
5190+
String value = EnvironmentVariables.get(name);
51895191
if (value != null) {
51905192
ConfigCollector.get().put(name, value, ConfigOrigin.ENV);
51915193
}
@@ -5208,7 +5210,7 @@ private static String getProp(String name) {
52085210
}
52095211

52105212
private static String getProp(String name, String def) {
5211-
String value = System.getProperty(name, def);
5213+
String value = SystemProperties.getOrDefault(name, def);
52125214
if (value != null) {
52135215
ConfigCollector.get().put(name, value, ConfigOrigin.JVM_PROP);
52145216
}

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;

internal-api/src/main/java/datadog/trace/bootstrap/config/provider/AgentArgsInjector.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package datadog.trace.bootstrap.config.provider;
22

3+
import datadog.environment.EnvironmentVariables;
4+
import datadog.environment.SystemProperties;
35
import datadog.trace.util.Strings;
46
import java.util.Map;
57

@@ -21,14 +23,14 @@ public static void injectAgentArgsConfig(Map<String, String> args) {
2123
}
2224
for (Map.Entry<String, String> e : args.entrySet()) {
2325
String propertyName = e.getKey();
24-
String existingPropertyValue = System.getProperty(propertyName);
26+
String existingPropertyValue = SystemProperties.get(propertyName);
2527
if (existingPropertyValue != null) {
2628
// system properties should have higher priority than agent arguments
2729
continue;
2830
}
2931

3032
String envVarName = Strings.toEnvVar(propertyName);
31-
String envVarValue = System.getenv(envVarName);
33+
String envVarValue = EnvironmentVariables.get(envVarName);
3234
if (envVarValue != null) {
3335
// env variables should have higher priority than agent arguments
3436
continue;

internal-api/src/main/java/datadog/trace/bootstrap/config/provider/ConfigProvider.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static datadog.trace.api.config.GeneralConfig.CONFIGURATION_FILE;
44

5+
import datadog.environment.SystemProperties;
56
import datadog.trace.api.ConfigCollector;
67
import datadog.trace.api.ConfigOrigin;
78
import de.thetaphi.forbiddenapis.SuppressForbidden;
@@ -471,8 +472,11 @@ private static Properties loadConfigurationFile(ConfigProvider configProvider) {
471472
}
472473

473474
// Normalizing tilde (~) paths for unix systems
474-
configurationFilePath =
475-
configurationFilePath.replaceFirst("^~", System.getProperty("user.home"));
475+
String home;
476+
if (configurationFilePath.charAt(0) == '~'
477+
&& (home = SystemProperties.get("user.home")) != null) {
478+
configurationFilePath = home + configurationFilePath.substring(1);
479+
}
476480

477481
// Configuration properties file is optional
478482
final File configurationFile = new File(configurationFilePath);
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
package datadog.trace.bootstrap.config.provider;
22

3+
import static datadog.trace.api.ConfigOrigin.ENV;
34
import static datadog.trace.util.Strings.propertyNameToEnvironmentVariableName;
45

6+
import datadog.environment.EnvironmentVariables;
57
import datadog.trace.api.ConfigOrigin;
68

79
final class EnvironmentConfigSource extends ConfigProvider.Source {
8-
910
@Override
1011
protected String get(String key) {
11-
try {
12-
return System.getenv(propertyNameToEnvironmentVariableName(key));
13-
} catch (SecurityException e) {
14-
return null;
15-
}
12+
return EnvironmentVariables.get(propertyNameToEnvironmentVariableName(key));
1613
}
1714

1815
@Override
1916
public ConfigOrigin origin() {
20-
return ConfigOrigin.ENV;
17+
return ENV;
2118
}
2219
}

internal-api/src/main/java/datadog/trace/bootstrap/config/provider/OtelEnvironmentConfigSource.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
import static datadog.trace.api.config.TracerConfig.RESPONSE_HEADER_TAGS;
1515
import static datadog.trace.api.config.TracerConfig.TRACE_PROPAGATION_STYLE;
1616
import static datadog.trace.api.config.TracerConfig.TRACE_SAMPLE_RATE;
17+
import static datadog.trace.util.Strings.toEnvVar;
1718

19+
import datadog.environment.EnvironmentVariables;
20+
import datadog.environment.SystemProperties;
1821
import datadog.trace.api.ConfigOrigin;
1922
import datadog.trace.api.TracePropagationStyle;
2023
import datadog.trace.api.telemetry.OtelEnvMetricCollector;
@@ -141,12 +144,8 @@ private String getOtelProperty(String otelSysProp, String ddSysProp) {
141144
}
142145
String ddValue = getDatadogProperty(ddSysProp);
143146
if (null != ddValue) {
144-
String otelEnvVar = Strings.toEnvVar(otelSysProp);
145-
log.warn(
146-
"Both {} and {} are set, ignoring {}",
147-
Strings.toEnvVar(ddSysProp),
148-
otelEnvVar,
149-
otelEnvVar);
147+
String otelEnvVar = toEnvVar(otelSysProp);
148+
log.warn("Both {} and {} are set, ignoring {}", toEnvVar(ddSysProp), otelEnvVar, otelEnvVar);
150149
otelEnvMetricCollector.setHidingOtelEnvVarMetric(
151150
Strings.toEnvVarLowerCase(otelSysProp), Strings.toEnvVarLowerCase(ddSysProp));
152151
return null;
@@ -186,9 +185,9 @@ private String getDatadogProperty(String sysProp) {
186185
* <p>Checks system properties and environment variables.
187186
*/
188187
private static String getProperty(String sysProp) {
189-
String value = System.getProperty(sysProp);
188+
String value = SystemProperties.get(sysProp);
190189
if (null == value) {
191-
value = System.getenv(Strings.toEnvVar(sysProp));
190+
value = EnvironmentVariables.get(toEnvVar(sysProp));
192191
}
193192
return value;
194193
}
@@ -204,8 +203,10 @@ private void capture(String key, String value) {
204203
private static Properties loadOtelConfigFile() {
205204
String path = getProperty("otel.javaagent.configuration-file");
206205
if (null != path && !path.isEmpty()) {
207-
if (path.charAt(0) == '~') {
208-
path = System.getProperty("user.home") + path.substring(1);
206+
// Inflate '~' prefix as home folder
207+
String home;
208+
if (path.charAt(0) == '~' && (home = SystemProperties.get("user.home")) != null) {
209+
path = home + path.substring(1);
209210
}
210211
File file = new File(path);
211212
if (file.exists()) {

internal-api/src/main/java/datadog/trace/bootstrap/config/provider/StableConfigParser.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package datadog.trace.bootstrap.config.provider;
22

3+
import datadog.environment.EnvironmentVariables;
4+
import datadog.environment.SystemProperties;
35
import datadog.trace.bootstrap.config.provider.stableconfig.Rule;
46
import datadog.trace.bootstrap.config.provider.stableconfig.Selector;
57
import datadog.trace.bootstrap.config.provider.stableconfig.StableConfig;
@@ -138,7 +140,7 @@ private static boolean checkEnvMatches(
138140
return false;
139141
}
140142

141-
// We do all of the case insensitivity modifications in this function, because each selector will
143+
// We do all the case insensitivity modifications in this function, because each selector will
142144
// be viewed just once
143145
static boolean selectorMatch(String origin, List<String> matches, String operator, String key) {
144146
switch (origin.toLowerCase()) {
@@ -156,7 +158,7 @@ static boolean selectorMatch(String origin, List<String> matches, String operato
156158
if (key == null) {
157159
return false;
158160
}
159-
String envValue = System.getenv(key.toUpperCase());
161+
String envValue = EnvironmentVariables.get(key.toUpperCase());
160162
if (envValue == null) {
161163
return false;
162164
}
@@ -187,7 +189,7 @@ static boolean selectorMatch(String origin, List<String> matches, String operato
187189
return false;
188190
}
189191
// Cut the -D prefix
190-
return System.getProperty(key.substring(2)) != null;
192+
return SystemProperties.get(key.substring(2)) != null;
191193
case "tags":
192194
// TODO: Support this down the line (Must define the source of "tags" first)
193195
return false;
@@ -249,7 +251,7 @@ private static String processTemplateVar(String templateVar) throws IOException
249251
if (envVar.isEmpty()) {
250252
throw new IOException("Empty environment variable name in template");
251253
}
252-
String value = System.getenv(envVar.toUpperCase());
254+
String value = EnvironmentVariables.get(envVar.toUpperCase());
253255
if (value == null || value.isEmpty()) {
254256
return UNDEFINED_VALUE;
255257
}
@@ -266,7 +268,7 @@ private static String processTemplateVar(String templateVar) throws IOException
266268
processArg);
267269
return UNDEFINED_VALUE;
268270
}
269-
String value = System.getProperty(processArg.substring(2));
271+
String value = SystemProperties.get(processArg.substring(2));
270272
if (value == null || value.isEmpty()) {
271273
return UNDEFINED_VALUE;
272274
}
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
package datadog.trace.bootstrap.config.provider;
22

3+
import static datadog.trace.api.ConfigOrigin.JVM_PROP;
34
import static datadog.trace.util.Strings.propertyNameToSystemPropertyName;
45

6+
import datadog.environment.SystemProperties;
57
import datadog.trace.api.ConfigOrigin;
68

79
public final class SystemPropertiesConfigSource extends ConfigProvider.Source {
8-
910
@Override
1011
protected String get(String key) {
11-
try {
12-
return System.getProperty(propertyNameToSystemPropertyName(key));
13-
} catch (SecurityException e) {
14-
return null;
15-
}
12+
return SystemProperties.get(propertyNameToSystemPropertyName(key));
1613
}
1714

1815
@Override
1916
public ConfigOrigin origin() {
20-
return ConfigOrigin.JVM_PROP;
17+
return JVM_PROP;
2118
}
2219
}

0 commit comments

Comments
 (0)