Skip to content

Commit

Permalink
Merge pull request #6191 from DataDog/jpbempel/exceptions-smoke-tests
Browse files Browse the repository at this point in the history
Add caught/uncaught exception smoke tests
  • Loading branch information
jpbempel authored Nov 10, 2023
2 parents f15f4b4 + fce52e8 commit d7a0af0
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ static void run(String[] args) throws Exception {
System.out.println("Waiting for instrumentation...");
waitForInstrumentation(LOG_FILENAME, Main.class.getName());
System.out.println("Executing method: " + methodName);
method.accept(args.length > 2 ? args[2] : null);
System.out.println("Executed");
waitForUpload(LOG_FILENAME, expectedUploads);
System.out.println("Exiting...");
try {
method.accept(args.length > 2 ? args[2] : null);
System.out.println("Executed");
} finally {
waitForUpload(LOG_FILENAME, expectedUploads);
System.out.println("Exiting...");
}
}

private static void registerMethods() {
Expand All @@ -47,6 +50,7 @@ private static void registerMethods() {
methodsByName.put("fullMethod", Main::runFullMethod);
methodsByName.put("multiProbesFullMethod", Main::runFullMethod);
methodsByName.put("loopingFullMethod", Main::runLoopingFullMethod);
methodsByName.put("exceptionMethod", Main::runExceptionMethod);
}

private static void emptyMethod(String arg) {}
Expand Down Expand Up @@ -74,6 +78,10 @@ private static void runLoopingFullMethod(String arg) {
}
}

private static void runExceptionMethod(String s) {
exceptionMethod(s);
}

private static String fullMethod(
int argInt, String argStr, double argDouble, Map<String, String> argMap, String... argVar) {
try {
Expand All @@ -92,4 +100,17 @@ private static String fullMethod(
return null;
}
}

private static void exceptionMethod(String arg) {
if (arg.equals("uncaught")) {
throw new RuntimeException("oops uncaught!");
}
if (arg.equals("caught")) {
try {
throw new IllegalArgumentException("oops caught!");
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,71 @@ void testSamplingLogCustom() throws Exception {
assertTrue(countSnapshots() < 120);
}

@Test
@DisplayName("testUncaughtException")
void testUncaughtException() throws Exception {
final String EXPECTED_UPLOADS = "3";
final String METHOD_NAME = "exceptionMethod";
LogProbe probe =
LogProbe.builder()
.probeId(PROBE_ID)
.where(MAIN_CLASS_NAME, METHOD_NAME)
.evaluateAt(MethodLocation.EXIT)
.captureSnapshot(true)
.build();
setCurrentConfiguration(createConfig(probe));
targetProcess =
createProcessBuilder(logFilePath, METHOD_NAME, EXPECTED_UPLOADS, "uncaught").start();
RecordedRequest request = retrieveSnapshotRequest();
assertNotNull(request);
assertFalse(logHasErrors(logFilePath, it -> false));
String bodyStr = request.getBody().readUtf8();
JsonAdapter<List<JsonSnapshotSerializer.IntakeRequest>> adapter = createAdapterForSnapshot();
System.out.println(bodyStr);
JsonSnapshotSerializer.IntakeRequest intakeRequest = adapter.fromJson(bodyStr).get(0);
Snapshot snapshot = intakeRequest.getDebugger().getSnapshot();
assertEquals("123356536", snapshot.getProbe().getId());
CapturedContext.CapturedThrowable throwable = snapshot.getCaptures().getReturn().getThrowable();
assertEquals("oops uncaught!", throwable.getMessage());
assertTrue(throwable.getStacktrace().size() > 0);
assertEquals(
"datadog.smoketest.debugger.Main.exceptionMethod",
throwable.getStacktrace().get(0).getFunction());
}

@Test
@DisplayName("testCaughtException")
void testCaughtException() throws Exception {
final String EXPECTED_UPLOADS = "3";
final String METHOD_NAME = "exceptionMethod";
LogProbe probe =
LogProbe.builder()
.probeId(PROBE_ID)
.where(MAIN_CLASS_NAME, METHOD_NAME)
.evaluateAt(MethodLocation.EXIT)
.captureSnapshot(true)
.build();
setCurrentConfiguration(createConfig(probe));
targetProcess =
createProcessBuilder(logFilePath, METHOD_NAME, EXPECTED_UPLOADS, "caught").start();
RecordedRequest request = retrieveSnapshotRequest();
assertNotNull(request);
assertFalse(logHasErrors(logFilePath, it -> false));
String bodyStr = request.getBody().readUtf8();
JsonAdapter<List<JsonSnapshotSerializer.IntakeRequest>> adapter = createAdapterForSnapshot();
System.out.println(bodyStr);
JsonSnapshotSerializer.IntakeRequest intakeRequest = adapter.fromJson(bodyStr).get(0);
Snapshot snapshot = intakeRequest.getDebugger().getSnapshot();
assertEquals("123356536", snapshot.getProbe().getId());
assertEquals(1, snapshot.getCaptures().getCaughtExceptions().size());
CapturedContext.CapturedThrowable throwable =
snapshot.getCaptures().getCaughtExceptions().get(0);
assertEquals("oops caught!", throwable.getMessage());
assertEquals(
"datadog.smoketest.debugger.Main.exceptionMethod",
throwable.getStacktrace().get(0).getFunction());
}

private int countSnapshots() throws Exception {
int snapshotCount = 0;
RecordedRequest request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ private void doLineMetric(
MetricProbe metricProbe =
MetricProbe.builder()
.probeId(PROBE_ID)
.where("DebuggerTestApplication.java", 80)
// on line: System.out.println("fullMethod");
.where("DebuggerTestApplication.java", 88)
.kind(kind)
.metricName(metricName)
.valueScript(script)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void setup(TestInfo testInfo) throws Exception {
controlServer = new MockWebServer();
// controlServer.setDispatcher(new ControlDispatcher());
controlServer.start();
LOG.info("ControlServer on {}", controlServer.getPort());
controlUrl = controlServer.url(CONTROL_URL);
startApp();
appUrl = waitForAppStartedAndGetUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ void testLineRangeSpan() throws Exception {
final String METHOD_NAME = "fullMethod";
final String EXPECTED_UPLOADS = "3"; // 2 + 1 for letting the trace being sent (async)
SpanProbe spanProbe =
SpanProbe.builder().probeId(PROBE_ID).where(MAIN_CLASS_NAME, 80, 89).build();
SpanProbe.builder()
.probeId(PROBE_ID)
// from line: System.out.println("fullMethod");
// to line: + String.join(",", argVar);
.where(MAIN_CLASS_NAME, 88, 97)
.build();
setCurrentConfiguration(createSpanConfig(spanProbe));
targetProcess = createProcessBuilder(logFilePath, METHOD_NAME, EXPECTED_UPLOADS).start();
DecodedSpan decodedSpan = retrieveSpanRequest(DebuggerTracer.OPERATION_NAME);
assertEquals("Main.fullMethod:L80-89", decodedSpan.getResource());
assertEquals("Main.fullMethod:L88-97", decodedSpan.getResource());
}

@Test
Expand Down

0 comments on commit d7a0af0

Please sign in to comment.