Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/Sentry/Internal/SentryEventHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Sentry.Internal;
internal static class SentryEventHelper
{
public static SentryEvent? ProcessEvent(SentryEvent? evt, IEnumerable<ISentryEventProcessor> processors,
SentryHint? hint, SentryOptions options, DataCategory dataCategory)
SentryHint? hint, SentryOptions options, DataCategory dataCategory, bool recordDiscards = true)
{
if (evt == null)
{
Expand All @@ -27,22 +27,29 @@ 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;
}
}
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)
{
Expand All @@ -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;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Sentry/Platforms/Cocoa/SentrySdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions test/Sentry.Tests/Platforms/iOS/SentrySdkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IClientReportRecorder>();
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<DiscardReason>(), Arg.Any<DataCategory>(), Arg.Any<int>());
}

private static object ReplaySnapshot(CocoaSdk.SentryObjCReplayOptions replay) => new
{
replay.SessionSampleRate,
Expand Down
Loading