Skip to content

Commit b10125d

Browse files
committed
feat(env): Migrate ConfigProvider to environment component
1 parent 24b5903 commit b10125d

File tree

9 files changed

+53
-45
lines changed

9 files changed

+53
-45
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
@@ -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;

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: 6 additions & 4 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;
@@ -153,7 +155,7 @@ static boolean selectorMatch(String origin, List<String> matches, String operato
153155
if (key == null) {
154156
return false;
155157
}
156-
String envValue = System.getenv(key.toUpperCase(Locale.ROOT));
158+
String envValue = EnvironmentVariables.get(key.toUpperCase(Locale.ROOT));
157159
return matchOperator(envValue, operator, matches);
158160
case "process_arguments":
159161
if (key == null) {
@@ -166,7 +168,7 @@ static boolean selectorMatch(String origin, List<String> matches, String operato
166168
key);
167169
return false;
168170
}
169-
String argValue = System.getProperty(key.substring(2));
171+
String argValue = SystemProperties.get(key.substring(2));
170172
return matchOperator(argValue, operator, matches);
171173
case "tags":
172174
// TODO: Support this down the line (Must define the source of "tags" first)
@@ -229,7 +231,7 @@ private static String processTemplateVar(String templateVar) throws IOException
229231
if (envVar.isEmpty()) {
230232
throw new IOException("Empty environment variable name in template");
231233
}
232-
String value = System.getenv(envVar.toUpperCase(Locale.ROOT));
234+
String value = EnvironmentVariables.get(envVar.toUpperCase(Locale.ROOT));
233235
if (value == null || value.isEmpty()) {
234236
return UNDEFINED_VALUE;
235237
}
@@ -246,7 +248,7 @@ private static String processTemplateVar(String templateVar) throws IOException
246248
processArg);
247249
return UNDEFINED_VALUE;
248250
}
249-
String value = System.getProperty(processArg.substring(2));
251+
String value = SystemProperties.get(processArg.substring(2));
250252
if (value == null || value.isEmpty()) {
251253
return UNDEFINED_VALUE;
252254
}
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)