Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
a1b0cb6
Initial commit for exposing iOS native before send & crashed last run
aritchie Feb 11, 2025
1a8474e
Initial feature set is working
aritchie Feb 11, 2025
c36be94
Format code
getsentry-bot Feb 11, 2025
cd074d4
Update CHANGELOG.md
aritchie Feb 11, 2025
17790c7
Merge branch '2102-ios_beforesend_oncrashedlastrun' of https://github…
aritchie Feb 11, 2025
70a677d
Merge signal abort code before user based BeforeSend
aritchie Feb 12, 2025
4a6462c
Move native BeforeSend & OnCrashedLastRun events to SentryOptions - O…
aritchie Feb 14, 2025
91145ab
Always run NativeOptions.BeforeSend so sig aborts can be filtered
aritchie Feb 14, 2025
fd48026
Update SentrySdk.cs
aritchie Feb 14, 2025
1b3dfdd
Merge branch 'main' into 2102-ios_beforesend_oncrashedlastrun
aritchie Feb 18, 2025
c29a785
Update CHANGELOG.md
aritchie Feb 18, 2025
50a5554
Format code
getsentry-bot Feb 18, 2025
63e3f55
WIP - removing serialization mechanics as they cause a native crash d…
aritchie Feb 18, 2025
5c06eb5
Transferring transformed sentryevent back to original native event in…
aritchie Feb 21, 2025
18a1079
Merge branch 'main' into 2102-ios_beforesend_oncrashedlastrun
aritchie Feb 21, 2025
000b324
Remove unnecessary hacks
aritchie Feb 21, 2025
bdfe3a9
Format code
getsentry-bot Feb 21, 2025
bf4d33d
Add missing bindable option for native iOS sentryoptions
aritchie Feb 21, 2025
52f0699
Update SentrySdk.cs
aritchie Feb 24, 2025
dab118e
Update SentrySdk.cs
aritchie Feb 24, 2025
1f721b8
Update CHANGELOG.md
aritchie Feb 24, 2025
4830f8e
Update CHANGELOG.md
aritchie Feb 24, 2025
b8b08cc
Unit tests mapping checks to/from native iOS
aritchie Feb 24, 2025
a46bd43
Format code
getsentry-bot Feb 24, 2025
5dca9e2
Update NativeSerializationTests.cs
aritchie Feb 24, 2025
00cbc04
Revert "Add missing bindable option for native iOS sentryoptions"
aritchie Feb 24, 2025
d2776e1
Remove iOS native event suppress configuration
aritchie Feb 24, 2025
4be3abb
Ability to suppress native errors
aritchie Feb 24, 2025
fc85bce
Update CHANGELOG.md
aritchie Feb 24, 2025
5e8134a
Update CHANGELOG.md
aritchie Feb 24, 2025
d8ac6ea
Rename tests according to review
aritchie Feb 25, 2025
63326ec
add timestamp for deserialize test
aritchie Feb 25, 2025
d1feffc
Account for fractions of seconds
aritchie Feb 26, 2025
b7f28d9
Merge branch '2102-ios_beforesend_oncrashedlastrun' into 3944-ios_nat…
aritchie Feb 26, 2025
9270d50
Add unit tests for suppress segfaults and beforesend
aritchie Feb 26, 2025
941e671
Merge branch 'main' into 3944-ios_native_error_suppression
aritchie Feb 26, 2025
92d38b6
Format code
getsentry-bot Feb 26, 2025
30b3bd3
Update SentrySdkTests.cs
aritchie Feb 26, 2025
8903a56
Merge branch 'main' into 3944-ios_native_error_suppression
aritchie Mar 3, 2025
de9afa5
Update CHANGELOG.md
aritchie Mar 3, 2025
599185f
Update SentrySdkTests.cs
aritchie Mar 4, 2025
3da967a
Updating additional changes requested from code review
aritchie Mar 4, 2025
0f159b7
Test fixes
aritchie Mar 4, 2025
dd37709
Update SentrySdkTests.cs
aritchie Mar 4, 2025
925a632
Merge branch 'main' into 3944-ios_native_error_suppression
aritchie Mar 4, 2025
a4ba982
Update CHANGELOG.md
aritchie Mar 4, 2025
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
Next Next commit
Initial commit for exposing iOS native before send & crashed last run
  • Loading branch information
aritchie committed Feb 11, 2025
commit a1b0cb6acf95ffbbeabbd1f2642f14f37ee00c72
11 changes: 11 additions & 0 deletions samples/Sentry.Samples.Ios/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ public override bool FinishedLaunching(UIApplication application, NSDictionary l
options.Native.EnableAppHangTracking = true;

options.CacheDirectoryPath = Path.GetTempPath();

options.Native.BeforeSend = e =>
{
e.SetTag("dotnet-iOS-Native-Before", "Hello World");
return e;
};

options.Native.CrashedLastRun = e =>
{
Console.WriteLine(e);
};
});

// create a new window instance based on the screen size
Expand Down
2 changes: 1 addition & 1 deletion samples/Sentry.Samples.Ios/Sentry.Samples.Ios.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0-ios18.0</TargetFramework>
<TargetFramework>net9.0-ios</TargetFramework>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>
Expand Down
6 changes: 6 additions & 0 deletions src/Sentry.Bindings.Cocoa/ApiDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2454,6 +2454,12 @@ interface PrivateSentrySDKOnly
[Static]
[Export ("breadcrumbWithDictionary:")]
SentryBreadcrumb BreadcrumbWithDictionary (NSDictionary dictionary);


// +(SentryEnvelope * _Nonnull)envelopeWithData:(NSData * _Nonnull)data;
[Static]
[Export ("envelopeWithData:")]
SentryEnvelope EnvelopeWithData (NSData data);
}

// @interface SentryId : NSObject
Expand Down
10 changes: 10 additions & 0 deletions src/Sentry.Bindings.Cocoa/PrivateApiDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,13 @@ interface SentrySession
interface SentryTracer
{
}

[Internal]
[DisableDefaultCtor]
[BaseType (typeof(NSObject))]
interface SentryEnvelope
{
// @property (nonatomic, readonly, strong) NSArray<SentryEnvelopeItem *> *items;
[Export("items")]
NSArray Items { get; }
}
91 changes: 46 additions & 45 deletions src/Sentry/Platforms/Cocoa/Extensions/SentryEventExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
// using Sentry.Extensibility;
// using Sentry.Protocol.Envelopes;
//
// namespace Sentry.Cocoa.Extensions;
//
// internal static class SentryEventExtensions
// {
// /*
// * These methods map between a SentryEvent and it's Cocoa counterpart by serializing as JSON into memory on one side,
// * then deserializing back to an object on the other side. It is not expected to be performant, as this code is only
// * used when a BeforeSend option is set, and then only when an event is captured by the Cocoa SDK (which should be
// * relatively rare).
// *
// * This approach avoids having to write to/from methods for the entire object graph. However, it's also important to
// * recognize that there's not necessarily a one-to-one mapping available on all objects (even through serialization)
// * between the two SDKs, so some optional details may be lost when roundtripping. That's generally OK, as this is
// * still better than nothing. If a specific part of the object graph becomes important to roundtrip, we can consider
// * updating the objects on either side.
// */
//
// public static SentryEvent ToSentryEvent(this CocoaSdk.SentryEvent sentryEvent, SentryCocoaSdkOptions nativeOptions)
// {
// using var stream = sentryEvent.ToJsonStream()!;
// //stream.Seek(0, SeekOrigin.Begin); ??
//
// using var json = JsonDocument.Parse(stream);
// var exception = sentryEvent.Error == null ? null : new NSErrorException(sentryEvent.Error);
// return SentryEvent.FromJson(json.RootElement, exception);
// }
//
// public static CocoaSdk.SentryEvent ToCocoaSentryEvent(this SentryEvent sentryEvent, SentryOptions options, SentryCocoaSdkOptions nativeOptions)
// {
// var envelope = Envelope.FromEvent(sentryEvent);
//
// using var stream = new MemoryStream();
// envelope.Serialize(stream, options.DiagnosticLogger);
// stream.Seek(0, SeekOrigin.Begin);
//
// using var data = NSData.FromStream(stream)!;
// var cocoaEnvelope = CocoaSdk.PrivateSentrySDKOnly.EnvelopeWithData(data);
//
// var cocoaEvent = (CocoaSdk.SentryEvent) cocoaEnvelope.Items[0];
// return cocoaEvent;
// }
// }
using Sentry.Extensibility;
using Sentry.Protocol.Envelopes;

namespace Sentry.Cocoa.Extensions;

internal static class SentryEventExtensions
{
/*
* These methods map between a SentryEvent and it's Cocoa counterpart by serializing as JSON into memory on one side,
* then deserializing back to an object on the other side. It is not expected to be performant, as this code is only
* used when a BeforeSend option is set, and then only when an event is captured by the Cocoa SDK (which should be
* relatively rare).
*
* This approach avoids having to write to/from methods for the entire object graph. However, it's also important to
* recognize that there's not necessarily a one-to-one mapping available on all objects (even through serialization)
* between the two SDKs, so some optional details may be lost when roundtripping. That's generally OK, as this is
* still better than nothing. If a specific part of the object graph becomes important to roundtrip, we can consider
* updating the objects on either side.
*/

public static SentryEvent ToSentryEvent(this CocoaSdk.SentryEvent sentryEvent, SentryCocoaSdkOptions nativeOptions)
{
using var stream = sentryEvent.ToJsonStream()!;
//stream.Seek(0, SeekOrigin.Begin); ??

using var json = JsonDocument.Parse(stream);
var exception = sentryEvent.Error == null ? null : new NSErrorException(sentryEvent.Error);
var ev = SentryEvent.FromJson(json.RootElement, exception);
return ev;
}

public static CocoaSdk.SentryEvent ToCocoaSentryEvent(this SentryEvent sentryEvent, SentryOptions options, SentryCocoaSdkOptions nativeOptions)
{
var envelope = Envelope.FromEvent(sentryEvent);

using var stream = new MemoryStream();
envelope.Serialize(stream, options.DiagnosticLogger);
stream.Seek(0, SeekOrigin.Begin);

using var data = NSData.FromStream(stream)!;
var cocoaEnvelope = CocoaSdk.PrivateSentrySDKOnly.EnvelopeWithData(data);

var cocoaEvent = (CocoaSdk.SentryEvent) cocoaEnvelope.Items.GetItem<CocoaSdk.SentryEvent>(0);
return cocoaEvent;
}
}
10 changes: 10 additions & 0 deletions src/Sentry/Platforms/Cocoa/SentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ public void AddInAppInclude(string prefix)
InAppIncludes ??= new List<string>();
InAppIncludes.Add(prefix);
}

/// <summary>
/// Sets native BeforeSend callback
/// </summary>
public Func<SentryEvent, SentryEvent>? BeforeSend { get; set; }

/// <summary>
/// Sets native OnCrashedLastRun callback
/// </summary>
public Action<SentryEvent>? CrashedLastRun { get; set; }
}

// We actually add the profiling integration automatically in InitSentryCocoaSdk().
Expand Down
60 changes: 39 additions & 21 deletions src/Sentry/Platforms/Cocoa/SentrySdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ private static void InitSentryCocoaSdk(SentryOptions options)
nativeOptions.SendDefaultPii = options.SendDefaultPii;
nativeOptions.SessionTrackingIntervalMillis = (nuint)options.AutoSessionTrackingInterval.TotalMilliseconds;

// nativeOptions.BeforeSend
if (options.Environment is { } environment)
{
nativeOptions.Environment = environment;
Expand Down Expand Up @@ -88,27 +89,44 @@ private static void InitSentryCocoaSdk(SentryOptions options)

// TODO: Finish SentryEventExtensions to enable these

// if (options.Native.EnableBeforeSend && options.BeforeSend is { } beforeSend)
// {
// nativeOptions.BeforeSend = evt =>
// {
// var sentryEvent = evt.ToSentryEvent(nativeOptions);
// var result = beforeSend(sentryEvent)?.ToCocoaSentryEvent(options, nativeOptions);
//
// // Note: Nullable result is allowed but delegate is generated incorrectly
// // See https://github.com/xamarin/xamarin-macios/issues/15299#issuecomment-1201863294
// return result!;
// };
// }

// if (options.Native.OnCrashedLastRun is { } onCrashedLastRun)
// {
// nativeOptions.OnCrashedLastRun = evt =>
// {
// var sentryEvent = evt.ToSentryEvent(nativeOptions);
// onCrashedLastRun(sentryEvent);
// };
// }
if (options.Native.BeforeSend is { } beforeSend)
{
nativeOptions.BeforeSend = evt =>
{
// because we delegate to user code, we need to protect anything that could happen in this event
try
{
var sentryEvent = evt.ToSentryEvent(nativeOptions);
var result = beforeSend(sentryEvent)?.ToCocoaSentryEvent(options, nativeOptions);

// Note: Nullable result is allowed but delegate is generated incorrectly
// See https://github.com/xamarin/xamarin-macios/issues/15299#issuecomment-1201863294
return result!;
}
catch (Exception ex)
{
options.LogError(ex, "Before Send Error");
return evt;
}
};
}

if (options.Native.CrashedLastRun is { } onCrashedLastRun)
{
nativeOptions.OnCrashedLastRun = evt =>
{
// because we delegate to user code, we need to protect anything that could happen in this event
try
{
var sentryEvent = evt.ToSentryEvent(nativeOptions);
onCrashedLastRun(sentryEvent);
}
catch (Exception ex)
{
options.LogError(ex, "Crashed Last Run Error");
}
};
}

// These options are from Cocoa's SentryOptions
nativeOptions.AttachScreenshot = options.Native.AttachScreenshot;
Expand Down