Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add caught/uncaught exception smoke tests #6191

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -262,6 +262,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 @@ -146,7 +146,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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - would be cool if we could have a utility function that read the ".java" file (can we put it as a project resource?) and find the line number based on a regex.

from = Util.findLineInFile('DebuggerTestApplicaiton.java', RegEx('System.out.println("fullMethod")'))

.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
Loading