Skip to content

Commit ae9b776

Browse files
committed
Protect exception advices from NoClassDefFound
1 parent 425276d commit ae9b776

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

dd-java-agent/agent-bootstrap/src/main/java11/datadog/trace/bootstrap/instrumentation/jfr/exceptions/ExceptionProfiling.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,29 @@
22

33
import datadog.trace.api.Config;
44
import datadog.trace.bootstrap.CallDepthThreadLocalMap;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
57

68
/**
79
* JVM-wide singleton exception profiling service. Uses {@linkplain Config} class to configure
810
* itself using either system properties, environment or properties override.
911
*/
1012
public final class ExceptionProfiling {
1113

14+
private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionProfiling.class);
15+
1216
/** Lazy initialization-on-demand. */
1317
private static final class Holder {
14-
static final ExceptionProfiling INSTANCE = new ExceptionProfiling(Config.get());
18+
static final ExceptionProfiling INSTANCE = create();
19+
20+
private static ExceptionProfiling create() {
21+
try {
22+
return new ExceptionProfiling(Config.get());
23+
} catch (Throwable t) {
24+
LOGGER.debug("Unable to create ExceptionProfiling", t);
25+
return null;
26+
}
27+
}
1528
}
1629

1730
/**

dd-java-agent/agent-bootstrap/src/main/java11/datadog/trace/bootstrap/instrumentation/jfr/exceptions/ExceptionSampleEvent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public ExceptionSampleEvent(Throwable e, boolean sampled, boolean firstOccurrenc
4545
}
4646

4747
private static String getMessage(Throwable t) {
48-
if (ExceptionProfiling.getInstance().recordExceptionMessage()) {
48+
final ExceptionProfiling exceptionProfiling = ExceptionProfiling.getInstance();
49+
if (exceptionProfiling != null && exceptionProfiling.recordExceptionMessage()) {
4950
try {
5051
return t.getMessage();
5152
} catch (Throwable ignored) {

dd-java-agent/agent-profiling/profiling-controller-openjdk/src/main/java/com/datadog/profiling/controller/openjdk/OpenJdkController.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,10 @@ && isEventEnabled(recordingSettings, "jdk.NativeMethodSample")) {
258258
this.recordingSettings = Collections.unmodifiableMap(recordingSettings);
259259

260260
if (isEventEnabled(this.recordingSettings, "datadog.ExceptionSample")) {
261-
ExceptionProfiling.getInstance().start();
261+
final ExceptionProfiling exceptionProfiling = ExceptionProfiling.getInstance();
262+
if (exceptionProfiling != null) {
263+
exceptionProfiling.start();
264+
}
262265
}
263266

264267
if (Config.get().isProfilingBackPressureSamplingEnabled()) {

dd-java-agent/instrumentation/exception-profiling/src/main/java11/datadog/exceptions/instrumentation/ThrowableInstanceAdvice.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ public static void onExit(@Advice.This final Object t) {
4545
* JFR will assign the stacktrace depending on the place where the event is committed.
4646
* Therefore, we need to commit the event here, right in the 'Exception' constructor
4747
*/
48-
final ExceptionSampleEvent event = ExceptionProfiling.getInstance().process((Throwable) t);
48+
final ExceptionSampleEvent event = null;
49+
final ExceptionProfiling exceptionProfiling = ExceptionProfiling.getInstance();
50+
if (exceptionProfiling != null) {
51+
exceptionProfiling.process((Throwable) t);
52+
}
4953
if (event != null && event.shouldCommit()) {
5054
event.commit();
5155
}

0 commit comments

Comments
 (0)