Improve smoke tests around exceptions (#3899)

This commit is contained in:
Trask Stalnaker 2024-10-09 20:20:58 -07:00 коммит произвёл GitHub
Родитель 06935bdbe9
Коммит d428e7e081
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
4 изменённых файлов: 82 добавлений и 0 удалений

Просмотреть файл

@ -10,8 +10,11 @@ import static org.assertj.core.data.MapEntry.entry;
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 @@ class SpringBootControllerSpansEnabledTest {
RequestData rd = testing.getTelemetryDataForType(0, "RequestData");
RemoteDependencyData rdd1 =
(RemoteDependencyData) ((Data<?>) rddEnvelope1.getData()).getBaseData();
ExceptionData ed = (ExceptionData) ((Data<?>) edEnvelope1.getData()).getBaseData();
List<ExceptionDetails> details = ed.getExceptions();
ExceptionDetails ex = details.get(0);
assertThat(rd.getName()).isEqualTo("GET /SpringBoot/throwsException");
assertThat(rd.getResponseCode()).isEqualTo("500");
@ -85,6 +92,17 @@ class SpringBootControllerSpansEnabledTest {
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(

Просмотреть файл

@ -17,8 +17,11 @@ import static org.assertj.core.data.MapEntry.entry;
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 @@ abstract class SpringBootTest {
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<ExceptionDetails> details = ed.getExceptions();
ExceptionDetails ex = details.get(0);
assertThat(rd.getName()).isEqualTo("GET /SpringBoot/throwsException");
assertThat(rd.getResponseCode()).isEqualTo("500");
@ -74,6 +83,17 @@ abstract class SpringBootTest {
.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");
}

Просмотреть файл

@ -22,4 +22,9 @@ public class SpringSchedulingApp extends SpringBootServletInitializer {
public void fixedRateScheduler() {
System.out.println("Hello world.");
}
@Scheduled(fixedRate = 100)
public void exceptional() {
throw new RuntimeException("exceptional");
}
}

Просмотреть файл

@ -15,7 +15,11 @@ import static org.assertj.core.api.Assertions.assertThat;
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 @@ abstract class SpringSchedulingTest {
2,
10,
TimeUnit.SECONDS);
List<Envelope> exceptionEnvelopes =
testing.mockedIngestion.getItemsEnvelopeDataType("ExceptionData");
assertThat(exceptionEnvelopes)
.anySatisfy(
envelope -> {
ExceptionData ed = (ExceptionData) ((Data<?>) envelope.getData()).getBaseData();
List<ExceptionDetails> 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<ExceptionDetails> 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)