From 467969be4763c27ddf152ce170ef37ff8b542831 Mon Sep 17 00:00:00 2001 From: James Crosswell Date: Mon, 30 Nov 2026 20:19:06 +1300 Subject: [PATCH] fix(ios): count native events dropped by managed callbacks once in client reports sentry-cocoa already records a before_send discard when the managed BeforeSend bridge returns nil, so skip the .NET record for native events. Closes #5681 Co-Authored-By: Claude --- src/Sentry/Internal/SentryEventHelper.cs | 25 ++++++++++++++----- src/Sentry/Platforms/Cocoa/SentrySdk.cs | 6 +++-- .../Platforms/iOS/SentrySdkTests.cs | 22 ++++++++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/Sentry/Internal/SentryEventHelper.cs b/src/Sentry/Internal/SentryEventHelper.cs index 54e8af85cd..6c6c6e88ea 100644 --- a/src/Sentry/Internal/SentryEventHelper.cs +++ b/src/Sentry/Internal/SentryEventHelper.cs @@ -5,7 +5,7 @@ namespace Sentry.Internal; internal static class SentryEventHelper { public static SentryEvent? ProcessEvent(SentryEvent? evt, IEnumerable processors, - SentryHint? hint, SentryOptions options, DataCategory dataCategory) + SentryHint? hint, SentryOptions options, DataCategory dataCategory, bool recordDiscards = true) { if (evt == null) { @@ -27,14 +27,20 @@ internal static class SentryEventHelper } catch (Exception e) { - options.ClientReportRecorder.RecordDiscardedEvent(DiscardReason.CallbackError, dataCategory); + if (recordDiscards) + { + options.ClientReportRecorder.RecordDiscardedEvent(DiscardReason.CallbackError, dataCategory); + } options.LogError(e, "Event processor {0} threw an exception. The event will be dropped.", processor.GetType().Name); return null; } if (processedEvent == null) { - options.ClientReportRecorder.RecordDiscardedEvent(DiscardReason.EventProcessor, dataCategory); + if (recordDiscards) + { + options.ClientReportRecorder.RecordDiscardedEvent(DiscardReason.EventProcessor, dataCategory); + } options.LogInfo("Event dropped by processor {0}", processor.GetType().Name); break; } @@ -42,7 +48,8 @@ internal static class SentryEventHelper return processedEvent; } - public static SentryEvent? DoBeforeSend(SentryEvent? @event, SentryHint hint, SentryOptions options) + public static SentryEvent? DoBeforeSend(SentryEvent? @event, SentryHint hint, SentryOptions options, + bool recordDiscards = true) { if (@event is null || options.BeforeSendInternal is null) { @@ -55,13 +62,19 @@ internal static class SentryEventHelper @event = options.BeforeSendInternal?.Invoke(@event, hint); if (@event == null) // Rejected event { - options.ClientReportRecorder.RecordDiscardedEvent(DiscardReason.BeforeSend, DataCategory.Error); + if (recordDiscards) + { + options.ClientReportRecorder.RecordDiscardedEvent(DiscardReason.BeforeSend, DataCategory.Error); + } options.LogInfo("Event dropped by BeforeSend callback."); } } catch (Exception e) { - options.ClientReportRecorder.RecordDiscardedEvent(DiscardReason.CallbackError, DataCategory.Error); + if (recordDiscards) + { + options.ClientReportRecorder.RecordDiscardedEvent(DiscardReason.CallbackError, DataCategory.Error); + } options.LogError(e, "The BeforeSend callback threw an exception. The event will be dropped."); return null; } diff --git a/src/Sentry/Platforms/Cocoa/SentrySdk.cs b/src/Sentry/Platforms/Cocoa/SentrySdk.cs index a4bb4ca3fa..8413b90fc1 100644 --- a/src/Sentry/Platforms/Cocoa/SentrySdk.cs +++ b/src/Sentry/Platforms/Cocoa/SentrySdk.cs @@ -309,14 +309,16 @@ private static bool SuppressNativeCrash(SentryOptions options, CocoaSdk.SentryOb return evt; } + // sentry-cocoa records its own before_send discard when we return null var sentryEvent = evt.ToSentryEvent(); - if (SentryEventHelper.ProcessEvent(sentryEvent, manualProcessors, null, options, DataCategory.Error) + if (SentryEventHelper.ProcessEvent(sentryEvent, manualProcessors, null, options, DataCategory.Error, + recordDiscards: false) is not { } processedEvent) { return null; } - processedEvent = SentryEventHelper.DoBeforeSend(processedEvent, new SentryHint(), options); + processedEvent = SentryEventHelper.DoBeforeSend(processedEvent, new SentryHint(), options, recordDiscards: false); if (processedEvent == null) { return null; diff --git a/test/Sentry.Tests/Platforms/iOS/SentrySdkTests.cs b/test/Sentry.Tests/Platforms/iOS/SentrySdkTests.cs index 7bfea6aa57..b5eb88ebeb 100644 --- a/test/Sentry.Tests/Platforms/iOS/SentrySdkTests.cs +++ b/test/Sentry.Tests/Platforms/iOS/SentrySdkTests.cs @@ -509,6 +509,28 @@ public void ProcessOnBeforeSend_NonmatchingCrash_ReachesApplicationFilter(string native.ServerName.Should().Be("filtered"); } + [Theory] + [InlineData("Options", false)] + [InlineData("Options", true)] + [InlineData("BeforeSend", false)] + [InlineData("BeforeSend", true)] + public void ProcessOnBeforeSend_ApplicationFilterDropsEvent_LeavesDiscardToNativeSdk(string registration, bool throws) + { + var options = new SentryOptions(); + var recorder = Substitute.For(); + options.ClientReportRecorder = recorder; + var scope = new Scope(options); + RegisterFilter(options, scope, registration, + _ => throws ? throw new InvalidOperationException("callback failed") : null!); + using var native = new CocoaSdk.SentryObjCEvent(); + + var result = SentrySdk.ProcessOnBeforeSend(options, native, CreateHub(scope)); + + result.Should().BeNull(); + recorder.DidNotReceive().RecordDiscardedEvent( + Arg.Any(), Arg.Any(), Arg.Any()); + } + private static object ReplaySnapshot(CocoaSdk.SentryObjCReplayOptions replay) => new { replay.SessionSampleRate,