Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4f86e96
Let an SDK supply the replay session that captures error replays
bitsandfoxes Nov 16, 2026
9513c02
Give non-error events the replay that's active when they're sent
bitsandfoxes Nov 16, 2026
d6590dd
Open the replay hook to Unity's iOS assemblies and cover its edge cases
bitsandfoxes Nov 23, 2026
43ae1a5
Merge main into the replay hook branch
bitsandfoxes Nov 23, 2026
32464ff
fix(maui): capture the on-error replay for errors captured in C#
bitsandfoxes Nov 23, 2026
09a6455
Give the active transaction the replay its error captured
bitsandfoxes Nov 24, 2026
b9044c5
Leave an empty dynamic sampling context without a replay id
bitsandfoxes Nov 24, 2026
c1d0ae3
Merge main into the replay hook branch
bitsandfoxes Nov 24, 2026
b0cb4dc
Cover the replay id given to an unsampled transaction
bitsandfoxes Nov 24, 2026
0e2ec9c
Merge the replay hook branch: the active transaction gets the capture…
bitsandfoxes Nov 24, 2026
31fabca
Verify on-error replays in the MAUI sample on Android and iOS
bitsandfoxes Nov 24, 2026
da93e23
Skip the iOS replay capture when replay is off, and keep the active i…
bitsandfoxes Nov 24, 2026
5c22bb9
Cover the replay session's desktop capture and drop an unreachable br…
bitsandfoxes Nov 24, 2026
f180071
Keep the Java scope getters the replay session reads by name
bitsandfoxes Nov 24, 2026
ee9f93c
Keep only the replay session tests that check something the others don't
bitsandfoxes Nov 24, 2026
e890e49
Leave the span processor's replay session as it is
bitsandfoxes Nov 24, 2026
3f9f702
Merge main into the MAUI error replay branch
jamescrosswell Nov 25, 2026
015e0ce
Skip the Android replay capture when session replay is off
bitsandfoxes Nov 25, 2026
04e9d5a
Say what a false crash costs the Java replay, in the IsCrash comment
bitsandfoxes Nov 25, 2026
d53042e
Merge James's merge of main into the MAUI error replay branch
bitsandfoxes Nov 25, 2026
ad239f4
Merge main into the MAUI error replay branch
bitsandfoxes Nov 25, 2026
9906f75
Skip reading the native replay id when session replay is off
bitsandfoxes Nov 25, 2026
1072c63
Merge main into the MAUI error replay branch
bitsandfoxes Nov 25, 2026
689fd9e
Merge remote-tracking branch 'origin/main' into spire/maui-error-repl…
jamescrosswell Nov 25, 2026
72e83e8
Read the Java scope's replay id through the binding
jamescrosswell Nov 25, 2026
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
8 changes: 6 additions & 2 deletions scripts/patch-cocoa-bindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
.PropertyToMethod("SentryObjCSpan", "ToTraceHeader")
.PropertyToMethod("SentryObjCTraceContext", "ToBaggage")
.PropertyToMethod("SentryObjCInternalScopeApi", "CreateScope")
.PropertyToMethod("SentryObjCInternalReplayApi", "Capture")
// Verify the rest
.VerifyProperty("*Sentry*", "*", "MethodToProperty") // TODO: replace broad patterns with one-by-one verification
.VerifyProperty("SentryObjCOptions", "*Targets", "StronglyTypedNSArray")
Expand Down Expand Up @@ -91,9 +92,11 @@
// signatures reference SentryObjC* types we don't whitelist.
.KeepMethods("SentryObjCSDK", "StartWithOptions", "ConfigureScope", "Crash")
.KeepProperties("SentryObjCSDK", "Internal", "CrashedLastRun")
// SentryObjCInternalApi: keep only the sub-APIs the .NET SDK uses (sdk, profiling, debug).
.KeepProperties("SentryObjCInternalApi", "Sdk", "Profiling", "Debug", "Scope", "Serializer")
// SentryObjCInternalApi: keep only the sub-APIs the .NET SDK uses.
.KeepProperties("SentryObjCInternalApi", "Sdk", "Profiling", "Debug", "Scope", "Serializer", "Replay")
.KeepMethods("SentryObjCInternalApi", "SetTrace", "IgnoreNextSignal")
.KeepMethods("SentryObjCInternalReplayApi", "Capture")
.KeepProperties("SentryObjCInternalReplayApi", "ReplayId")
.KeepInterfaces(
"SentryObjCAttachment",
"SentryObjCBreadcrumb",
Expand All @@ -108,6 +111,7 @@
"SentryObjCInternalApi",
"SentryObjCInternalDebugApi",
"SentryObjCInternalProfilingApi",
"SentryObjCInternalReplayApi",
"SentryObjCInternalScopeApi",
"SentryObjCInternalSdkApi",
"SentryObjCInternalSerializerApi",
Expand Down
20 changes: 20 additions & 0 deletions src/Sentry.Bindings.Cocoa/ApiDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1709,6 +1709,10 @@ interface SentryObjCInternalApi
[Export("serializer")]
SentryObjCInternalSerializerApi Serializer { get; }

// @property (readonly, nonatomic) SentryObjCInternalReplayApi * _Nonnull replay;
[Export("replay")]
SentryObjCInternalReplayApi Replay { get; }

// @property (readonly, nonatomic) SentryObjCInternalProfilingApi * _Nonnull profiling;
[Export("profiling")]
SentryObjCInternalProfilingApi Profiling { get; }
Expand Down Expand Up @@ -1756,6 +1760,22 @@ interface SentryObjCInternalProfilingApi
void DiscardFor(SentryObjCId traceId);
}

// @interface SentryObjCInternalReplayApi : NSObject
[BaseType(typeof(NSObject))]
[DisableDefaultCtor]
[Internal]
interface SentryObjCInternalReplayApi
{

// -(BOOL)capture;
[Export("capture")]
bool Capture();

// @property (readonly, nonatomic) NSString * _Nullable replayId;
[NullAllowed, Export("replayId")]
string ReplayId { get; }
}

// @interface SentryObjCInternalScopeApi : NSObject
[BaseType(typeof(NSObject))]
[DisableDefaultCtor]
Expand Down
146 changes: 140 additions & 6 deletions src/Sentry/Internal/ReplaySession.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#if __ANDROID__
using Sentry.Android.Extensions;
using System.Runtime.ExceptionServices;
using Sentry.Extensibility;
#if __IOS__
using CoreFoundation;
#endif

namespace Sentry.Internal;
Expand All @@ -19,6 +21,15 @@ internal class ReplaySession : IReplaySession
{
public static readonly IReplaySession Instance = new ReplaySession();

internal static IReplaySession? PlatformDefault =>
#if __ANDROID__ || __IOS__
Instance;
#else
null;
#endif

internal static readonly TimeSpan MainThreadCaptureTimeout = TimeSpan.FromSeconds(1);

private ReplaySession()
{
}
Expand All @@ -27,19 +38,142 @@ public SentryId? ActiveReplayId
{
get
{
#if __ANDROID__ || __IOS__
if (SentrySdk.CurrentOptions?.Native.ExperimentalOptions.SessionReplay.IsSessionReplayEnabled != true)
{
return null;
}
#endif
#if __ANDROID__
Comment thread
jamescrosswell marked this conversation as resolved.
Resolved
// Check to see if a Replay ID is available
var replayId = JavaSdk.ScopesAdapter.Instance?.Options?.ReplayController?.ReplayId?.ToSentryId();
return (replayId is { } id && id != SentryId.Empty) ? id : null;
try
{
return ReadJavaScopeReplayId();
}
catch (Exception e)
{
SentrySdk.CurrentOptions?.LogError(e, "Failed to read the replay id from the Java scope.");
return null;
}
#elif __IOS__
string? nativeId = null;
Sentry.Cocoa.CocoaScope.Configure(scope => nativeId = scope.ReplayId);
Comment thread
jamescrosswell marked this conversation as resolved.
Resolved
return (nativeId is { } id) ? SentryId.Parse(id) : null;
return ToReplayId(nativeId);
#else
return null;
#endif
}
}

#if __ANDROID__
public SentryId? CaptureReplay(SentryEvent @event)
{
if (SentrySdk.CurrentOptions?.Native.ExperimentalOptions.SessionReplay.IsSessionReplayEnabled != true)
{
return null;
}

var capturedId = JavaSdk.ScopesAdapter.Instance?.Options?.ReplayController?.CaptureReplay(JavaBoolean.ValueOf(IsCrash(@event)));
Comment thread
jamescrosswell marked this conversation as resolved.
Resolved
return ToReplayId(capturedId?.ToString()) ?? ActiveReplayId;
}
#elif __IOS__
public SentryId? CaptureReplay(SentryEvent @event) =>
CaptureCocoaReplay(SentrySdk.CurrentOptions, NSThread.IsMain, work => DispatchQueue.MainQueue.DispatchAsync(work));
#else
public SentryId? CaptureReplay(SentryEvent @event) => null;
#endif

internal static SentryId? ToReplayId(string? nativeId) =>
Guid.TryParse(nativeId, out var guid) && guid != Guid.Empty ? new SentryId(guid) : null;

// Narrower than HasUnhandledTerminalException(): Java defers a terminating capture's replay to the next launch and stops the replay from ever switching to session mode.
internal static bool IsCrash(SentryEvent @event) =>
Comment thread
jamescrosswell marked this conversation as resolved.
Resolved
@event.SentryExceptions?.Any(exception => exception.Mechanism is { } mechanism && (
mechanism.Terminal == true ||
mechanism is { Handled: false, Type: "AppDomain.UnhandledException" or "UnhandledExceptionRaiser" })) == true;

internal static SentryId? CaptureOnMainThread(

@bitsandfoxes bitsandfoxes Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Cocoa, captureReplayInternal reads main-thread-only state through runOnMainThreadSync. We could move this helper into the #if __IOS__ as well but then the tests only run as part of the device tests.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My vote would be to leave it where it is - device tests are expensive and consequently late discovery (leads to longer cycle times on fixing things).

Func<SentryId?> capture,
Func<SentryId?> timedOutFallback,
bool isMainThread,
Action<Action> dispatchToMainThread,
TimeSpan timeout,
SentryOptions? options)
{
if (isMainThread)
{
return capture();
}

SentryId? replayId = null;
Exception? error = null;
// Not disposed: after a timeout the dispatched capture still runs and sets it.
var captured = new ManualResetEventSlim();
dispatchToMainThread(() =>
{
try
{
replayId = capture();
}
catch (Exception e)
{
error = e;
}
finally
{
captured.Set();
}
});

if (!captured.Wait(timeout))
{
options?.LogWarning("Capturing the replay on the main thread timed out after {0}. The event is sent with the active replay id, if any.", timeout);
return timedOutFallback();
}

if (error is not null)
{
ExceptionDispatchInfo.Capture(error).Throw();
}

return replayId;
}

#if __IOS__
internal SentryId? CaptureCocoaReplay(SentryOptions? options, bool isMainThread, Action<Action> dispatchToMainThread)
{
if (options?.Native.ExperimentalOptions.SessionReplay.IsSessionReplayEnabled != true)
{
return null;
}

return CaptureOnMainThread(
CaptureNativeReplay,
() => ActiveReplayId,
isMainThread,
dispatchToMainThread,
MainThreadCaptureTimeout,
options);
}

private static SentryId? CaptureNativeReplay()
{
var replay = SentryCocoaHybridSdk.Internal.Replay;
return replay.Capture() ? ToReplayId(replay.ReplayId) : null;
}
#endif

#if __ANDROID__
// In the order Java's CombinedScopeView reads them.
internal static SentryId? ReadJavaScopeReplayId()
{
if (JavaSdk.ScopesAdapter.Instance is not { } scopes)
{
return null;
}

return ToReplayId(scopes.Scope.ReplayId.ToString())
?? ToReplayId(scopes.IsolationScope.ReplayId.ToString())
?? ToReplayId(scopes.GlobalScope.ReplayId.ToString());
}
#endif
}
3 changes: 2 additions & 1 deletion src/Sentry/SentryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ private SentryId DoSendEvent(SentryEvent @event, SentryHint? hint, Scope? scope)
// The DSC cached on the propagation context only has the replay that was active when it was created.
private void CaptureReplay(SentryEvent @event, Scope scope)
{
if (_options.ReplaySession is not { } replaySession)
var replaySession = _options.ReplaySession ?? ReplaySession.PlatformDefault;
if (replaySession is null)
{
return;
}
Expand Down
35 changes: 35 additions & 0 deletions test/Sentry.Tests/Platforms/Android/ReplaySessionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#if ANDROID
namespace Sentry.Tests.Platforms.Android;

[CollectionDefinition(nameof(ReplaySessionTests), DisableParallelization = true)]
public class ReplaySessionTestsCollection;

[Collection(nameof(ReplaySessionTests))]
public class ReplaySessionTests
{
[Fact]
public void ReadJavaScopeReplayId_GlobalScopeHasReplayId_ReturnsIt()
{
var scopes = JavaSdk.ScopesAdapter.Instance!;
var empty = JavaSdk.Protocol.SentryId.EmptyId!;
var current = scopes.Scope.ReplayId;
var isolation = scopes.IsolationScope.ReplayId;
var global = scopes.GlobalScope.ReplayId;
var replayId = SentryId.Create();
try
{
scopes.Scope.ReplayId = empty;
scopes.IsolationScope.ReplayId = empty;
scopes.GlobalScope.ReplayId = new JavaSdk.Protocol.SentryId(replayId.ToString());

ReplaySession.ReadJavaScopeReplayId().Should().Be(replayId);
}
finally
{
scopes.Scope.ReplayId = current;
scopes.IsolationScope.ReplayId = isolation;
scopes.GlobalScope.ReplayId = global;
}
}
}
#endif
21 changes: 21 additions & 0 deletions test/Sentry.Tests/Platforms/iOS/ReplaySessionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#if IOS
namespace Sentry.Tests.Platforms.iOS;

public class ReplaySessionTests
{
[Theory]
[InlineData(false)]
[InlineData(true)]
public void CaptureCocoaReplay_ReplayOff_ReturnsNullWithoutDispatch(bool withOptions)
{
var options = withOptions ? new SentryOptions() : null;
var dispatched = false;

var result = ((ReplaySession)ReplaySession.Instance).CaptureCocoaReplay(options, isMainThread: false,
_ => dispatched = true);

result.Should().BeNull();
dispatched.Should().BeFalse();
}
}
#endif
Loading
Loading