From cc6ca0b067724a51bbc2c3de3122365424f48086 Mon Sep 17 00:00:00 2001 From: Qstick Date: Fri, 12 Aug 2022 23:33:27 -0500 Subject: [PATCH] Better Sentry Filtering for AggregateException children --- .../SentryTargetFixture.cs | 24 ++++++++++- .../Instrumentation/Sentry/SentryTarget.cs | 43 +++++++++++++++---- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/NzbDrone.Common.Test/InstrumentationTests/SentryTargetFixture.cs b/src/NzbDrone.Common.Test/InstrumentationTests/SentryTargetFixture.cs index 7392c3b85..dc0fbaffb 100644 --- a/src/NzbDrone.Common.Test/InstrumentationTests/SentryTargetFixture.cs +++ b/src/NzbDrone.Common.Test/InstrumentationTests/SentryTargetFixture.cs @@ -20,7 +20,21 @@ namespace NzbDrone.Common.Test.InstrumentationTests private static Exception[] FilteredExceptions = new Exception[] { - new UnauthorizedAccessException() + new UnauthorizedAccessException(), + new AggregateException(new Exception[] + { + new UnauthorizedAccessException(), + new UnauthorizedAccessException() + }) + }; + + private static Exception[] NonFilteredExceptions = new Exception[] + { + new AggregateException(new Exception[] + { + new UnauthorizedAccessException(), + new NotImplementedException() + }) }; [SetUp] @@ -63,6 +77,14 @@ namespace NzbDrone.Common.Test.InstrumentationTests _subject.IsSentryMessage(log).Should().BeFalse(); } + [Test] + [TestCaseSource("NonFilteredExceptions")] + public void should_not_filter_event_for_filtered_exception_types(Exception ex) + { + var log = GivenLogEvent(LogLevel.Error, ex, "test"); + _subject.IsSentryMessage(log).Should().BeTrue(); + } + [Test] [TestCaseSource("FilteredExceptions")] public void should_not_filter_event_for_filtered_exception_types_if_filtering_disabled(Exception ex) diff --git a/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs b/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs index b7a083136..ac8f6f522 100644 --- a/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs +++ b/src/NzbDrone.Common/Instrumentation/Sentry/SentryTarget.cs @@ -229,21 +229,48 @@ namespace NzbDrone.Common.Instrumentation.Sentry { if (FilterEvents) { - var sqlEx = logEvent.Exception as SQLiteException; - if (sqlEx != null && FilteredSQLiteErrors.Contains(sqlEx.ResultCode)) + var exceptions = new List(); + + var aggEx = logEvent.Exception as AggregateException; + + if (aggEx != null && aggEx.InnerExceptions.Count > 0) { - return false; + exceptions.AddRange(aggEx.InnerExceptions); } - - if (FilteredExceptionTypeNames.Contains(logEvent.Exception.GetType().Name)) + else { - return false; + exceptions.Add(logEvent.Exception); } - if (FilteredExceptionMessages.Any(x => logEvent.Exception.Message.Contains(x))) + // If any are sentry then send to sentry + foreach (var ex in exceptions) { - return false; + var isSentry = true; + + var sqlEx = ex as SQLiteException; + if (sqlEx != null && !FilteredSQLiteErrors.Contains(sqlEx.ResultCode)) + { + isSentry = false; + } + + if (FilteredExceptionTypeNames.Contains(ex.GetType().Name)) + { + isSentry = false; + } + + if (FilteredExceptionMessages.Any(x => ex.Message.Contains(x))) + { + isSentry = false; + } + + if (isSentry) + { + return true; + } } + + // The exception or aggregate exception children were not sentry exceptions + return false; } return true;