Skip to content

Commit

Permalink
Fix message for snapshots with evaluation errors (#7653)
Browse files Browse the repository at this point in the history
set the message in snapshot with the content of the first evaluation
errors
  • Loading branch information
jpbempel authored and shatzi committed Sep 25, 2024
1 parent 3f73dee commit fa39382
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -575,24 +576,33 @@ protected boolean fillSnapshot(
shouldCommit = true;
}
if (entryStatus.shouldReportError()) {
if (entryContext.getCapturedThrowable() != null) {
// report also uncaught exception
snapshot.setEntry(entryContext);
}
snapshot.addEvaluationErrors(entryStatus.getErrors());
populateErrors(entryContext, snapshot, entryStatus, snapshot::setEntry);
shouldCommit = true;
}
if (exitStatus.shouldReportError()) {
if (exitContext.getCapturedThrowable() != null) {
// report also uncaught exception
snapshot.setExit(exitContext);
}
snapshot.addEvaluationErrors(exitStatus.getErrors());
populateErrors(exitContext, snapshot, exitStatus, snapshot::setExit);
shouldCommit = true;
}
return shouldCommit;
}

private static void populateErrors(
CapturedContext context,
Snapshot snapshot,
LogStatus status,
Consumer<CapturedContext> contextSetter) {
if (context.getCapturedThrowable() != null) {
// report also uncaught exception
contextSetter.accept(context);
}
snapshot.addEvaluationErrors(status.getErrors());
if (status.getMessage() != null) {
snapshot.setMessage(status.getMessage());
} else if (!status.getErrors().isEmpty()) {
snapshot.setMessage(status.getErrors().get(0).getMessage());
}
}

private LogStatus convertStatus(CapturedContext.Status status) {
if (status == CapturedContext.Status.EMPTY_STATUS) {
return LogStatus.EMPTY_LOG_STATUS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1147,11 +1147,12 @@ public void nullCondition() throws IOException, URISyntaxException {
TestSnapshotListener listener = installProbes(CLASS_NAME, logProbes);
Class<?> testClass = compileAndLoadClass(CLASS_NAME);
int result = Reflect.onClass(testClass).call("main", "1").get();
assertEquals(1, listener.snapshots.size());
List<EvaluationError> evaluationErrors = listener.snapshots.get(0).getEvaluationErrors();
Assertions.assertEquals(1, evaluationErrors.size());
Assertions.assertEquals("nullTyped.fld.fld", evaluationErrors.get(0).getExpr());
Assertions.assertEquals("Cannot dereference field: fld", evaluationErrors.get(0).getMessage());
Snapshot snapshot = assertOneSnapshot(listener);
List<EvaluationError> evaluationErrors = snapshot.getEvaluationErrors();
assertEquals(1, evaluationErrors.size());
assertEquals("nullTyped.fld.fld", evaluationErrors.get(0).getExpr());
assertEquals("Cannot dereference field: fld", evaluationErrors.get(0).getMessage());
assertEquals("Cannot dereference field: fld", snapshot.getMessage());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.datadog.debugger.sink.Snapshot;
import datadog.trace.bootstrap.debugger.CapturedContext;
import datadog.trace.bootstrap.debugger.EvaluationError;
import datadog.trace.bootstrap.debugger.MethodLocation;
import datadog.trace.bootstrap.debugger.ProbeId;
import java.util.stream.Stream;
Expand Down Expand Up @@ -134,12 +135,35 @@ private static Stream<Arguments> statusValues() {
public void fillSnapshot_shouldSend_exit() {
LogProbe logProbe = createLog(null).evaluateAt(MethodLocation.EXIT).build();
CapturedContext entryContext = new CapturedContext();
entryContext.evaluate(PROBE_ID.getEncodedId(), logProbe, "", 0, MethodLocation.ENTRY);
entryContext.getStatus(PROBE_ID.getEncodedId());
prepareContext(entryContext, logProbe, MethodLocation.ENTRY);
CapturedContext exitContext = new CapturedContext();
prepareContext(exitContext, logProbe, MethodLocation.EXIT);
Snapshot snapshot = new Snapshot(Thread.currentThread(), logProbe, 10);
assertTrue(logProbe.fillSnapshot(entryContext, exitContext, null, snapshot));
}

@Test
public void fillSnapshot_shouldSend_evalErrors() {
LogProbe logProbe = createLog(null).evaluateAt(MethodLocation.EXIT).build();
CapturedContext entryContext = new CapturedContext();
LogProbe.LogStatus logStatus = prepareContext(entryContext, logProbe, MethodLocation.ENTRY);
logStatus.addError(new EvaluationError("expr", "msg1"));
logStatus.setLogTemplateErrors(true);
entryContext.addThrowable(new RuntimeException("errorEntry"));
CapturedContext exitContext = new CapturedContext();
exitContext.evaluate(PROBE_ID.getEncodedId(), logProbe, "", 0, MethodLocation.EXIT);
logStatus = prepareContext(exitContext, logProbe, MethodLocation.EXIT);
logStatus.addError(new EvaluationError("expr", "msg2"));
logStatus.setLogTemplateErrors(true);
exitContext.addThrowable(new RuntimeException("errorExit"));
Snapshot snapshot = new Snapshot(Thread.currentThread(), logProbe, 10);
assertTrue(logProbe.fillSnapshot(entryContext, exitContext, null, snapshot));
assertEquals(2, snapshot.getEvaluationErrors().size());
assertEquals("msg1", snapshot.getEvaluationErrors().get(0).getMessage());
assertEquals("msg2", snapshot.getEvaluationErrors().get(1).getMessage());
assertEquals(
"errorEntry", snapshot.getCaptures().getEntry().getCapturedThrowable().getMessage());
assertEquals(
"errorExit", snapshot.getCaptures().getReturn().getCapturedThrowable().getMessage());
}

private LogProbe.Builder createLog(String template) {
Expand Down

0 comments on commit fa39382

Please sign in to comment.