Skip to content

Commit b6c331b

Browse files
committed
feat(env): Migrate ConfigProvider to environment component
1 parent 78ad76f commit b6c331b

File tree

6 files changed

+29
-31
lines changed

6 files changed

+29
-31
lines changed

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: 2 additions & 1 deletion
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;
@@ -472,7 +473,7 @@ private static Properties loadConfigurationFile(ConfigProvider configProvider) {
472473

473474
// Normalizing tilde (~) paths for unix systems
474475
configurationFilePath =
475-
configurationFilePath.replaceFirst("^~", System.getProperty("user.home"));
476+
configurationFilePath.replaceFirst("^~", SystemProperties.getOrDefault("user.home", ""));
476477

477478
// Configuration properties file is optional
478479
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: 8 additions & 9 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
}
@@ -205,7 +204,7 @@ private static Properties loadOtelConfigFile() {
205204
String path = getProperty("otel.javaagent.configuration-file");
206205
if (null != path && !path.isEmpty()) {
207206
if (path.charAt(0) == '~') {
208-
path = System.getProperty("user.home") + path.substring(1);
207+
path = SystemProperties.getOrDefault("user.home", "") + path.substring(1);
209208
}
210209
File file = new File(path);
211210
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)