-
Notifications
You must be signed in to change notification settings - Fork 35
Discard classes as patterns #248
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
82df229
initial change
richardelms aed4bf7
tests
richardelms 27a926f
syntax fix
richardelms 67a925b
Update features/fixtures/scenarios/src/main/java/com/bugsnag/mazerunn…
richardelms 374cd91
review changes
richardelms 96b9733
Merge branch 'discardClassesAsPatterns' of github.com:bugsnag/bugsnag…
richardelms 859b52c
remove md file
richardelms d0a8b22
convert away from blob
richardelms fd79d6d
full pattern support
richardelms d40d29c
refac
richardelms 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # discardClasses Pattern Matching | ||
|
|
||
| The `discardClasses` configuration has been enhanced to support pattern matching using wildcards. | ||
|
|
||
| ## Features | ||
|
|
||
| ### Exact Matching (Backward Compatible) | ||
| Works exactly as before for exact class names: | ||
| ```java | ||
| config.setDiscardClasses(new String[] { | ||
| "com.example.CustomException", | ||
| "java.io.IOException" | ||
| }); | ||
| ``` | ||
|
|
||
| ### Wildcard Patterns | ||
| Now supports glob-style wildcards: | ||
|
|
||
| **`*` - Matches any characters (including dots)** | ||
| ```java | ||
| config.setDiscardClasses(new String[] { | ||
| "com.example.*" // Matches all classes in com.example package | ||
| }); | ||
| // Matches: com.example.CustomException, com.example.OtherException, etc. | ||
| ``` | ||
|
|
||
| **`?` - Matches a single character** | ||
| ```java | ||
| config.setDiscardClasses(new String[] { | ||
| "com.example.Exception?" | ||
| }); | ||
| // Matches: com.example.Exception1, com.example.ExceptionX | ||
| // Does not match: com.example.Exception, com.example.Exception12 | ||
| ``` | ||
|
|
||
| ### Combined Patterns | ||
| Mix exact matches and wildcards: | ||
| ```java | ||
| config.setDiscardClasses(new String[] { | ||
| "java.io.*", // All java.io exceptions | ||
| "com.*.CustomException", // CustomException in any com.* package | ||
| "org.example.SpecificException" // Exact match | ||
| }); | ||
| ``` | ||
|
|
||
| ## Appender Compatibility | ||
|
|
||
| The BugsnagAppender continues to work exactly as before. When setting discard classes through the appender: | ||
|
|
||
| ```xml | ||
| <appender name="BUGSNAG" class="com.bugsnag.BugsnagAppender"> | ||
| <discardClasses>com.example.*,java.io.IOException</discardClasses> | ||
| </appender> | ||
| ``` | ||
|
|
||
| Or programmatically: | ||
| ```java | ||
| appender.setDiscardClass("com.example.*"); | ||
| appender.setDiscardClass("java.io.IOException"); | ||
| ``` | ||
|
|
||
| The appender internally converts these to the Configuration's pattern set format. | ||
|
|
||
| ## Implementation Details | ||
|
|
||
| - Patterns without wildcards are treated as exact matches (using `Pattern.quote()` internally) | ||
| - Special regex characters are properly escaped | ||
| - The `getDiscardClasses()` method returns the original pattern strings (not compiled regex patterns) | ||
| - Pattern matching is case-sensitive | ||
| - Empty or null patterns are safely ignored |
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
114 changes: 114 additions & 0 deletions
114
bugsnag/src/test/java/com/bugsnag/ConfigurationDiscardClassesTest.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,114 @@ | ||
| package com.bugsnag; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| /** | ||
| * Test for Configuration.shouldIgnoreClass with pattern matching | ||
| */ | ||
| public class ConfigurationDiscardClassesTest { | ||
|
|
||
| private Configuration config; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| config = new Configuration("test-api-key"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExactMatch() { | ||
| config.setDiscardClasses(new String[] {"com.example.CustomException"}); | ||
|
|
||
| assertTrue(config.shouldIgnoreClass("com.example.CustomException")); | ||
| assertFalse(config.shouldIgnoreClass("com.example.OtherException")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWildcardMatch() { | ||
| config.setDiscardClasses(new String[] {"com.example.*"}); | ||
|
|
||
| assertTrue(config.shouldIgnoreClass("com.example.CustomException")); | ||
| assertTrue(config.shouldIgnoreClass("com.example.OtherException")); | ||
| assertTrue(config.shouldIgnoreClass("com.example.")); | ||
| assertFalse(config.shouldIgnoreClass("com.other.Exception")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMultipleWildcards() { | ||
| config.setDiscardClasses(new String[] {"com.*.Exception"}); | ||
|
|
||
| assertTrue(config.shouldIgnoreClass("com.example.Exception")); | ||
| assertTrue(config.shouldIgnoreClass("com.other.Exception")); | ||
| assertFalse(config.shouldIgnoreClass("com.example.CustomException")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testQuestionMarkWildcard() { | ||
| config.setDiscardClasses(new String[] {"com.example.Exception?"}); | ||
|
|
||
| assertTrue(config.shouldIgnoreClass("com.example.Exception1")); | ||
| assertTrue(config.shouldIgnoreClass("com.example.ExceptionX")); | ||
| assertFalse(config.shouldIgnoreClass("com.example.Exception")); | ||
| assertFalse(config.shouldIgnoreClass("com.example.Exception12")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMultiplePatterns() { | ||
| config.setDiscardClasses(new String[] { | ||
| "java.io.*", | ||
| "com.example.CustomException", | ||
| "org.*.SpecialException" | ||
| }); | ||
|
|
||
| assertTrue(config.shouldIgnoreClass("java.io.IOException")); | ||
| assertTrue(config.shouldIgnoreClass("java.io.FileNotFoundException")); | ||
| assertTrue(config.shouldIgnoreClass("com.example.CustomException")); | ||
| assertTrue(config.shouldIgnoreClass("org.apache.SpecialException")); | ||
| assertTrue(config.shouldIgnoreClass("org.springframework.SpecialException")); | ||
| assertFalse(config.shouldIgnoreClass("com.example.OtherException")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetDiscardClassesReturnsOriginalPatterns() { | ||
| String[] patterns = new String[] {"com.example.*", "java.io.IOException"}; | ||
| config.setDiscardClasses(patterns); | ||
|
|
||
| String[] retrieved = config.getDiscardClasses(); | ||
| assertEquals(2, retrieved.length); | ||
|
|
||
| // Check that patterns are returned (not regex) | ||
| boolean hasWildcard = false; | ||
| boolean hasExact = false; | ||
| for (String pattern : retrieved) { | ||
| if (pattern.equals("com.example.*")) { | ||
| hasWildcard = true; | ||
| } | ||
| if (pattern.equals("java.io.IOException")) { | ||
| hasExact = true; | ||
| } | ||
| } | ||
| assertTrue(hasWildcard); | ||
| assertTrue(hasExact); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmptyAndNullPatterns() { | ||
| config.setDiscardClasses(new String[] {}); | ||
| assertFalse(config.shouldIgnoreClass("com.example.Exception")); | ||
|
|
||
| config.setDiscardClasses(null); | ||
| assertFalse(config.shouldIgnoreClass("com.example.Exception")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSpecialCharactersAreEscaped() { | ||
| config.setDiscardClasses(new String[] {"com.example.Exception$Inner"}); | ||
|
|
||
| assertTrue(config.shouldIgnoreClass("com.example.Exception$Inner")); | ||
| assertFalse(config.shouldIgnoreClass("com.example.ExceptionXInner")); | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
features/fixtures/logback/ignored_class_wildcard_config.xml
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,18 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <configuration> | ||
| <include resource="org/springframework/boot/logging/logback/base.xml" /> | ||
|
|
||
| <appender name="BUGSNAG" class="com.bugsnag.BugsnagAppender"> | ||
| <apiKey>a35a2a72bd230ac0aa0f52715bbdc6aa</apiKey> | ||
| <releaseStage>production</releaseStage> | ||
| <appVersion>1.0.0</appVersion> | ||
|
|
||
| <discardClass>java.lang.*</discardClass> | ||
|
|
||
| <endpoint>http://localhost:9339/notify</endpoint> | ||
| </appender> | ||
|
|
||
| <root level="INFO"> | ||
| <appender-ref ref="BUGSNAG"/> | ||
| </root> | ||
| </configuration> |
32 changes: 32 additions & 0 deletions
32
...rios/src/main/java/com/bugsnag/mazerunner/scenarios/IgnoredExceptionWildcardScenario.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,32 @@ | ||
| package com.bugsnag.mazerunner.scenarios; | ||
|
|
||
| import com.bugsnag.Bugsnag; | ||
|
|
||
| /** | ||
| * Attempts to send ignored handled exceptions using wildcard patterns to Bugsnag, | ||
| * which should not result in any operation. | ||
| */ | ||
| public class IgnoredExceptionWildcardScenario extends Scenario { | ||
|
|
||
| public IgnoredExceptionWildcardScenario(Bugsnag bugsnag) { | ||
| super(bugsnag); | ||
| } | ||
|
|
||
| @Override | ||
| public void run() { | ||
| // Use wildcard pattern to ignore all RuntimeException and its subclasses | ||
| bugsnag.setDiscardClasses("java.lang.*"); | ||
|
|
||
| // These should all be ignored due to the wildcard pattern | ||
| bugsnag.notify(new RuntimeException("Should never appear")); | ||
| bugsnag.notify(new IllegalArgumentException("Should never appear")); | ||
| bugsnag.notify(new IllegalStateException("Should never appear")); | ||
|
|
||
| // This should also be sent but will be ignored due to pattern | ||
|
richardelms marked this conversation as resolved.
Outdated
|
||
| try { | ||
| throw new NullPointerException("Should never appear"); | ||
| } catch (Exception e) { | ||
| bugsnag.notify(e); | ||
| } | ||
| } | ||
| } | ||
33 changes: 33 additions & 0 deletions
33
...rios/src/main/java/com/bugsnag/mazerunner/scenarios/MultipleWildcardPatternsScenario.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,33 @@ | ||
| package com.bugsnag.mazerunner.scenarios; | ||
|
|
||
| import com.bugsnag.Bugsnag; | ||
|
|
||
| /** | ||
| * Tests multiple wildcard patterns working together. | ||
| * Uses both * and ? wildcards along with exact matches. | ||
| */ | ||
| public class MultipleWildcardPatternsScenario extends Scenario { | ||
|
|
||
| public MultipleWildcardPatternsScenario(Bugsnag bugsnag) { | ||
| super(bugsnag); | ||
| } | ||
|
|
||
| @Override | ||
| public void run() { | ||
| // Set multiple patterns: wildcards and exact matches | ||
| bugsnag.setDiscardClasses( | ||
| "java.io.*", // All java.io exceptions | ||
| "java.lang.IllegalStateException", // Exact match | ||
| "java.lang.Illegal*" // All IllegalXException classes | ||
| ); | ||
|
|
||
| // These should all be ignored | ||
| bugsnag.notify(new java.io.IOException("Should be ignored - java.io.*")); | ||
| bugsnag.notify(new java.io.FileNotFoundException("Should be ignored - java.io.*")); | ||
| bugsnag.notify(new IllegalStateException("Should be ignored - exact match")); | ||
| bugsnag.notify(new IllegalArgumentException("Should be ignored - java.lang.Illegal*")); | ||
|
|
||
| // This should be sent (not matching any pattern) | ||
| bugsnag.notify(new RuntimeException("Should be sent")); | ||
| } | ||
| } |
Oops, something went wrong.
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.