Skip to content

Commit c207781

Browse files
Added logic to unwind and surface the potential root cause when an error occurs during the agent's premain initialization phase.
1 parent 562e533 commit c207781

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.OutputStream;
77
import java.util.ArrayList;
88
import java.util.List;
9+
import java.util.Stack;
910

1011
/** Thread safe telemetry class used to relay information about tracer activation. */
1112
public abstract class BootstrapInitializationTelemetry {
@@ -115,7 +116,23 @@ public void onAbort(String reasonCode) {
115116

116117
@Override
117118
public void onError(Throwable t) {
118-
onPoint("library_entrypoint.error", "error_type:" + t.getClass().getName());
119+
Stack<String> causes = new Stack<>();
120+
121+
Throwable cause = t.getCause();
122+
if (cause != null) {
123+
while (cause != null) {
124+
causes.push(cause.getClass().getName());
125+
cause = cause.getCause();
126+
}
127+
}
128+
causes.push(t.getClass().getName());
129+
130+
// Limit the number of causes to avoid overpopulating the JSON payload.
131+
int cnt = Math.min(5, causes.size());
132+
while (cnt > 0) {
133+
onPoint("library_entrypoint.error", "error_type:" + causes.pop());
134+
cnt--;
135+
}
119136
}
120137

121138
@Override

dd-java-agent/src/test/groovy/datadog/trace/bootstrap/BootstrapInitializationTelemetryTest.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ class BootstrapInitializationTelemetryTest extends Specification {
7676
!capture.json().contains("library_entrypoint.complete")
7777
}
7878

79+
def "unwind root cause"() {
80+
when:
81+
initTelemetry.initMetaInfo("runtime_name", "java")
82+
initTelemetry.initMetaInfo("runtime_version", "1.8.0_382")
83+
84+
initTelemetry.onError(new Exception("top cause", new NullPointerException("root cause")))
85+
initTelemetry.finish()
86+
87+
then:
88+
capture.json() == '{"metadata":{"runtime_name":"java","runtime_version":"1.8.0_382"},"points":[{"name":"library_entrypoint.error","tags":["error_type:java.lang.Exception"]},{"name":"library_entrypoint.error","tags":["error_type:java.lang.NullPointerException"]},{"name":"library_entrypoint.complete"}]}'
89+
}
90+
7991
static class Capture implements BootstrapInitializationTelemetry.JsonSender {
8092
String json
8193

0 commit comments

Comments
 (0)