Skip to content

Commit 51545ca

Browse files
committed
Initialise bytebuddy instrumentation in a new thread at bootstrap
1 parent b09dcc5 commit 51545ca

File tree

3 files changed

+45
-23
lines changed

3 files changed

+45
-23
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/Agent.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,11 @@ public static void start(
206206
remoteConfigEnabled = false;
207207
telemetryEnabled = false;
208208
// apply trace instrumentation, but skip other products at native-image build time
209-
startDatadogAgent(initTelemetry, inst);
209+
try {
210+
startDatadogAgent(initTelemetry, inst).join();
211+
} catch (InterruptedException e) {
212+
Thread.currentThread().interrupt();
213+
}
210214
StaticEventLogger.end("Agent.start");
211215
return;
212216
}
@@ -301,7 +305,7 @@ public static void start(
301305
startCrashTracking();
302306
StaticEventLogger.end("crashtracking");
303307
}
304-
startDatadogAgent(initTelemetry, inst);
308+
final Thread agentLoadThread = startDatadogAgent(initTelemetry, inst);
305309

306310
final EnumSet<Library> libraries = detectLibraries(log);
307311

@@ -384,6 +388,12 @@ public static void start(
384388
StaticEventLogger.end("Profiling");
385389
}
386390

391+
try {
392+
agentLoadThread.join();
393+
} catch (InterruptedException e) {
394+
Thread.currentThread().interrupt();
395+
}
396+
387397
StaticEventLogger.end("Agent.start");
388398
}
389399

@@ -722,7 +732,7 @@ private static void maybeStartRemoteConfig(Class<?> scoClass, Object sco) {
722732
StaticEventLogger.end("Remote Config");
723733
}
724734

725-
private static synchronized void startDatadogAgent(
735+
private static synchronized Thread startDatadogAgent(
726736
final InitializationTelemetry initTelemetry, final Instrumentation inst) {
727737
if (null != inst) {
728738
StaticEventLogger.begin("BytebuddyAgent");
@@ -732,14 +742,15 @@ private static synchronized void startDatadogAgent(
732742
AGENT_CLASSLOADER.loadClass(AGENT_INSTALLER_CLASS_NAME);
733743
final Method agentInstallerMethod =
734744
agentInstallerClass.getMethod("installBytebuddyAgent", Instrumentation.class);
735-
agentInstallerMethod.invoke(null, inst);
745+
return (Thread) agentInstallerMethod.invoke(null, inst);
736746
} catch (final Throwable ex) {
737747
log.error("Throwable thrown while installing the Datadog Agent", ex);
738748
initTelemetry.onFatalError(ex);
739749
} finally {
740750
StaticEventLogger.end("BytebuddyAgent");
741751
}
742752
}
753+
return null;
743754
}
744755

745756
private static synchronized void installDatadogTracer(

dd-java-agent/agent-builder/src/main/java/datadog/trace/agent/tooling/AgentInstaller.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static datadog.trace.agent.tooling.ExtensionFinder.findExtensions;
44
import static datadog.trace.agent.tooling.ExtensionLoader.loadExtensions;
55
import static datadog.trace.agent.tooling.bytebuddy.matcher.GlobalIgnoresMatcher.globalIgnoresMatcher;
6+
import static datadog.trace.util.AgentThreadFactory.AgentThread.AGENT_INIT_INSTRUMENTATION;
7+
import static datadog.trace.util.AgentThreadFactory.newAgentThread;
68
import static net.bytebuddy.matcher.ElementMatchers.isDefaultFinalizer;
79

810
import datadog.environment.SystemProperties;
@@ -71,29 +73,36 @@ private static void circularityErrorWorkaround() {
7173
}
7274
}
7375

74-
public static void installBytebuddyAgent(final Instrumentation inst) {
76+
public static Thread installBytebuddyAgent(final Instrumentation inst) {
7577
/*
7678
* ByteBuddy agent is used by several systems which can be enabled independently;
7779
* we need to install the agent whenever any of them is active.
7880
*/
79-
Set<InstrumenterModule.TargetSystem> enabledSystems = getEnabledSystems();
80-
if (!enabledSystems.isEmpty()) {
81-
installBytebuddyAgent(inst, false, enabledSystems);
82-
if (DEBUG) {
83-
log.debug("Instrumentation installed for {}", enabledSystems);
84-
}
85-
int poolCleaningInterval = InstrumenterConfig.get().getResolverResetInterval();
86-
if (poolCleaningInterval > 0) {
87-
AgentTaskScheduler.get()
88-
.scheduleAtFixedRate(
89-
SharedTypePools::clear,
90-
poolCleaningInterval,
91-
Math.max(poolCleaningInterval, 10),
92-
TimeUnit.SECONDS);
93-
}
94-
} else if (DEBUG) {
95-
log.debug("No target systems enabled, skipping instrumentation.");
96-
}
81+
final Thread ret =
82+
newAgentThread(
83+
AGENT_INIT_INSTRUMENTATION,
84+
() -> {
85+
Set<InstrumenterModule.TargetSystem> enabledSystems = getEnabledSystems();
86+
if (!enabledSystems.isEmpty()) {
87+
installBytebuddyAgent(inst, false, enabledSystems);
88+
if (DEBUG) {
89+
log.debug("Instrumentation installed for {}", enabledSystems);
90+
}
91+
int poolCleaningInterval = InstrumenterConfig.get().getResolverResetInterval();
92+
if (poolCleaningInterval > 0) {
93+
AgentTaskScheduler.get()
94+
.scheduleAtFixedRate(
95+
SharedTypePools::clear,
96+
poolCleaningInterval,
97+
Math.max(poolCleaningInterval, 10),
98+
TimeUnit.SECONDS);
99+
}
100+
} else if (DEBUG) {
101+
log.debug("No target systems enabled, skipping instrumentation.");
102+
}
103+
});
104+
ret.start();
105+
return ret;
97106
}
98107

99108
/**

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public enum AgentThread {
5757

5858
RETRANSFORMER("dd-retransformer"),
5959

60+
AGENT_INIT_INSTRUMENTATION("dd-agent-init-instrumentation"),
61+
6062
LOGS_INTAKE("dd-logs-intake"),
6163

6264
LLMOBS_EVALS_PROCESSOR("dd-llmobs-evals-processor");

0 commit comments

Comments
 (0)