Skip to content

Commit 67342bb

Browse files
authored
Fix message for snapshots with evaluation errors (#7653)
set the message in snapshot with the content of the first evaluation errors
1 parent 27f522f commit 67342bb

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/probe/LogProbe.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.Collections;
4141
import java.util.List;
4242
import java.util.Objects;
43+
import java.util.function.Consumer;
4344
import org.slf4j.Logger;
4445
import org.slf4j.LoggerFactory;
4546

@@ -572,24 +573,33 @@ protected boolean fillSnapshot(
572573
shouldCommit = true;
573574
}
574575
if (entryStatus.shouldReportError()) {
575-
if (entryContext.getCapturedThrowable() != null) {
576-
// report also uncaught exception
577-
snapshot.setEntry(entryContext);
578-
}
579-
snapshot.addEvaluationErrors(entryStatus.getErrors());
576+
populateErrors(entryContext, snapshot, entryStatus, snapshot::setEntry);
580577
shouldCommit = true;
581578
}
582579
if (exitStatus.shouldReportError()) {
583-
if (exitContext.getCapturedThrowable() != null) {
584-
// report also uncaught exception
585-
snapshot.setExit(exitContext);
586-
}
587-
snapshot.addEvaluationErrors(exitStatus.getErrors());
580+
populateErrors(exitContext, snapshot, exitStatus, snapshot::setExit);
588581
shouldCommit = true;
589582
}
590583
return shouldCommit;
591584
}
592585

586+
private static void populateErrors(
587+
CapturedContext context,
588+
Snapshot snapshot,
589+
LogStatus status,
590+
Consumer<CapturedContext> contextSetter) {
591+
if (context.getCapturedThrowable() != null) {
592+
// report also uncaught exception
593+
contextSetter.accept(context);
594+
}
595+
snapshot.addEvaluationErrors(status.getErrors());
596+
if (status.getMessage() != null) {
597+
snapshot.setMessage(status.getMessage());
598+
} else if (!status.getErrors().isEmpty()) {
599+
snapshot.setMessage(status.getErrors().get(0).getMessage());
600+
}
601+
}
602+
593603
private LogStatus convertStatus(CapturedContext.Status status) {
594604
if (status == CapturedContext.Status.EMPTY_STATUS) {
595605
return LogStatus.EMPTY_LOG_STATUS;

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/agent/CapturedSnapshotTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,11 +1147,12 @@ public void nullCondition() throws IOException, URISyntaxException {
11471147
TestSnapshotListener listener = installProbes(CLASS_NAME, logProbes);
11481148
Class<?> testClass = compileAndLoadClass(CLASS_NAME);
11491149
int result = Reflect.onClass(testClass).call("main", "1").get();
1150-
assertEquals(1, listener.snapshots.size());
1151-
List<EvaluationError> evaluationErrors = listener.snapshots.get(0).getEvaluationErrors();
1152-
Assertions.assertEquals(1, evaluationErrors.size());
1153-
Assertions.assertEquals("nullTyped.fld.fld", evaluationErrors.get(0).getExpr());
1154-
Assertions.assertEquals("Cannot dereference field: fld", evaluationErrors.get(0).getMessage());
1150+
Snapshot snapshot = assertOneSnapshot(listener);
1151+
List<EvaluationError> evaluationErrors = snapshot.getEvaluationErrors();
1152+
assertEquals(1, evaluationErrors.size());
1153+
assertEquals("nullTyped.fld.fld", evaluationErrors.get(0).getExpr());
1154+
assertEquals("Cannot dereference field: fld", evaluationErrors.get(0).getMessage());
1155+
assertEquals("Cannot dereference field: fld", snapshot.getMessage());
11551156
}
11561157

11571158
@Test

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/probe/LogProbeTest.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.datadog.debugger.sink.Snapshot;
99
import datadog.trace.bootstrap.debugger.CapturedContext;
10+
import datadog.trace.bootstrap.debugger.EvaluationError;
1011
import datadog.trace.bootstrap.debugger.MethodLocation;
1112
import datadog.trace.bootstrap.debugger.ProbeId;
1213
import java.util.stream.Stream;
@@ -134,12 +135,35 @@ private static Stream<Arguments> statusValues() {
134135
public void fillSnapshot_shouldSend_exit() {
135136
LogProbe logProbe = createLog(null).evaluateAt(MethodLocation.EXIT).build();
136137
CapturedContext entryContext = new CapturedContext();
137-
entryContext.evaluate(PROBE_ID.getEncodedId(), logProbe, "", 0, MethodLocation.ENTRY);
138-
entryContext.getStatus(PROBE_ID.getEncodedId());
138+
prepareContext(entryContext, logProbe, MethodLocation.ENTRY);
139+
CapturedContext exitContext = new CapturedContext();
140+
prepareContext(exitContext, logProbe, MethodLocation.EXIT);
141+
Snapshot snapshot = new Snapshot(Thread.currentThread(), logProbe, 10);
142+
assertTrue(logProbe.fillSnapshot(entryContext, exitContext, null, snapshot));
143+
}
144+
145+
@Test
146+
public void fillSnapshot_shouldSend_evalErrors() {
147+
LogProbe logProbe = createLog(null).evaluateAt(MethodLocation.EXIT).build();
148+
CapturedContext entryContext = new CapturedContext();
149+
LogProbe.LogStatus logStatus = prepareContext(entryContext, logProbe, MethodLocation.ENTRY);
150+
logStatus.addError(new EvaluationError("expr", "msg1"));
151+
logStatus.setLogTemplateErrors(true);
152+
entryContext.addThrowable(new RuntimeException("errorEntry"));
139153
CapturedContext exitContext = new CapturedContext();
140-
exitContext.evaluate(PROBE_ID.getEncodedId(), logProbe, "", 0, MethodLocation.EXIT);
154+
logStatus = prepareContext(exitContext, logProbe, MethodLocation.EXIT);
155+
logStatus.addError(new EvaluationError("expr", "msg2"));
156+
logStatus.setLogTemplateErrors(true);
157+
exitContext.addThrowable(new RuntimeException("errorExit"));
141158
Snapshot snapshot = new Snapshot(Thread.currentThread(), logProbe, 10);
142159
assertTrue(logProbe.fillSnapshot(entryContext, exitContext, null, snapshot));
160+
assertEquals(2, snapshot.getEvaluationErrors().size());
161+
assertEquals("msg1", snapshot.getEvaluationErrors().get(0).getMessage());
162+
assertEquals("msg2", snapshot.getEvaluationErrors().get(1).getMessage());
163+
assertEquals(
164+
"errorEntry", snapshot.getCaptures().getEntry().getCapturedThrowable().getMessage());
165+
assertEquals(
166+
"errorExit", snapshot.getCaptures().getReturn().getCapturedThrowable().getMessage());
143167
}
144168

145169
private LogProbe.Builder createLog(String template) {

0 commit comments

Comments
 (0)