From 0afa1985edf38d33fb55926e6761f307c22cb0db Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Mon, 8 Jul 2024 15:27:12 +0200 Subject: [PATCH 1/7] Implement ignoredExceptionsForType --- dart/lib/src/sentry_client.dart | 10 +++++++++ dart/lib/src/sentry_options.dart | 16 +++++++++++++++ dart/test/sentry_client_test.dart | 34 +++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/dart/lib/src/sentry_client.dart b/dart/lib/src/sentry_client.dart index 63fcbfb421..9882287d6f 100644 --- a/dart/lib/src/sentry_client.dart +++ b/dart/lib/src/sentry_client.dart @@ -83,6 +83,16 @@ class SentryClient { dynamic stackTrace, Hint? hint, }) async { + if (_options.containsIgnoredExceptionForType(event.throwable)) { + _options.logger( + SentryLevel.debug, + 'Event was dropped as the exception ${event.throwable.runtimeType.toString()} is ignored.', + ); + + _recordLostEvent(event, DiscardReason.eventProcessor); + return _sentryId; + } + if (_sampleRate()) { _recordLostEvent(event, DiscardReason.sampleRate); _options.logger( diff --git a/dart/lib/src/sentry_options.dart b/dart/lib/src/sentry_options.dart index f3f3c65a50..50da709851 100644 --- a/dart/lib/src/sentry_options.dart +++ b/dart/lib/src/sentry_options.dart @@ -334,6 +334,22 @@ class SentryOptions { _scopeObservers.add(scopeObserver); } + final List _ignoredExceptionsForType = []; + + /// Ignored exception types. + List get ignoredExceptionsForType => _ignoredExceptionsForType; + + /// Adds exception type to the list of ignored exceptions. + void addExceptionFilterForType(Type exceptionType) { + _ignoredExceptionsForType.add(exceptionType); + } + + /// Check if [ignoredExceptionsForType] contains an exception. + bool containsIgnoredExceptionForType(dynamic exception) { + return exception != null && + _ignoredExceptionsForType.contains(exception.runtimeType); + } + @internal late ClientReportRecorder recorder = NoOpClientReportRecorder(); diff --git a/dart/test/sentry_client_test.dart b/dart/test/sentry_client_test.dart index 153c1515f0..cca9a81361 100644 --- a/dart/test/sentry_client_test.dart +++ b/dart/test/sentry_client_test.dart @@ -1032,6 +1032,40 @@ void main() { }); }); + group('SentryClient ignored exceptions', () { + late Fixture fixture; + + setUp(() { + fixture = Fixture(); + }); + + test('addExceptionFilterForType drops matching error event throwable', + () async { + fixture.options.addExceptionFilterForType(ExceptionWithCause); + + final throwable = ExceptionWithCause(Error(), StackTrace.current); + final event = SentryEvent(throwable: throwable); + + final client = fixture.getSut(); + await client.captureEvent(event); + + expect((fixture.transport).called(0), true); + }); + + test('record ignored exceptions dropping event', () async { + fixture.options.addExceptionFilterForType(ExceptionWithCause); + + final throwable = ExceptionWithCause(Error(), StackTrace.current); + final event = SentryEvent(throwable: throwable); + + final client = fixture.getSut(); + await client.captureEvent(event); + + expect(fixture.recorder.reason, DiscardReason.eventProcessor); + expect(fixture.recorder.category, DataCategory.error); + }); + }); + group('SentryClient before send transaction', () { late Fixture fixture; From 726bc9720aa7ff902463ef0c5a55ee4459832907 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Mon, 8 Jul 2024 15:50:23 +0200 Subject: [PATCH 2/7] Add changelog entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6a26c1553..6a6373891b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Features + +- Support `ignoredExceptionsForType` ([#2150](https://github.com/getsentry/sentry-dart/pull/2150)) + ### Fixes - App starts hanging for 30s ([#2140](https://github.com/getsentry/sentry-dart/pull/2140)) From eab5bf4015728dfb495497a1bb39194f3e34517f Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 9 Jul 2024 10:54:02 +0200 Subject: [PATCH 3/7] update cl --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 805e61e787..27b97c69eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,9 @@ ### Features -- Support `ignoredExceptionsForType` ([#2150](https://github.com/getsentry/sentry-dart/pull/2150)) - Add memory usage to contexts ([#2133](https://github.com/getsentry/sentry-dart/pull/2133)) - Only for Linux/Windows applications, as iOS/Android/macOS use native SDKs +- Support `ignoredExceptionsForType` ([#2150](https://github.com/getsentry/sentry-dart/pull/2150)) ### Fixes From b8124d957f0a08411d831b1aee8bc50ee0140005 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 16 Jul 2024 13:49:44 +0200 Subject: [PATCH 4/7] fix changelog --- CHANGELOG.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a39c3246a..506515d91e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Features + +- Support `ignoredExceptionsForType` ([#2150](https://github.com/getsentry/sentry-dart/pull/2150)) + ## 8.4.0 ### Features @@ -14,7 +20,6 @@ - Record dropped spans in client reports ([#2154](https://github.com/getsentry/sentry-dart/pull/2154)) - Add memory usage to contexts ([#2133](https://github.com/getsentry/sentry-dart/pull/2133)) - Only for Linux/Windows applications, as iOS/Android/macOS use native SDKs -- Support `ignoredExceptionsForType` ([#2150](https://github.com/getsentry/sentry-dart/pull/2150)) ### Fixes From 587a51e34a9226aa7a77175557b31bf1b5b2ac37 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 16 Jul 2024 13:52:09 +0200 Subject: [PATCH 5/7] Rename _sentryId to _emptySentryId --- dart/lib/src/sentry_client.dart | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/dart/lib/src/sentry_client.dart b/dart/lib/src/sentry_client.dart index a7978b945b..212996c218 100644 --- a/dart/lib/src/sentry_client.dart +++ b/dart/lib/src/sentry_client.dart @@ -45,7 +45,7 @@ class SentryClient { late final MetricsAggregator? _metricsAggregator; - static final _sentryId = Future.value(SentryId.empty()); + static final _emptySentryId = Future.value(SentryId.empty()); SentryExceptionFactory get _exceptionFactory => _options.exceptionFactory; @@ -88,9 +88,9 @@ class SentryClient { SentryLevel.debug, 'Event was dropped as the exception ${event.throwable.runtimeType.toString()} is ignored.', ); - - _recordLostEvent(event, DiscardReason.eventProcessor); - return _sentryId; + _options.recorder + .recordLostEvent(DiscardReason.eventProcessor, _getCategory(event)); + return _emptySentryId; } if (_sampleRate()) { @@ -100,7 +100,7 @@ class SentryClient { SentryLevel.debug, 'Event ${event.eventId.toString()} was dropped due to sampling decision.', ); - return _sentryId; + return _emptySentryId; } SentryEvent? preparedEvent = _prepareEvent(event, stackTrace: stackTrace); @@ -116,7 +116,7 @@ class SentryClient { // dropped by scope event processors if (preparedEvent == null) { - return _sentryId; + return _emptySentryId; } preparedEvent = await _runEventProcessors( @@ -127,7 +127,7 @@ class SentryClient { // dropped by event processors if (preparedEvent == null) { - return _sentryId; + return _emptySentryId; } preparedEvent = _createUserOrSetDefaultIpAddress(preparedEvent); @@ -139,7 +139,7 @@ class SentryClient { // dropped by beforeSend if (preparedEvent == null) { - return _sentryId; + return _emptySentryId; } var attachments = List.from(scope?.attachments ?? []); @@ -336,7 +336,7 @@ class SentryClient { // dropped by scope event processors if (preparedTransaction == null) { - return _sentryId; + return _emptySentryId; } preparedTransaction = await _runEventProcessors( @@ -347,7 +347,7 @@ class SentryClient { // dropped by event processors if (preparedTransaction == null) { - return _sentryId; + return _emptySentryId; } preparedTransaction = @@ -355,7 +355,7 @@ class SentryClient { // dropped by beforeSendTransaction if (preparedTransaction == null) { - return _sentryId; + return _emptySentryId; } final attachments = scope?.attachments From ba461c08c7402f302b29d6c7a5a83a901f1b8660 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Tue, 16 Jul 2024 14:06:39 +0200 Subject: [PATCH 6/7] fix test --- dart/test/sentry_client_test.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dart/test/sentry_client_test.dart b/dart/test/sentry_client_test.dart index b2ad65f58d..5a2945a6ec 100644 --- a/dart/test/sentry_client_test.dart +++ b/dart/test/sentry_client_test.dart @@ -1062,8 +1062,10 @@ void main() { final client = fixture.getSut(); await client.captureEvent(event); - expect(fixture.recorder.reason, DiscardReason.eventProcessor); - expect(fixture.recorder.category, DataCategory.error); + expect(fixture.recorder.discardedEvents.first.reason, + DiscardReason.eventProcessor); + expect( + fixture.recorder.discardedEvents.first.category, DataCategory.error); }); }); From 94b869e2bf9652dac2a2a8b5b7ffad61358a9e53 Mon Sep 17 00:00:00 2001 From: Denis Andrasec Date: Mon, 22 Jul 2024 11:49:16 +0200 Subject: [PATCH 7/7] add usage info --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b006a2b5a0..be0b28da64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ ### Features - Support `ignoredExceptionsForType` ([#2150](https://github.com/getsentry/sentry-dart/pull/2150)) + - Filter out exception types by calling `SentryOptions.addExceptionFilterForType(Type exceptionType)` + ### Fixes - Disable sff & frame delay detection on web, linux and windows ([#2182](https://github.com/getsentry/sentry-dart/pull/2182))