Skip to content

Commit 5ea434b

Browse files
committed
feat(env): Migrate internal apis to environment component
1 parent dda65fd commit 5ea434b

File tree

9 files changed

+41
-37
lines changed

9 files changed

+41
-37
lines changed

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

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

3+
import datadog.environment.EnvironmentVariables;
4+
import datadog.environment.SystemProperties;
35
import datadog.trace.api.env.CapturedEnvironment;
46
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
57
import datadog.trace.util.TraceUtils;
@@ -27,7 +29,7 @@ public class ProcessTags {
2729
public static final String ENTRYPOINT_WORKDIR = "entrypoint.workdir";
2830

2931
// visible for testing
30-
static Function<String, String> envGetter = System::getenv;
32+
static Function<String, String> envGetter = EnvironmentVariables::get;
3133

3234
private static class Lazy {
3335
// the tags are used to compute a hash for dsm hence that map must be sorted.
@@ -58,20 +60,12 @@ private static void fillJeeTags(SortedMap<String, String> tags) {
5860

5961
private static void insertTagFromSysPropIfPresent(
6062
Map<String, String> tags, String propKey, String tagKey) {
61-
String value = maybeGetSystemProperty(propKey);
63+
String value = SystemProperties.get(propKey);
6264
if (value != null) {
6365
tags.put(tagKey, value);
6466
}
6567
}
6668

67-
private static String maybeGetSystemProperty(String propKey) {
68-
try {
69-
return System.getProperty(propKey);
70-
} catch (Throwable ignored) {
71-
}
72-
return null;
73-
}
74-
7569
private static boolean insertTagFromEnvIfPresent(
7670
Map<String, String> tags, String envKey, String tagKey) {
7771
try {
@@ -102,11 +96,7 @@ private static boolean insertLastPathSegmentIfPresent(
10296
}
10397

10498
private static boolean hasSystemProperty(String propKey) {
105-
try {
106-
return System.getProperties().containsKey(propKey);
107-
} catch (Throwable ignored) {
108-
}
109-
return false;
99+
return SystemProperties.get(propKey) != null;
110100
}
111101

112102
private static void fillBaseTags(Map<String, String> tags) {
@@ -123,12 +113,12 @@ private static void fillBaseTags(Map<String, String> tags) {
123113
insertLastPathSegmentIfPresent(tags, processInfo.jarFile.getParent(), ENTRYPOINT_BASEDIR);
124114
}
125115

126-
insertLastPathSegmentIfPresent(tags, maybeGetSystemProperty("user.dir"), ENTRYPOINT_WORKDIR);
116+
insertLastPathSegmentIfPresent(tags, SystemProperties.get("user.dir"), ENTRYPOINT_WORKDIR);
127117
}
128118

129119
private static boolean fillJbossTags(Map<String, String> tags) {
130120
if (insertLastPathSegmentIfPresent(
131-
tags, maybeGetSystemProperty("jboss.home.dir"), "jboss.home")) {
121+
tags, SystemProperties.get("jboss.home.dir"), "jboss.home")) {
132122
insertTagFromSysPropIfPresent(tags, "jboss.server.name", SERVER_NAME);
133123
tags.put("jboss.mode", hasSystemProperty("[Standalone]") ? "standalone" : "domain");
134124
tags.put(SERVER_TYPE, "jboss");

internal-api/src/main/java/datadog/trace/util/PidHelper.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package datadog.trace.util;
22

3+
import datadog.environment.EnvironmentVariables;
34
import datadog.environment.JavaVirtualMachine;
45
import datadog.environment.OperatingSystem;
6+
import datadog.environment.SystemProperties;
57
import de.thetaphi.forbiddenapis.SuppressForbidden;
68
import java.io.IOException;
79
import java.lang.management.ManagementFactory;
810
import java.nio.file.Files;
911
import java.nio.file.Path;
1012
import java.nio.file.Paths;
1113
import java.util.Collections;
14+
import java.util.Objects;
1215
import java.util.Set;
16+
import java.util.function.Predicate;
1317
import java.util.function.Supplier;
1418
import java.util.stream.Collectors;
1519
import java.util.stream.Stream;
@@ -75,14 +79,16 @@ private static String getTempDir() {
7579
if (OperatingSystem.isLinux()) {
7680
return "/tmp";
7781
} else if (OperatingSystem.isWindows()) {
78-
return Stream.of(System.getenv("TMP"), System.getenv("TEMP"), System.getenv("USERPROFILE"))
79-
.filter(String::isEmpty)
82+
return Stream.of("TMP", "TEMP", "USERPROFILE")
83+
.map(EnvironmentVariables::get)
84+
.filter(Objects::nonNull)
85+
.filter(((Predicate<String>) String::isEmpty).negate())
8086
.findFirst()
8187
.orElse("C:\\Windows");
8288
} else if (OperatingSystem.isMacOs()) {
83-
return System.getenv("TMPDIR");
89+
return EnvironmentVariables.get("TMPDIR");
8490
} else {
85-
return System.getProperty("java.io.tmpdir");
91+
return SystemProperties.get("java.io.tmpdir");
8692
}
8793
} else {
8894
try {
@@ -93,7 +99,7 @@ private static String getTempDir() {
9399
.invoke(null);
94100
} catch (Throwable t) {
95101
// Fall back to constants based on J9 source code, may not have perfect coverage
96-
String tmpDir = System.getProperty("java.io.tmpdir");
102+
String tmpDir = SystemProperties.get("java.io.tmpdir");
97103
if (tmpDir != null && !tmpDir.isEmpty()) {
98104
return tmpDir;
99105
} else if (OperatingSystem.isWindows()) {
@@ -114,7 +120,7 @@ private static Path getJavaProcessesDir() {
114120
} else {
115121
// Emulating the hotspot way to enumerate the JVM processes using the perfdata file
116122
// https://github.com/openjdk/jdk/blob/d7cb933b89839b692f5562aeeb92076cd25a99f6/src/hotspot/share/runtime/perfMemory.cpp#L244
117-
return Paths.get(getTempDir(), "hsperfdata_" + System.getProperty("user.name"));
123+
return Paths.get(getTempDir(), "hsperfdata_" + SystemProperties.get("user.name"));
118124
}
119125
}
120126

internal-api/src/main/java/datadog/trace/util/ProcessUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package datadog.trace.util;
22

33
import datadog.environment.JavaVirtualMachine;
4+
import datadog.environment.SystemProperties;
45
import de.thetaphi.forbiddenapis.SuppressForbidden;
56
import java.util.function.Supplier;
67
import javax.annotation.Nullable;
@@ -43,7 +44,7 @@ public static String getCurrentJvmPath() {
4344
}
4445

4546
// JDK/JRE home, does not include "bin/java" portion
46-
return System.getProperty("java.home");
47+
return SystemProperties.get("java.home");
4748
}
4849

4950
private ProcessUtils() {}

internal-api/src/main/java/datadog/trace/util/TempLocationManager.java

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

33
import static datadog.trace.api.telemetry.LogCollector.SEND_TELEMETRY;
44

5+
import datadog.environment.EnvironmentVariables;
6+
import datadog.environment.SystemProperties;
57
import datadog.trace.api.config.ProfilingConfig;
68
import datadog.trace.bootstrap.config.provider.ConfigProvider;
79
import java.io.IOException;
@@ -304,9 +306,9 @@ private TempLocationManager() {
304306

305307
// @VisibleForTesting
306308
static String getBaseTempDirName() {
307-
String userName = System.getProperty("user.name");
309+
String userName = SystemProperties.get("user.name");
308310
// unlikely, but fall-back to system env based user name
309-
userName = userName == null ? System.getenv("USER") : userName;
311+
userName = userName == null ? EnvironmentVariables.get("USER") : userName;
310312
// make sure we do not have any illegal characters in the user name
311313
userName =
312314
userName != null ? userName.replace('.', '_').replace('/', '_').replace(' ', '_') : null;

telemetry/src/main/java/datadog/telemetry/HostInfo.java

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

33
import datadog.environment.OperatingSystem;
4+
import datadog.environment.SystemProperties;
45
import datadog.trace.api.Config;
56
import java.io.IOException;
67
import java.nio.charset.StandardCharsets;
@@ -40,7 +41,7 @@ public static String getOsName() {
4041
// os.name == Mac OS X, while uanme -s == Darwin. We'll hardcode it to Darwin.
4142
osName = "Darwin";
4243
} else {
43-
osName = System.getProperty("os.name");
44+
osName = SystemProperties.get("os.name");
4445
}
4546
}
4647
return osName;
@@ -60,7 +61,7 @@ public static String getKernelName() {
6061
public static String getKernelRelease() {
6162
if (kernelRelease == null) {
6263
// In Linux, os.version == uname -r
63-
kernelRelease = System.getProperty("os.version");
64+
kernelRelease = SystemProperties.get("os.version");
6465
}
6566
return kernelRelease;
6667
}
@@ -78,7 +79,7 @@ public static String getKernelVersion() {
7879
}
7980
kernelVersion = version;
8081
} else {
81-
kernelVersion = System.getProperty("os.version");
82+
kernelVersion = SystemProperties.get("os.version");
8283
}
8384
}
8485
return kernelVersion;
@@ -87,7 +88,7 @@ public static String getKernelVersion() {
8788
public static String getArchitecture() {
8889
if (architecture == null) {
8990
// In Linux, os.arch == uname -me
90-
architecture = System.getProperty("os.arch");
91+
architecture = SystemProperties.get("os.arch");
9192
}
9293
return architecture;
9394
}
@@ -135,7 +136,7 @@ public static String getOsVersion() {
135136
return name + " " + version;
136137
}
137138
}
138-
return System.getProperty("os.version");
139+
return SystemProperties.get("os.version");
139140
}
140141
}
141142
}

telemetry/src/main/java/datadog/telemetry/TelemetryRequest.java

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

33
import datadog.common.container.ContainerInfo;
44
import datadog.communication.ddagent.TracerVersion;
5+
import datadog.environment.EnvironmentVariables;
56
import datadog.telemetry.api.DistributionSeries;
67
import datadog.telemetry.api.Integration;
78
import datadog.telemetry.api.LogMessage;
@@ -111,9 +112,9 @@ public void writeProducts() {
111112
}
112113

113114
public void writeInstallSignature() {
114-
String installId = System.getenv("DD_INSTRUMENTATION_INSTALL_ID");
115-
String installType = System.getenv("DD_INSTRUMENTATION_INSTALL_TYPE");
116-
String installTime = System.getenv("DD_INSTRUMENTATION_INSTALL_TIME");
115+
String installId = EnvironmentVariables.get("DD_INSTRUMENTATION_INSTALL_ID");
116+
String installType = EnvironmentVariables.get("DD_INSTRUMENTATION_INSTALL_TYPE");
117+
String installTime = EnvironmentVariables.get("DD_INSTRUMENTATION_INSTALL_TIME");
117118

118119
try {
119120
requestBody.writeInstallSignature(installId, installType, installTime);

utils/container-utils/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ plugins {
55
apply(from = "$rootDir/gradle/java.gradle")
66

77
dependencies {
8+
implementation(project(":components:environment"))
89
implementation(libs.slf4j)
910

1011
testImplementation(project(":utils:test-utils"))

utils/container-utils/src/main/java/datadog/common/container/ServerlessInfo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datadog.common.container;
22

3+
import datadog.environment.EnvironmentVariables;
34
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
45
import java.io.File;
56

@@ -12,7 +13,7 @@ public class ServerlessInfo {
1213
private final boolean hasExtension;
1314

1415
private ServerlessInfo(final String extensionPath) {
15-
this.functionName = System.getenv(AWS_FUNCTION_VARIABLE);
16+
this.functionName = EnvironmentVariables.get(AWS_FUNCTION_VARIABLE);
1617
if (null == extensionPath) {
1718
this.hasExtension = false;
1819
} else {

utils/socket-utils/src/main/java/datadog/common/socket/SocketUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static datadog.trace.api.ConfigDefaults.DEFAULT_TRACE_AGENT_SOCKET_PATH;
44

55
import datadog.environment.OperatingSystem;
6+
import datadog.environment.SystemProperties;
67
import datadog.trace.api.Config;
78
import datadog.trace.api.config.TracerConfig;
89
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -26,9 +27,9 @@ && new File(DEFAULT_TRACE_AGENT_SOCKET_PATH).exists()) {
2627
} else /* windows */ {
2728
if (unixDomainSocket != null) {
2829
log.warn(
29-
"{} setting not supported on {}. Reverting to the default.",
30+
"{} setting not supported on {}. Reverting to the default.",
3031
TracerConfig.AGENT_UNIX_DOMAIN_SOCKET,
31-
System.getProperty("os.name"));
32+
SystemProperties.get("os.name"));
3233
unixDomainSocket = null;
3334
}
3435
}

0 commit comments

Comments
 (0)