-
Notifications
You must be signed in to change notification settings - Fork 208
Fix exception message handling to prevent empty message field in Application Insights telemetry #4310
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
Closed
Closed
Fix exception message handling to prevent empty message field in Application Insights telemetry #4310
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
25b8fbd
Initial plan
Copilot 38f3fff
Added comprehensive tests for exception message handling
Copilot 03b720e
Fix exception message handling to prevent empty message field
Copilot 879c66b
Remove redundant defensive check for exception class names
Copilot 05b825c
Add smoke test for exception message handling
Copilot 881b9cc
Add endpoints that throw exceptions to smoke test for exception messa…
Copilot 2900e5d
Add tests for exception throwing endpoints in smoke test
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
smoke-tests/apps/ExceptionMessageHandling/build.gradle.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| plugins { | ||
| id("ai.smoke-test-war") | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation("org.springframework.boot:spring-boot-starter-web:2.5.12") { | ||
| exclude("org.springframework.boot", "spring-boot-starter-tomcat") | ||
| } | ||
| // this dependency is needed to make wildfly happy | ||
| implementation("org.reactivestreams:reactive-streams:1.0.3") | ||
| } |
16 changes: 16 additions & 0 deletions
16
...eHandling/src/main/java/com/microsoft/applicationinsights/smoketestapp/SpringBootApp.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.microsoft.applicationinsights.smoketestapp; | ||
|
|
||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
| import org.springframework.boot.builder.SpringApplicationBuilder; | ||
| import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; | ||
|
|
||
| @SpringBootApplication | ||
| public class SpringBootApp extends SpringBootServletInitializer { | ||
| @Override | ||
| protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) { | ||
| return applicationBuilder.sources(SpringBootApp.class); | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
...Handling/src/main/java/com/microsoft/applicationinsights/smoketestapp/TestController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.microsoft.applicationinsights.smoketestapp; | ||
|
|
||
| import javax.servlet.http.HttpServletResponse; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| public class TestController { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger("smoketestapp"); | ||
|
|
||
| @GetMapping("/") | ||
| public String root() { | ||
| return "OK"; | ||
| } | ||
|
|
||
| @GetMapping("/testExceptionWithoutMessage") | ||
| public String testExceptionWithoutMessage(HttpServletResponse response) { | ||
| // This reproduces the original issue: exceptions without messages | ||
| // that would cause 206 errors from Application Insights service | ||
| logger.error("Exception without message test", new NullPointerException()); | ||
| return "Exception logged"; | ||
| } | ||
|
|
||
| @GetMapping("/testExceptionWithEmptyMessage") | ||
| public String testExceptionWithEmptyMessage(HttpServletResponse response) { | ||
| // Test exception with empty message | ||
| logger.error("Exception with empty message test", new RuntimeException("")); | ||
| return "Exception logged"; | ||
| } | ||
|
|
||
| @GetMapping("/testExceptionWithWhitespaceMessage") | ||
| public String testExceptionWithWhitespaceMessage(HttpServletResponse response) { | ||
| // Test exception with whitespace-only message | ||
| logger.error("Exception with whitespace message test", new IllegalArgumentException(" ")); | ||
| return "Exception logged"; | ||
| } | ||
| } |
100 changes: 100 additions & 0 deletions
100
...keTest/java/com/microsoft/applicationinsights/smoketest/ExceptionMessageHandlingTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.microsoft.applicationinsights.smoketest; | ||
|
|
||
| import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_11; | ||
| import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_17; | ||
| import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_21; | ||
| import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8; | ||
| 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.SeverityLevel; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| @UseAgent | ||
| abstract class ExceptionMessageHandlingTest { | ||
|
|
||
| @RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create(); | ||
|
|
||
| @Test | ||
| @TargetUri("/testExceptionWithoutMessage") | ||
| void testExceptionWithoutMessage() throws Exception { | ||
| List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1); | ||
|
|
||
| Envelope rdEnvelope = rdList.get(0); | ||
| String operationId = rdEnvelope.getTags().get("ai.operation.id"); | ||
| List<Envelope> edList = | ||
| testing.mockedIngestion.waitForItemsInOperation("ExceptionData", 1, operationId); | ||
|
|
||
| Envelope edEnvelope = edList.get(0); | ||
| ExceptionData ed = (ExceptionData) ((Data<?>) edEnvelope.getData()).getBaseData(); | ||
|
|
||
| // Verify that exceptions without messages have their class name as the message | ||
| // This prevents the 206 error: "Field 'message' on type 'ExceptionDetails' is required but | ||
| // missing or empty" | ||
| assertThat(ed.getExceptions().get(0).getTypeName()).isEqualTo("java.lang.NullPointerException"); | ||
| assertThat(ed.getExceptions().get(0).getMessage()).isEqualTo("java.lang.NullPointerException"); | ||
| assertThat(ed.getExceptions().get(0).getMessage()).isNotEmpty(); | ||
| assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR); | ||
| } | ||
|
|
||
| @Test | ||
| @TargetUri("/testExceptionWithEmptyMessage") | ||
| void testExceptionWithEmptyMessage() throws Exception { | ||
| List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1); | ||
|
|
||
| Envelope rdEnvelope = rdList.get(0); | ||
| String operationId = rdEnvelope.getTags().get("ai.operation.id"); | ||
| List<Envelope> edList = | ||
| testing.mockedIngestion.waitForItemsInOperation("ExceptionData", 1, operationId); | ||
|
|
||
| Envelope edEnvelope = edList.get(0); | ||
| ExceptionData ed = (ExceptionData) ((Data<?>) edEnvelope.getData()).getBaseData(); | ||
|
|
||
| // Verify that exceptions with empty messages have their class name as the message | ||
| assertThat(ed.getExceptions().get(0).getTypeName()).isEqualTo("java.lang.RuntimeException"); | ||
| assertThat(ed.getExceptions().get(0).getMessage()).isEqualTo("java.lang.RuntimeException"); | ||
| assertThat(ed.getExceptions().get(0).getMessage()).isNotEmpty(); | ||
| assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR); | ||
| } | ||
|
|
||
| @Test | ||
| @TargetUri("/testExceptionWithWhitespaceMessage") | ||
| void testExceptionWithWhitespaceMessage() throws Exception { | ||
| List<Envelope> rdList = testing.mockedIngestion.waitForItems("RequestData", 1); | ||
|
|
||
| Envelope rdEnvelope = rdList.get(0); | ||
| String operationId = rdEnvelope.getTags().get("ai.operation.id"); | ||
| List<Envelope> edList = | ||
| testing.mockedIngestion.waitForItemsInOperation("ExceptionData", 1, operationId); | ||
|
|
||
| Envelope edEnvelope = edList.get(0); | ||
| ExceptionData ed = (ExceptionData) ((Data<?>) edEnvelope.getData()).getBaseData(); | ||
|
|
||
| // Verify that exceptions with whitespace-only messages have their class name as the message | ||
| assertThat(ed.getExceptions().get(0).getTypeName()) | ||
| .isEqualTo("java.lang.IllegalArgumentException"); | ||
| assertThat(ed.getExceptions().get(0).getMessage()) | ||
| .isEqualTo("java.lang.IllegalArgumentException"); | ||
| assertThat(ed.getExceptions().get(0).getMessage()).isNotEmpty(); | ||
| assertThat(ed.getSeverityLevel()).isEqualTo(SeverityLevel.ERROR); | ||
| } | ||
|
|
||
| @Environment(TOMCAT_8_JAVA_8) | ||
| static class Tomcat8Java8Test extends ExceptionMessageHandlingTest {} | ||
|
|
||
| @Environment(TOMCAT_8_JAVA_11) | ||
| static class Tomcat8Java11Test extends ExceptionMessageHandlingTest {} | ||
|
|
||
| @Environment(TOMCAT_8_JAVA_17) | ||
| static class Tomcat8Java17Test extends ExceptionMessageHandlingTest {} | ||
|
|
||
| @Environment(TOMCAT_8_JAVA_21) | ||
| static class Tomcat8Java21Test extends ExceptionMessageHandlingTest {} | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.