-
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 3 commits
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
83 changes: 83 additions & 0 deletions
83
...microsoft/applicationinsights/agent/internal/classicsdk/ExceptionMessageBehaviorTest.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,83 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.microsoft.applicationinsights.agent.internal.classicsdk; | ||
|
|
||
| import com.azure.monitor.opentelemetry.autoconfigure.implementation.utils.Strings; | ||
| import org.junit.jupiter.api.Test; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| public class ExceptionMessageBehaviorTest { | ||
|
|
||
| @Test | ||
| public void testNullPointerExceptionMessageBehavior() { | ||
| // Test the exact scenario from the problem statement | ||
| NullPointerException exception = new NullPointerException(); | ||
|
|
||
| // Check the message | ||
| String message = exception.getMessage(); | ||
| assertThat(message).isNull(); | ||
|
|
||
| // Apply the TelemetryUtil logic | ||
| String exceptionMessage = message; | ||
| if (Strings.isNullOrEmpty(exceptionMessage)) { | ||
| exceptionMessage = exception.getClass().getName(); | ||
| } | ||
|
|
||
| // The final message should be the class name | ||
| assertThat(exceptionMessage).isEqualTo("java.lang.NullPointerException"); | ||
| assertThat(exceptionMessage).isNotEmpty(); | ||
| assertThat(exceptionMessage).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNullPointerExceptionWithEmptyMessage() { | ||
| // Test the scenario with empty string | ||
| NullPointerException exception = new NullPointerException(""); | ||
|
|
||
| // Check the message | ||
| String message = exception.getMessage(); | ||
| assertThat(message).isEqualTo(""); | ||
|
|
||
| // Apply the TelemetryUtil logic | ||
| String exceptionMessage = message; | ||
| if (Strings.isNullOrEmpty(exceptionMessage)) { | ||
| exceptionMessage = exception.getClass().getName(); | ||
| } | ||
|
|
||
| // The final message should be the class name | ||
| assertThat(exceptionMessage).isEqualTo("java.lang.NullPointerException"); | ||
| assertThat(exceptionMessage).isNotEmpty(); | ||
| assertThat(exceptionMessage).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRuntimeExceptionMessageBehavior() { | ||
| // Test with RuntimeException | ||
| RuntimeException exception = new RuntimeException(); | ||
|
|
||
| // Check the message | ||
| String message = exception.getMessage(); | ||
| assertThat(message).isNull(); | ||
|
|
||
| // Apply the TelemetryUtil logic | ||
| String exceptionMessage = message; | ||
| if (Strings.isNullOrEmpty(exceptionMessage)) { | ||
| exceptionMessage = exception.getClass().getName(); | ||
| } | ||
|
|
||
| // The final message should be the class name | ||
| assertThat(exceptionMessage).isEqualTo("java.lang.RuntimeException"); | ||
| assertThat(exceptionMessage).isNotEmpty(); | ||
| assertThat(exceptionMessage).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testStringIsNullOrEmptyBehavior() { | ||
| // Test the Strings.isNullOrEmpty behavior with various inputs | ||
| assertThat(Strings.isNullOrEmpty(null)).isTrue(); | ||
| assertThat(Strings.isNullOrEmpty("")).isTrue(); | ||
| assertThat(Strings.isNullOrEmpty(" ")).isFalse(); // Whitespace is not empty | ||
| assertThat(Strings.isNullOrEmpty("test")).isFalse(); | ||
| } | ||
| } |
132 changes: 132 additions & 0 deletions
132
...t/java/com/microsoft/applicationinsights/agent/internal/classicsdk/TelemetryUtilTest.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,132 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.microsoft.applicationinsights.agent.internal.classicsdk; | ||
|
|
||
| import com.azure.monitor.opentelemetry.autoconfigure.implementation.builders.ExceptionDetailBuilder; | ||
| import com.azure.monitor.opentelemetry.autoconfigure.implementation.utils.Strings; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.Test; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| public class TelemetryUtilTest { | ||
|
|
||
| @Test | ||
| public void testStringsIsNullOrEmpty() { | ||
| // Test the behavior of Strings.isNullOrEmpty with different inputs | ||
|
|
||
| // null should return true | ||
| assertThat(Strings.isNullOrEmpty(null)).isTrue(); | ||
|
|
||
| // empty string should return true | ||
| assertThat(Strings.isNullOrEmpty("")).isTrue(); | ||
|
|
||
| // string with only whitespace should return false (important!) | ||
| assertThat(Strings.isNullOrEmpty(" ")).isFalse(); | ||
|
|
||
| // non-empty string should return false | ||
| assertThat(Strings.isNullOrEmpty("test")).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExceptionMessageHandling() { | ||
| // Create different types of exceptions and test the message logic | ||
|
|
||
| // Exception with null message | ||
| NullPointerException nullMsgException = new NullPointerException(); | ||
| assertThat(nullMsgException.getMessage()).isNull(); | ||
|
|
||
| // Exception with empty message | ||
| NullPointerException emptyMsgException = new NullPointerException(""); | ||
| assertThat(emptyMsgException.getMessage()).isEqualTo(""); | ||
|
|
||
| // Exception with whitespace message | ||
| NullPointerException whitespaceMsgException = new NullPointerException(" "); | ||
| assertThat(whitespaceMsgException.getMessage()).isEqualTo(" "); | ||
|
|
||
| // Test our logic | ||
| String[] testMessages = {null, "", " ", "real message"}; | ||
| String[] expectedResults = { | ||
| "java.lang.NullPointerException", | ||
| "java.lang.NullPointerException", | ||
| " ", | ||
| "real message" | ||
| }; | ||
|
|
||
| for (int i = 0; i < testMessages.length; i++) { | ||
| String testMessage = testMessages[i]; | ||
| String expectedResult = expectedResults[i]; | ||
|
|
||
| // Simulate TelemetryUtil logic with defensive check | ||
| String exceptionMessage = testMessage; | ||
| if (Strings.isNullOrEmpty(exceptionMessage)) { | ||
| exceptionMessage = "java.lang.NullPointerException"; | ||
| } | ||
|
|
||
| // Defensive check to ensure the message is never null or empty | ||
| if (Strings.isNullOrEmpty(exceptionMessage)) { | ||
| exceptionMessage = "Unknown Exception"; | ||
| } | ||
|
|
||
| assertThat(exceptionMessage).isEqualTo(expectedResult); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testExceptionWithMessage() { | ||
| // Create an exception with a message | ||
| String testMessage = "Test exception message"; | ||
| NullPointerException exception = new NullPointerException(testMessage); | ||
|
|
||
| // Process the exception | ||
| List<ExceptionDetailBuilder> exceptions = TelemetryUtil.getExceptions(exception); | ||
|
|
||
| // Should have one exception detail | ||
| assertThat(exceptions).hasSize(1); | ||
|
|
||
| // Test the logic directly - the message should remain as original | ||
| String exceptionMessage = exception.getMessage(); | ||
| if (Strings.isNullOrEmpty(exceptionMessage)) { | ||
| exceptionMessage = exception.getClass().getName(); | ||
| } | ||
| assertThat(exceptionMessage).isEqualTo(testMessage); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExceptionWithEmptyMessage() { | ||
| // Create an exception with an empty message | ||
| NullPointerException exception = new NullPointerException(""); | ||
|
|
||
| // Process the exception | ||
| List<ExceptionDetailBuilder> exceptions = TelemetryUtil.getExceptions(exception); | ||
|
|
||
| // Should have one exception detail | ||
| assertThat(exceptions).hasSize(1); | ||
|
|
||
| // Test the logic directly - the message should be set to class name | ||
| String exceptionMessage = exception.getMessage(); | ||
| if (Strings.isNullOrEmpty(exceptionMessage)) { | ||
| exceptionMessage = exception.getClass().getName(); | ||
| } | ||
| assertThat(exceptionMessage).isEqualTo("java.lang.NullPointerException"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExceptionWithNullMessage() { | ||
| // Create an exception with null message (which is the default for most exceptions) | ||
| RuntimeException exception = new RuntimeException((String) null); | ||
|
|
||
| // Process the exception | ||
| List<ExceptionDetailBuilder> exceptions = TelemetryUtil.getExceptions(exception); | ||
|
|
||
| // Should have one exception detail | ||
| assertThat(exceptions).hasSize(1); | ||
|
|
||
| // Test the logic directly - the message should be set to class name | ||
| String exceptionMessage = exception.getMessage(); | ||
| if (Strings.isNullOrEmpty(exceptionMessage)) { | ||
| exceptionMessage = exception.getClass().getName(); | ||
| } | ||
| assertThat(exceptionMessage).isEqualTo("java.lang.RuntimeException"); | ||
| } | ||
| } |
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.