Skip to content

Improve agent to avoid loading global config on main thread #9190

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

Merged
merged 7 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -24,7 +24,7 @@
*/
class CommandLine {
private static final String SUN_JAVA_COMMAND_PROPERTY = "sun.java.command";
final List<String> fullCommand = findFullCommand();
private final List<String> fullCommand = findFullCommand();
final String name = getCommandName();
final List<String> arguments = getCommandArguments();

Expand All @@ -35,14 +35,14 @@ private List<String> findFullCommand() {
}

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

private List<String> getCommandArguments() {
if (fullCommand.isEmpty()) {
return fullCommand;
if (this.fullCommand.isEmpty()) {
return this.fullCommand;
} else {
return fullCommand.subList(1, fullCommand.size());
return this.fullCommand.subList(1, this.fullCommand.size());
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package datadog.trace.bootstrap;

import datadog.environment.EnvironmentVariables;
import java.util.EnumSet;
import org.slf4j.Logger;

Expand All @@ -15,7 +16,7 @@ public enum Library {
public static EnumSet<Library> detectLibraries(final Logger log) {
final EnumSet<Library> libraries = EnumSet.noneOf(Library.class);

final String jbossHome = System.getenv("JBOSS_HOME");
final String jbossHome = EnvironmentVariables.get("JBOSS_HOME");
if (jbossHome != null) {
log.debug("Env - jboss: {}", jbossHome);
libraries.add(WILDFLY);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package datadog.trace.logging.ddlogger;

import datadog.environment.EnvironmentVariables;
import datadog.environment.SystemProperties;
import datadog.trace.api.Platform;
import datadog.trace.logging.LogLevel;
import datadog.trace.logging.LogLevelSwitcher;
Expand Down Expand Up @@ -99,14 +101,14 @@ && isFlagEnabled(

private static boolean isFlagEnabled(
final String systemProperty, final String envVar, final boolean defaultValue) {
String value = System.getProperty(systemProperty);
String value = SystemProperties.get(systemProperty);
if ("true".equalsIgnoreCase(value)) {
return true;
}
if ("false".equalsIgnoreCase(value)) {
return false;
}
value = System.getenv(envVar);
value = EnvironmentVariables.get(envVar);
if ("true".equalsIgnoreCase(value)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,20 @@ private static void agentmainImpl(
if (agentClass.getClassLoader() != null) {
throw new IllegalStateException("DD Java Agent NOT added to bootstrap classpath.");
}
final Method startMethod =
agentClass.getMethod("start", Object.class, Instrumentation.class, URL.class, String.class);

startMethod.invoke(null, initTelemetry, inst, agentJarURL, agentArgs);
try {
final Method startMethod =
agentClass.getMethod(
"start", Object.class, Instrumentation.class, URL.class, String.class);
startMethod.invoke(null, initTelemetry, inst, agentJarURL, agentArgs);
} catch (Throwable e) {
throw new IllegalStateException("Unable to start DD Java Agent.", e);
}
}

static boolean getConfig(String configName) {
switch (configName) {
case LIB_INJECTION_ENABLED_ENV_VAR:
return System.getenv(LIB_INJECTION_ENABLED_ENV_VAR) != null;
return EnvironmentVariables.get(LIB_INJECTION_ENABLED_ENV_VAR) != null;
case LIB_INJECTION_FORCE_SYS_PROP:
{
String envVarName =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package datadog.trace.bootstrap;

import datadog.trace.bootstrap.environment.SystemProperties;
import de.thetaphi.forbiddenapis.SuppressForbidden;
import java.io.BufferedReader;
import java.io.IOException;
Expand Down Expand Up @@ -174,7 +175,7 @@ public static String getAgentVersion() throws IOException {
}

private static void checkProfilerEnv(final String[] args) throws Exception {
String tmpDir = args.length == 2 ? args[1] : System.getProperty("java.io.tmpdir");
String tmpDir = args.length == 2 ? args[1] : SystemProperties.get("java.io.tmpdir");

installAgentCLI().getMethod("checkProfilerEnv", String.class).invoke(null, tmpDir);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ protected boolean checkFileExecutePermission(FilePermission perm, Object ctx, St
}
}

public static final Result runTestJvm(Class<? extends TestSecurityManager> securityManagerClass)
public static Result runTestJvm(Class<? extends TestSecurityManager> securityManagerClass)
throws Exception {
return runTestJvm(securityManagerClass, DEFAULT_TRACE_AGENT_PORT);
}

public static final Result runTestJvm(
public static Result runTestJvm(
Class<? extends TestSecurityManager> securityManagerClass, int port) throws Exception {

File jarFile =
Expand Down Expand Up @@ -130,8 +130,8 @@ public static final Result runTestJvm(
}
}

static final File createTempFile(
String baseName, String extension, Set<PosixFilePermission> perms) throws IOException {
static File createTempFile(String baseName, String extension, Set<PosixFilePermission> perms)
throws IOException {
Path path =
Files.createTempFile(
baseName + "-integration-telemetry-check",
Expand All @@ -142,25 +142,25 @@ static final File createTempFile(
return file;
}

static final void write(File file, String... lines) throws IOException {
static void write(File file, String... lines) throws IOException {
Files.write(file.toPath(), Arrays.asList(lines));
}

static final String read(File file) {
static String read(File file) {
try {
return new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
} catch (IOException e) {
return null;
}
}

static final void delete(File... tempFiles) {
static void delete(File... tempFiles) {
for (File file : tempFiles) {
file.delete();
}
}

public static final Class<?>[] requiredClasses(
public static Class<?>[] requiredClasses(
Class<? extends TestSecurityManager> securityManagerClass) {

if (securityManagerClass == null) {
Expand All @@ -178,7 +178,7 @@ public static final Class<?>[] requiredClasses(
}
}

public static final Map<String, String> envVars(File forwarderFile) {
public static Map<String, String> envVars(File forwarderFile) {
Map<String, String> envVars = new HashMap<>();
envVars.put("DD_TELEMETRY_FORWARDER_PATH", forwarderFile.getAbsolutePath());
return envVars;
Expand Down
9 changes: 0 additions & 9 deletions internal-api/src/main/java/datadog/trace/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@
import static datadog.trace.api.config.GeneralConfig.APPLICATION_KEY;
import static datadog.trace.api.config.GeneralConfig.APPLICATION_KEY_FILE;
import static datadog.trace.api.config.GeneralConfig.AZURE_APP_SERVICES;
import static datadog.trace.api.config.GeneralConfig.DATA_JOBS_COMMAND_PATTERN;
import static datadog.trace.api.config.GeneralConfig.DATA_JOBS_ENABLED;
import static datadog.trace.api.config.GeneralConfig.DATA_JOBS_OPENLINEAGE_ENABLED;
import static datadog.trace.api.config.GeneralConfig.DATA_STREAMS_BUCKET_DURATION_SECONDS;
Expand Down Expand Up @@ -1143,7 +1142,6 @@ public static String getHostName() {
private final int cwsTlsRefresh;

private final boolean dataJobsEnabled;
private final String dataJobsCommandPattern;
private final boolean dataJobsOpenLineageEnabled;

private final boolean dataStreamsEnabled;
Expand Down Expand Up @@ -2535,7 +2533,6 @@ PROFILING_DATADOG_PROFILER_ENABLED, isDatadogProfilerSafeInCurrentEnvironment())
dataJobsOpenLineageEnabled =
configProvider.getBoolean(
DATA_JOBS_OPENLINEAGE_ENABLED, DEFAULT_DATA_JOBS_OPENLINEAGE_ENABLED);
dataJobsCommandPattern = configProvider.getString(DATA_JOBS_COMMAND_PATTERN);

dataStreamsEnabled =
configProvider.getBoolean(DATA_STREAMS_ENABLED, DEFAULT_DATA_STREAMS_ENABLED);
Expand Down Expand Up @@ -4385,10 +4382,6 @@ public boolean isDataJobsOpenLineageEnabled() {
return dataJobsOpenLineageEnabled;
}

public String getDataJobsCommandPattern() {
return dataJobsCommandPattern;
}

public boolean isApmTracingEnabled() {
return apmTracingEnabled;
}
Expand Down Expand Up @@ -5687,8 +5680,6 @@ public String toString() {
+ appSecRaspEnabled
+ ", dataJobsEnabled="
+ dataJobsEnabled
+ ", dataJobsCommandPattern="
+ dataJobsCommandPattern
+ ", apmTracingEnabled="
+ apmTracingEnabled
+ ", jdkSocketEnabled="
Expand Down