Skip to content

Introduce environment component #8919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 20 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import static datadog.trace.util.ProcessSupervisor.Health.NEVER_CHECKED;
import static datadog.trace.util.ProcessSupervisor.Health.READY_TO_START;

import datadog.environment.OperatingSystem;
import datadog.trace.api.Config;
import datadog.trace.api.Platform;
import datadog.trace.util.ProcessSupervisor;
import java.io.Closeable;
import java.io.File;
Expand All @@ -17,7 +17,7 @@ public class ExternalAgentLauncher implements Closeable {
private static final Logger log = LoggerFactory.getLogger(ExternalAgentLauncher.class);

private static final ProcessBuilder.Redirect DISCARD =
ProcessBuilder.Redirect.to(new File((Platform.isWindows() ? "NUL" : "/dev/null")));
ProcessBuilder.Redirect.to(new File((OperatingSystem.isWindows() ? "NUL" : "/dev/null")));

private ProcessSupervisor traceProcessSupervisor;
private ProcessSupervisor dogStatsDProcessSupervisor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import com.timgroup.statsd.NoOpDirectStatsDClient;
import com.timgroup.statsd.NonBlockingStatsDClientBuilder;
import com.timgroup.statsd.StatsDClientErrorHandler;
import datadog.environment.OperatingSystem;
import datadog.trace.api.Config;
import datadog.trace.api.Platform;
import datadog.trace.relocate.api.IOLogger;
import datadog.trace.util.AgentTaskScheduler;
import datadog.trace.util.AgentThreadFactory;
Expand Down Expand Up @@ -136,7 +136,7 @@ private void doConnect() {
if (bufferSize != null) {
clientBuilder.socketBufferSize(bufferSize);
}
int packetSize = Platform.isMac() ? 2048 : 8192;
int packetSize = OperatingSystem.isMacOs() ? 2048 : 8192;
if (bufferSize != null && bufferSize < packetSize) {
packetSize = bufferSize;
}
Expand Down Expand Up @@ -185,7 +185,7 @@ private void discoverConnectionSettings() {
}

if (null == host) {
if (!Platform.isWindows() && new File(DEFAULT_DOGSTATSD_SOCKET_PATH).exists()) {
if (!OperatingSystem.isWindows() && new File(DEFAULT_DOGSTATSD_SOCKET_PATH).exists()) {
log.info("Detected {}. Using it to send StatsD data.", DEFAULT_DOGSTATSD_SOCKET_PATH);
host = DEFAULT_DOGSTATSD_SOCKET_PATH;
port = 0; // tells dogstatsd client to treat host as a socket path
Expand Down
1 change: 0 additions & 1 deletion components/cli/build.gradle.kts

This file was deleted.

118 changes: 0 additions & 118 deletions components/cli/gradle.lockfile

This file was deleted.

88 changes: 0 additions & 88 deletions components/cli/src/main/java/datadog/cli/CLIHelper.java

This file was deleted.

18 changes: 18 additions & 0 deletions components/environment/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
plugins {
`java-library`
}

apply(from = "$rootDir/gradle/java.gradle")

extra.set("minimumInstructionCoverage", 0.7)
val excludedClassesCoverage by extra {
listOf(
"datadog.environment.JavaVirtualMachine", // depends on OS and JVM vendor
"datadog.environment.JavaVirtualMachine.JvmOptionsHolder", // depends on OS and JVM vendor
"datadog.environment.JvmOptions", // depends on OS and JVM vendor
"datadog.environment.OperatingSystem", // depends on OS
)
}
val excludedClassesBranchCoverage by extra {
listOf("datadog.environment.CommandLine") // tested using forked process
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package datadog.environment;

import static java.util.Collections.emptyList;

import de.thetaphi.forbiddenapis.SuppressForbidden;
import java.util.Arrays;
import java.util.List;

/**
* Fetches and captures the command line, both command and its arguments. It relies on a
* non-standard {@code sun.java.command} system property and was tested on:
*
* <ul>
* <li>OracleJDK,
* <li>OpenJDK,
* <li>Temurin based JDK,
* <li>IMB JDK,
* <li>Azul Zulu,
* <li>Amazon Coretto,
* </ul>
*/
class CommandLine {
private static final String SUN_JAVA_COMMAND_PROPERTY = "sun.java.command";
final List<String> fullCommand = findFullCommand();
final String command = getCommand();
final List<String> arguments = getCommandArguments();

@SuppressForbidden // split on single-character uses fast path
private List<String> findFullCommand() {
String command = SystemProperties.getOrDefault(SUN_JAVA_COMMAND_PROPERTY, "").trim();
return command.isEmpty() ? emptyList() : Arrays.asList(command.split(" "));
}

private String getCommand() {
return fullCommand.isEmpty() ? null : fullCommand.get(0);
}

private List<String> getCommandArguments() {
if (fullCommand.isEmpty()) {
return fullCommand;
} else {
return fullCommand.subList(1, fullCommand.size());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package datadog.environment;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* Safely queries environment variables against security manager.
*
* @see <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/SecurityManager.html">Security
* Manager</a>
*/
public final class EnvironmentVariables {
private EnvironmentVariables() {}

/**
* Gets an environment variable value.
*
* @param name The environment variable name.
* @return The environment variable value, {@code null} if missing or can't be retrieved.
*/
public static @Nullable String get(String name) {
return getOrDefault(name, null);
}

/**
* Gets an environment variable value, or default value if missing or can't be retrieved.
*
* @param name The environment variable name.
* @param defaultValue The default value to return if the environment variable is missing or can't
* be retrieved.
* @return The environment variable value, {@code defaultValue} if missing or can't be retrieved.
*/
public static String getOrDefault(@Nonnull String name, String defaultValue) {
try {
String value = System.getenv(name);
return value == null ? defaultValue : value;
} catch (SecurityException e) {
return defaultValue;
}
}
}
Loading
Loading