From d428e7e0811d6016859cd5974d66a7405e6ab8f1 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Wed, 9 Oct 2024 20:20:58 -0700 Subject: [PATCH] Improve smoke tests around exceptions (#3899) --- .../SpringBootControllerSpansEnabledTest.java | 18 +++++++++ .../smoketest/SpringBootTest.java | 20 ++++++++++ .../smoketestapp/SpringSchedulingApp.java | 5 +++ .../smoketest/SpringSchedulingTest.java | 39 +++++++++++++++++++ 4 files changed, 82 insertions(+) diff --git a/smoke-tests/apps/SpringBoot/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SpringBootControllerSpansEnabledTest.java b/smoke-tests/apps/SpringBoot/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SpringBootControllerSpansEnabledTest.java index 2aacb1681de..1a4ea537690 100644 --- a/smoke-tests/apps/SpringBoot/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SpringBootControllerSpansEnabledTest.java +++ b/smoke-tests/apps/SpringBoot/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SpringBootControllerSpansEnabledTest.java @@ -10,8 +10,11 @@ import com.microsoft.applicationinsights.smoketest.schemav2.Data; import com.microsoft.applicationinsights.smoketest.schemav2.Envelope; import com.microsoft.applicationinsights.smoketest.schemav2.EventData; +import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionData; +import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionDetails; import com.microsoft.applicationinsights.smoketest.schemav2.RemoteDependencyData; import com.microsoft.applicationinsights.smoketest.schemav2.RequestData; +import com.microsoft.applicationinsights.smoketest.schemav2.SeverityLevel; import java.util.List; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -71,6 +74,10 @@ void testResultCodeWhenRestControllerThrows() throws Exception { RequestData rd = testing.getTelemetryDataForType(0, "RequestData"); RemoteDependencyData rdd1 = (RemoteDependencyData) ((Data) rddEnvelope1.getData()).getBaseData(); + ExceptionData ed = (ExceptionData) ((Data) edEnvelope1.getData()).getBaseData(); + + List details = ed.getExceptions(); + ExceptionDetails ex = details.get(0); assertThat(rd.getName()).isEqualTo("GET /SpringBoot/throwsException"); assertThat(rd.getResponseCode()).isEqualTo("500"); @@ -85,6 +92,17 @@ void testResultCodeWhenRestControllerThrows() throws Exception { assertThat(rdd1.getProperties()).isEmpty(); assertThat(rdd1.getSuccess()).isFalse(); + assertThat(ex.getTypeName()).isEqualTo("javax.servlet.ServletException"); + assertThat(ex.getMessage()).isEqualTo("This is an exception"); + assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR); + assertThat(ed.getProperties()) + .containsKey("Logger Message"); // specific message varies by app server + assertThat(ed.getProperties()).containsEntry("SourceType", "Logger"); + assertThat(ed.getProperties()) + .containsKey("LoggerName"); // specific logger varies by app server + assertThat(ed.getProperties()).containsKey("ThreadName"); + assertThat(ed.getProperties()).hasSize(4); + SmokeTestExtension.assertParentChild( rd, rdEnvelope, edEnvelope1, "GET /SpringBoot/throwsException"); SmokeTestExtension.assertParentChild( diff --git a/smoke-tests/apps/SpringBoot/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SpringBootTest.java b/smoke-tests/apps/SpringBoot/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SpringBootTest.java index 3f5c4c85acc..51c396ed825 100644 --- a/smoke-tests/apps/SpringBoot/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SpringBootTest.java +++ b/smoke-tests/apps/SpringBoot/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SpringBootTest.java @@ -17,8 +17,11 @@ import com.microsoft.applicationinsights.smoketest.schemav2.Data; import com.microsoft.applicationinsights.smoketest.schemav2.Envelope; import com.microsoft.applicationinsights.smoketest.schemav2.EventData; +import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionData; +import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionDetails; import com.microsoft.applicationinsights.smoketest.schemav2.RemoteDependencyData; import com.microsoft.applicationinsights.smoketest.schemav2.RequestData; +import com.microsoft.applicationinsights.smoketest.schemav2.SeverityLevel; import java.util.List; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -63,10 +66,16 @@ void testResultCodeWhenRestControllerThrows() throws Exception { Envelope edEnvelope1 = exceptions.get(0); + // assert on edEnvelope1 + assertThat(rdEnvelope.getSampleRate()).isNull(); assertThat(edEnvelope1.getSampleRate()).isNull(); RequestData rd = testing.getTelemetryDataForType(0, "RequestData"); + ExceptionData ed = (ExceptionData) ((Data) edEnvelope1.getData()).getBaseData(); + + List details = ed.getExceptions(); + ExceptionDetails ex = details.get(0); assertThat(rd.getName()).isEqualTo("GET /SpringBoot/throwsException"); assertThat(rd.getResponseCode()).isEqualTo("500"); @@ -74,6 +83,17 @@ void testResultCodeWhenRestControllerThrows() throws Exception { .containsExactly(entry("_MS.ProcessedByMetricExtractors", "True")); assertThat(rd.getSuccess()).isFalse(); + assertThat(ex.getTypeName()).isEqualTo("javax.servlet.ServletException"); + assertThat(ex.getMessage()).isEqualTo("This is an exception"); + assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR); + assertThat(ed.getProperties()) + .containsKey("Logger Message"); // specific message varies by app server + assertThat(ed.getProperties()).containsEntry("SourceType", "Logger"); + assertThat(ed.getProperties()) + .containsKey("LoggerName"); // specific logger varies by app server + assertThat(ed.getProperties()).containsKey("ThreadName"); + assertThat(ed.getProperties()).hasSize(4); + SmokeTestExtension.assertParentChild( rd, rdEnvelope, edEnvelope1, "GET /SpringBoot/throwsException"); } diff --git a/smoke-tests/apps/SpringScheduling/src/main/java/com/microsoft/applicationinsights/smoketestapp/SpringSchedulingApp.java b/smoke-tests/apps/SpringScheduling/src/main/java/com/microsoft/applicationinsights/smoketestapp/SpringSchedulingApp.java index 5606ee9a293..a92eec9d817 100644 --- a/smoke-tests/apps/SpringScheduling/src/main/java/com/microsoft/applicationinsights/smoketestapp/SpringSchedulingApp.java +++ b/smoke-tests/apps/SpringScheduling/src/main/java/com/microsoft/applicationinsights/smoketestapp/SpringSchedulingApp.java @@ -22,4 +22,9 @@ protected SpringApplicationBuilder configure(SpringApplicationBuilder applicatio public void fixedRateScheduler() { System.out.println("Hello world."); } + + @Scheduled(fixedRate = 100) + public void exceptional() { + throw new RuntimeException("exceptional"); + } } diff --git a/smoke-tests/apps/SpringScheduling/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SpringSchedulingTest.java b/smoke-tests/apps/SpringScheduling/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SpringSchedulingTest.java index 8b74aee9204..1417dd861c4 100644 --- a/smoke-tests/apps/SpringScheduling/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SpringSchedulingTest.java +++ b/smoke-tests/apps/SpringScheduling/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SpringSchedulingTest.java @@ -15,7 +15,11 @@ import com.microsoft.applicationinsights.smoketest.schemav2.Data; import com.microsoft.applicationinsights.smoketest.schemav2.Envelope; +import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionData; +import com.microsoft.applicationinsights.smoketest.schemav2.ExceptionDetails; import com.microsoft.applicationinsights.smoketest.schemav2.RequestData; +import com.microsoft.applicationinsights.smoketest.schemav2.SeverityLevel; +import java.util.List; import java.util.concurrent.TimeUnit; import java.util.function.Predicate; import org.junit.jupiter.api.Test; @@ -68,6 +72,41 @@ public boolean test(Envelope input) { 2, 10, TimeUnit.SECONDS); + + List exceptionEnvelopes = + testing.mockedIngestion.getItemsEnvelopeDataType("ExceptionData"); + + assertThat(exceptionEnvelopes) + .anySatisfy( + envelope -> { + ExceptionData ed = (ExceptionData) ((Data) envelope.getData()).getBaseData(); + List details = ed.getExceptions(); + ExceptionDetails ex = details.get(0); + assertThat(ex.getTypeName()).isEqualTo("java.lang.RuntimeException"); + assertThat(ex.getMessage()).isEqualTo("exceptional"); + assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR); + assertThat(ed.getProperties()) + .containsEntry("Logger Message", "Unexpected error occurred in scheduled task"); + assertThat(ed.getProperties()).containsEntry("SourceType", "Logger"); + assertThat(ed.getProperties()) + .containsEntry( + "LoggerName", + "org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler"); + assertThat(ed.getProperties()).containsKey("ThreadName"); + assertThat(ed.getProperties()).hasSize(4); + }); + + assertThat(exceptionEnvelopes) + .anySatisfy( + envelope -> { + ExceptionData ed = (ExceptionData) ((Data) envelope.getData()).getBaseData(); + List details = ed.getExceptions(); + ExceptionDetails ex = details.get(0); + assertThat(ex.getTypeName()).isEqualTo("java.lang.RuntimeException"); + assertThat(ex.getMessage()).isEqualTo("exceptional"); + assertThat(ed.getSeverityLevel()).isNull(); + assertThat(ed.getProperties()).isEmpty(); + }); } @Environment(TOMCAT_8_JAVA_8)