-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathAbstractExceptionDebugger.java
More file actions
220 lines (203 loc) · 8.66 KB
/
AbstractExceptionDebugger.java
File metadata and controls
220 lines (203 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package com.datadog.debugger.exception;
import static com.datadog.debugger.agent.ConfigurationAcceptor.Source.EXCEPTION;
import static com.datadog.debugger.util.ExceptionHelper.createThrowableMapping;
import com.datadog.debugger.agent.ConfigurationUpdater;
import com.datadog.debugger.agent.DebuggerAgent;
import com.datadog.debugger.sink.Snapshot;
import com.datadog.debugger.util.ExceptionHelper;
import datadog.trace.bootstrap.debugger.DebuggerContext;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.Tags;
import datadog.trace.util.AgentTaskScheduler;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Abstract implementation of {@link DebuggerContext.ExceptionDebugger} that uses {@link
* ExceptionProbeManager} to instrument the exception stacktrace and send snapshots.
*/
public abstract class AbstractExceptionDebugger implements DebuggerContext.ExceptionDebugger {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractExceptionDebugger.class);
public static final String DD_DEBUG_ERROR_PREFIX = "_dd.debug.error.";
public static final String DD_DEBUG_ERROR_EXCEPTION_ID = DD_DEBUG_ERROR_PREFIX + "exception_id";
public static final String DD_DEBUG_ERROR_EXCEPTION_HASH =
DD_DEBUG_ERROR_PREFIX + "exception_hash";
public static final String SNAPSHOT_ID_TAG_FMT = DD_DEBUG_ERROR_PREFIX + "%d.snapshot_id";
private final ExceptionProbeManager exceptionProbeManager;
private final ConfigurationUpdater configurationUpdater;
private final DebuggerContext.ClassNameFilter classNameFiltering;
private final int maxCapturedFrames;
private final boolean applyConfigAsync;
AbstractExceptionDebugger(
ExceptionProbeManager exceptionProbeManager,
ConfigurationUpdater configurationUpdater,
DebuggerContext.ClassNameFilter classNameFiltering,
int maxCapturedFrames,
boolean applyConfigAsync) {
this.exceptionProbeManager = exceptionProbeManager;
this.configurationUpdater = configurationUpdater;
this.classNameFiltering = classNameFiltering;
this.maxCapturedFrames = maxCapturedFrames;
this.applyConfigAsync = applyConfigAsync;
}
protected abstract boolean shouldHandleException(Throwable t, AgentSpan span);
@Override
public void handleException(Throwable t, AgentSpan span) {
if (!shouldHandleException(t, span)) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Skip handling error: {}", t.toString());
}
return;
}
String fingerprint = Fingerprinter.fingerprint(t, classNameFiltering);
if (fingerprint == null) {
LOGGER.debug("Unable to fingerprint exception", t);
return;
}
Deque<Throwable> chainedExceptions = new ArrayDeque<>();
Throwable innerMostException = ExceptionHelper.getInnerMostThrowable(t, chainedExceptions);
if (innerMostException == null) {
LOGGER.debug("Unable to find root cause of exception");
return;
}
List<Throwable> chainedExceptionsList = new ArrayList<>(chainedExceptions);
if (exceptionProbeManager.isAlreadyInstrumented(fingerprint)) {
ExceptionProbeManager.ThrowableState state =
exceptionProbeManager.getStateByThrowable(innerMostException);
if (state == null) {
LOGGER.debug("Unable to find state for throwable: {}", innerMostException.toString());
return;
}
processSnapshotsAndSetTags(
t, span, state, chainedExceptionsList, fingerprint, maxCapturedFrames);
exceptionProbeManager.updateLastCapture(fingerprint);
} else {
// climb up the exception chain to find the first exception that has instrumented frames
Throwable throwable;
int chainedExceptionIdx = 0;
while ((throwable = chainedExceptions.pollFirst()) != null) {
ExceptionProbeManager.CreationResult creationResult =
exceptionProbeManager.createProbesForException(
throwable.getStackTrace(), chainedExceptionIdx);
if (creationResult.probesCreated > 0) {
if (!applyConfigAsync) {
applyExceptionConfiguration(fingerprint);
} else {
AgentTaskScheduler.get().execute(() -> applyExceptionConfiguration(fingerprint));
}
break;
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(
"No probe created, nativeFrames={}, thirdPartyFrames={} for exception: {}",
creationResult.nativeFrames,
creationResult.thirdPartyFrames,
ExceptionHelper.foldExceptionStackTrace(throwable));
}
}
chainedExceptionIdx++;
}
}
}
private void applyExceptionConfiguration(String fingerprint) {
configurationUpdater.accept(EXCEPTION, exceptionProbeManager.getProbes());
exceptionProbeManager.addFingerprint(fingerprint);
LOGGER.debug("Exception Fingerprint {} added", fingerprint);
}
protected void addStackFrameTags(
AgentSpan span, Snapshot snapshot, int frameIndex, StackTraceElement stackFrame) {
String tagName = String.format(SNAPSHOT_ID_TAG_FMT, frameIndex);
span.setTag(tagName, snapshot.getId());
LOGGER.debug("add tag to span[{}]: {}: {}", span.getSpanId(), tagName, snapshot.getId());
}
private void processSnapshotsAndSetTags(
Throwable t,
AgentSpan span,
ExceptionProbeManager.ThrowableState state,
List<Throwable> chainedExceptions,
String fingerprint,
int maxCapturedFrames) {
if (span.getTag(DD_DEBUG_ERROR_EXCEPTION_ID) != null) {
LOGGER.debug("Clear previous frame tags");
// already set for this span, clear the frame tags
span.getTags()
.forEach(
(k, v) -> {
if (k.startsWith(DD_DEBUG_ERROR_PREFIX)) {
span.setTag(k, (String) null);
}
});
}
boolean snapshotAssigned = false;
List<Snapshot> snapshots = state.getSnapshots();
int maxSnapshotSize = Math.min(snapshots.size(), maxCapturedFrames);
for (int i = 0; i < maxSnapshotSize; i++) {
Snapshot snapshot = snapshots.get(i);
int chainedExceptionIdx = snapshot.getChainedExceptionIdx();
if (chainedExceptionIdx >= chainedExceptions.size()) {
LOGGER.debug(
"Chained exception for snapshot={} is out of bounds: {}/{}",
snapshot.getId(),
chainedExceptionIdx,
chainedExceptions.size());
continue;
}
Throwable currentEx = chainedExceptions.get(snapshot.getChainedExceptionIdx());
int[] mapping = createThrowableMapping(currentEx, t);
StackTraceElement[] innerTrace = currentEx.getStackTrace();
int currentIdx = innerTrace.length - snapshot.getStack().size();
if (currentIdx < 0) {
// This means the innerTrace was truncated by the underlying environment.
// This is known to happen in AWS Lambda, but may also happen elsewhere.
currentIdx = i;
}
if (!sanityCheckSnapshotAssignment(snapshot, innerTrace, currentIdx)) {
continue;
}
int frameIndex = mapping[currentIdx];
if (frameIndex == -1) {
continue;
}
addStackFrameTags(span, snapshot, frameIndex, innerTrace[currentIdx]);
if (!state.isSnapshotSent()) {
DebuggerAgent.getSink().addSnapshot(snapshot);
}
snapshotAssigned = true;
}
if (snapshotAssigned) {
state.markAsSnapshotSent();
span.setTag(DD_DEBUG_ERROR_EXCEPTION_ID, state.getExceptionId());
LOGGER.debug(
"add tag to span[{}]: {}: {}",
span.getSpanId(),
DD_DEBUG_ERROR_EXCEPTION_ID,
state.getExceptionId());
span.setTag(Tags.ERROR_DEBUG_INFO_CAPTURED, true);
span.setTag(DD_DEBUG_ERROR_EXCEPTION_HASH, fingerprint);
}
}
private static boolean sanityCheckSnapshotAssignment(
Snapshot snapshot, StackTraceElement[] innerTrace, int currentIdx) {
String className = snapshot.getProbe().getLocation().getType();
String methodName = snapshot.getProbe().getLocation().getMethod();
if (currentIdx < 0 || currentIdx >= innerTrace.length) {
LOGGER.warn(
"currentIdx={} out of bounds of innerTrace array length={}",
currentIdx,
innerTrace.length);
return false;
}
if (!className.equals(innerTrace[currentIdx].getClassName())
|| !methodName.equals(innerTrace[currentIdx].getMethodName())) {
LOGGER.warn("issue when assigning snapshot to frame: {} {}", className, methodName);
return false;
}
return true;
}
public ExceptionProbeManager getExceptionProbeManager() {
return exceptionProbeManager;
}
}