Skip to content

Commit 884b559

Browse files
committed
Refactor DI startup
Make sure Exception Replay can be started up without DI. Remote Config subscription is not started if DI is not enabled now. The debugger agent is initialized if either Dynamic Instrumentation or Exception Replay is enabled
1 parent b4718bd commit 884b559

3 files changed

Lines changed: 38 additions & 21 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ private enum AgentFeature {
103103
USM(propertyNameToSystemPropertyName(UsmConfig.USM_ENABLED), false),
104104
TELEMETRY(propertyNameToSystemPropertyName(GeneralConfig.TELEMETRY_ENABLED), true),
105105
DEBUGGER(propertyNameToSystemPropertyName(DebuggerConfig.DEBUGGER_ENABLED), false),
106+
EXCEPTION_DEBUGGING(
107+
propertyNameToSystemPropertyName(DebuggerConfig.EXCEPTION_REPLAY_ENABLED), false),
106108
DATA_JOBS(propertyNameToSystemPropertyName(GeneralConfig.DATA_JOBS_ENABLED), false),
107109
AGENTLESS_LOG_SUBMISSION(
108110
propertyNameToSystemPropertyName(GeneralConfig.AGENTLESS_LOG_SUBMISSION_ENABLED), false);
@@ -149,6 +151,7 @@ public boolean isEnabledByDefault() {
149151
private static boolean usmEnabled = false;
150152
private static boolean telemetryEnabled = true;
151153
private static boolean debuggerEnabled = false;
154+
private static boolean exceptionDebuggingEnabled = false;
152155
private static boolean agentlessLogSubmissionEnabled = false;
153156

154157
/**
@@ -259,6 +262,7 @@ public static void start(
259262
cwsEnabled = isFeatureEnabled(AgentFeature.CWS);
260263
telemetryEnabled = isFeatureEnabled(AgentFeature.TELEMETRY);
261264
debuggerEnabled = isFeatureEnabled(AgentFeature.DEBUGGER);
265+
exceptionDebuggingEnabled = isFeatureEnabled(AgentFeature.EXCEPTION_DEBUGGING);
262266
agentlessLogSubmissionEnabled = isFeatureEnabled(AgentFeature.AGENTLESS_LOG_SUBMISSION);
263267

264268
if (profilingEnabled) {
@@ -1069,7 +1073,7 @@ private static void shutdownProfilingAgent(final boolean sync) {
10691073
}
10701074

10711075
private static void maybeStartDebugger(Instrumentation inst, Class<?> scoClass, Object sco) {
1072-
if (!debuggerEnabled) {
1076+
if (!debuggerEnabled && !exceptionDebuggingEnabled) {
10731077
return;
10741078
}
10751079
if (!remoteConfigEnabled) {

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/agent/DebuggerAgent.java

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ public class DebuggerAgent {
5858
public static synchronized void run(
5959
Instrumentation instrumentation, SharedCommunicationObjects sco) {
6060
Config config = Config.get();
61-
if (!config.isDebuggerEnabled()) {
62-
LOGGER.info("Debugger agent disabled");
63-
return;
64-
}
65-
LOGGER.info("Starting Dynamic Instrumentation");
6661
ClassesToRetransformFinder classesToRetransformFinder = new ClassesToRetransformFinder();
6762
setupSourceFileTracking(instrumentation, classesToRetransformFinder);
6863
Redaction.addUserDefinedKeywords(config);
@@ -95,6 +90,7 @@ public static synchronized void run(
9590
DebuggerContext.initTracer(new DebuggerTracer(debuggerSink.getProbeStatusSink()));
9691
DefaultExceptionDebugger defaultExceptionDebugger = null;
9792
if (config.isDebuggerExceptionEnabled()) {
93+
LOGGER.info("Starting Exception Replay");
9894
defaultExceptionDebugger =
9995
new DefaultExceptionDebugger(
10096
configurationUpdater,
@@ -112,6 +108,36 @@ public static synchronized void run(
112108
setupInstrumentTheWorldTransformer(
113109
config, instrumentation, debuggerSink, statsdMetricForwarder);
114110
}
111+
// Dynamic Instrumentation
112+
if (config.isDebuggerEnabled()) {
113+
startDynamicInstrumentation(
114+
instrumentation, sco, config, configurationUpdater, debuggerSink, classNameFiltering);
115+
}
116+
try {
117+
/*
118+
Note: shutdown hooks are tricky because JVM holds reference for them forever preventing
119+
GC for anything that is reachable from it.
120+
*/
121+
Runtime.getRuntime().addShutdownHook(new ShutdownHook(configurationPoller, debuggerSink));
122+
} catch (final IllegalStateException ex) {
123+
// The JVM is already shutting down.
124+
}
125+
ExceptionProbeManager exceptionProbeManager =
126+
defaultExceptionDebugger != null
127+
? defaultExceptionDebugger.getExceptionProbeManager()
128+
: null;
129+
TracerFlare.addReporter(
130+
new DebuggerReporter(configurationUpdater, sink, exceptionProbeManager));
131+
}
132+
133+
private static void startDynamicInstrumentation(
134+
Instrumentation instrumentation,
135+
SharedCommunicationObjects sco,
136+
Config config,
137+
ConfigurationUpdater configurationUpdater,
138+
DebuggerSink debuggerSink,
139+
ClassNameFiltering classNameFiltering) {
140+
LOGGER.info("Starting Dynamic Instrumentation");
115141
String probeFileLocation = config.getDebuggerProbeFileLocation();
116142
if (probeFileLocation != null) {
117143
Path probeFilePath = Paths.get(probeFileLocation);
@@ -133,24 +159,9 @@ public static synchronized void run(
133159
}
134160
}
135161
subscribeConfigurationPoller(config, configurationUpdater, symDBEnablement);
136-
try {
137-
/*
138-
Note: shutdown hooks are tricky because JVM holds reference for them forever preventing
139-
GC for anything that is reachable from it.
140-
*/
141-
Runtime.getRuntime().addShutdownHook(new ShutdownHook(configurationPoller, debuggerSink));
142-
} catch (final IllegalStateException ex) {
143-
// The JVM is already shutting down.
144-
}
145162
} else {
146163
LOGGER.debug("No configuration poller available from SharedCommunicationObjects");
147164
}
148-
ExceptionProbeManager exceptionProbeManager =
149-
defaultExceptionDebugger != null
150-
? defaultExceptionDebugger.getExceptionProbeManager()
151-
: null;
152-
TracerFlare.addReporter(
153-
new DebuggerReporter(configurationUpdater, sink, exceptionProbeManager));
154165
}
155166

156167
private static DebuggerSink createDebuggerSink(Config config, ProbeStatusSink probeStatusSink) {

dd-smoke-tests/debugger-integration-tests/src/test/java/datadog/smoketest/ExceptionDebuggerIntegrationTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ protected ProcessBuilder createProcessBuilder(Path logFilePath, String... params
3434
commandParams.add("-Ddd.exception.replay.enabled=true"); // enable exception replay
3535
commandParams.add("-Ddd.internal.exception.replay.only.local.root=false"); // for all spans
3636
commandParams.add("-Ddd.third.party.excludes=datadog.smoketest");
37+
// disable DI to make sure exception debugger works alone
38+
commandParams.remove("-Ddd.dynamic.instrumentation.enabled=true");
3739
return ProcessBuilderHelper.createProcessBuilder(
3840
commandParams, logFilePath, getAppClass(), params);
3941
}

0 commit comments

Comments
 (0)