Skip to content

feat(unity): let Unity supply the replay session that captures error replays - #5990

Merged
jamescrosswell merged 8 commits into
mainfrom
spire/finalize-the-replay-hook-in-sentry-dotne
Nov 24, 2026
Merged

jamescrosswell merged 8 commits into
mainfrom
spire/finalize-the-replay-hook-in-sentry-dotne

Conversation

@bitsandfoxes

@bitsandfoxes bitsandfoxes commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Problem

In Unity apps on Android and iOS, an exception captured in C# never brings its session replay with it. The native SDK (sentry-java, sentry-cocoa) records the replay, but C# errors go through the .NET SentryClient, so the native SDK never learns of them. In buffer mode (OnErrorSampleRate), the native SDK's buffered replay is never sent, and neither the error nor the transaction it happened in carries a replay_id to link it to a replay. Other events can carry a stale replay id, because the DSC cached on the propagation context only has the replay that was active when that DSC was created.

How

An SDK that records replays through a native SDK sets the internal SentryOptions.ReplaySession to its own IReplaySession. sentry-unity does this on Android (getsentry/sentry-unity#2948). Its CaptureReplay asks the native SDK to send its buffered replay and returns that replay's id. A Unity user then sees each C# error with its replay, the transaction it happened in linked to that replay, and each other event and transaction linked to the replay that was running.

Which replay an event carries

Diagram

Diagram source
flowchart LR
    B{Event kept by BeforeSend and sampling?} -- no --> X([Dropped, no replay])
    B -- yes --> C{ReplaySession set?}
    C -- no --> U([Sent as before])
    C -- yes --> D{Exception?}
    D -- yes --> E[Native SDK sends its buffered replay]:::hot
    E --> G([Error carries that replay's id]):::good
    D -- no --> F([Event carries the active replay's id])

IReplaySession gains CaptureReplay(SentryEvent). SentryClient calls the session after BeforeSend, sampling and redaction: it calls CaptureReplay for events with an exception and reads ActiveReplayId for the others. When the id differs from the event's DSC, the event gets its own copy of the DSC with replay_id set or removed. A session that throws is logged, and the event is still sent unchanged. An empty DSC (a trace the SDK doesn't head) stays empty.

Which replay a transaction carries

Diagram

Diagram source
flowchart LR
    T([Transaction starts]) --> A[Takes the active replay]
    E([Error inside it captures a replay]) --> H{Transaction has a replay already?}
    H -- yes --> K[Keeps its replay]
    H -- no --> I[Takes the captured replay]:::hot
    A --> R([Transaction and its later requests carry that replay]):::good
    K --> R
    I --> R

The hub takes the transaction's replay id from options.ReplaySession when the transaction starts. When an error's CaptureReplay returns an id and the scope's transaction (sampled or not) has no replay_id, the transaction gets a copy of its DSC with that id, as sentry-java does with Baggage.forceSetReplayId. Hub.GetBaggage reads the transaction's DSC, so outgoing requests made in the transaction afterwards carry it too. Originals are shared with the propagation context and queued envelopes, so they stay as they are.

Caveats

  • Without options.ReplaySession, nothing changes: the built-in ReplaySession returns no replay from CaptureReplay.
  • Outgoing requests made outside a transaction keep the propagation context's cached DSC, so a replay captured after it was created doesn't reach them. sentry-java behaves the same way.
  • MAUI apps have the same gap for C# errors. That is left to MAUI: errors captured in C# never send the on-error session replay #5998: the built-in session needs to capture natively, and the client needs to use it when options.ReplaySession isn't set.

#skip-changelog: IReplaySession and SentryOptions.ReplaySession are internal and only sentry-unity uses them.

🤖 Generated with Claude Code

bitsandfoxes and others added 4 commits November 16, 2026 12:26
An SDK that records replays itself (Unity on Android) sets an internal
replay session on the options. The hub uses it for the replay id of the
dynamic sampling context, and the client asks it to capture the replay
for each error event that passed BeforeSend and sampling. The event gets
its own copy of the dynamic sampling context with the replay id the
session returned, or without one when it returned none. Without a
supplied session, nothing changes.
With a replay session supplied by an SDK, the dynamic sampling context of
events without an exception gets the session's active replay id, or none
when no replay is active, instead of the one cached on the propagation
context when it was created. The event gets its own copy when the id
differs; the shared context is never changed.
Sentry.Unity.iOS and its tests can see the internal replay session, so
Unity on iOS can supply one like Unity on Android does. New tests cover
the hub reading the replay session from the options, a replay session
that throws, and a capture that returns an empty replay id.

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 23, 2026
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

PR risk: medium

For commit b0cb4dc.

Main concerns: data sent to Sentry, reach, host performance.

This label is a hint for reviewers, not an approval.

@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.98%. Comparing base (0460fdc) to head (b0cb4dc).

Files with missing lines Patch % Lines
src/Sentry/Internal/ReplaySession.cs 0.00% 1 Missing ⚠️
src/Sentry/SentryClient.cs 96.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5990      +/-   ##
==========================================
+ Coverage   77.94%   77.98%   +0.04%     
==========================================
  Files         535      535              
  Lines       19843    19882      +39     
  Branches     3970     3980      +10     
==========================================
+ Hits        15466    15505      +39     
  Misses       3533     3533              
  Partials      844      844              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bitsandfoxes
bitsandfoxes marked this pull request as ready for review November 23, 2026 21:21
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 23, 2026
@jamescrosswell

Copy link
Copy Markdown
Collaborator

I've opened #5998 for the MAUI side. Implementing CaptureReplay in the built-in ReplaySession won't be enough on its own, because the hook returns early unless options.ReplaySession is set. One option is to default options.ReplaySession to ReplaySession.Instance and have the built-in CaptureReplay return ActiveReplayId until it can capture natively. The null check would then go away, and the Hub, the client and SentrySpanProcessor would all read the same session. It's fine to leave that to the MAUI issue.

@jamescrosswell jamescrosswell left a comment

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.

The hook sits where sentry-java has it: after beforeSend and sampling, for errors only. One change is needed to match sentry-java:

The transaction misses the replay its error captured. The transaction reads ActiveReplayId once, in StartTransaction. In buffer mode there's no id yet, so when an error inside the transaction captures the replay, only the error's copy of the DSC gets it. The transaction goes out without it, and so do later outgoing requests in that transaction, because Hub.GetBaggage reads the transaction's DSC.

sentry-java 8.65.0 handles this in SentryClient.captureEvent: when captureReplay returns an id, it sets that id on the active transaction's baggage (Baggage.forceSetReplayId). Could the hook do the same? When CaptureReplay returns an id and scope.Transaction's DSC has no replay_id, assign the transaction a copy of its DSC with the id set. A copy rather than an in-place change, because envelopes of earlier errors in the transaction may still be queued and hold a reference to the old one. A cloned scope shares the same transaction, so this also works for CaptureEvent with configureScope.

Outgoing requests made outside a transaction would still use the propagation context's cached DSC. sentry-java doesn't cover that either, so it seems fine to leave. The diagram currently suggests both are covered, though.

bitsandfoxes and others added 3 commits November 24, 2026 05:17
When an error inside a transaction captures a replay and the transaction's
dynamic sampling context has no replay id yet, the transaction gets a copy
of it with the captured id. The transaction then goes out with the replay,
and so does the baggage of outgoing requests made in it. A copy rather than
a change in place, since envelopes of earlier errors in the transaction may
still be queued with the old one.

Co-Authored-By: Claude <noreply@anthropic.com>
A transaction whose dynamic sampling context is empty doesn't propagate
one, because the SDK isn't the head of the trace. An error captured in it
that captures a replay no longer gives that context, or the event's copy
of it, a lone replay id without the trace id and public key.

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Mutation testing breaks the lines a PR changes on purpose, one mutation at a time, and checks that a test fails each time. A changed line no test runs fails the check, and a mutant that survives the tests only warns. Full report

Sentry: tests caught 7 of 7 mutants on the changed lines

Stryker couldn't build 50 mutants on the changed lines, so no test ran against them.

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 24, 2026
@bitsandfoxes

Copy link
Copy Markdown
Contributor Author

Missed that, thanks! The transaction part is in: when a capture returns an id and the scope's transaction has no replay_id yet, the transaction gets a copy of its DSC with the id, like Baggage.forceSetReplayId. Outgoing requests made later in that transaction carry it too, since Hub.GetBaggage reads the transaction's DSC. Requests outside a transaction aren't covered, matching what happens in sentry-java. I've updated the diagram and the caveats to say so.

I went ahead and did the MAUI part as well in #6001 so we don't have to guess. options.ReplaySession stays unset. The client falls back to the built-in session only on Android and iOS. A default everywhere would strip a frontend's replay_id on servers. The client removes the id when it differs from the session's, and the built-in session has none off mobile (#3429). On mobile the built-in session now captures natively (Java's replay controller on Android, Cocoa's internal replay API on iOS), so no interim ActiveReplayId step is needed. The Hub and SentrySpanProcessor both fall back to the same built-in instance, so all three agree on mobile. When Unity sets its own session, SentrySpanProcessor needs no change either: every transaction it starts goes through Hub.StartTransaction, which sets the DSC's replay id from the Hub's session.

@jamescrosswell
jamescrosswell merged commit 32f2cd3 into main Nov 24, 2026
65 checks passed
@jamescrosswell
jamescrosswell deleted the spire/finalize-the-replay-hook-in-sentry-dotne branch November 24, 2026 20:57
jamescrosswell added a commit that referenced this pull request Nov 25, 2026
The conflicts in ReplaySession.cs, SentryClient.cs and SentryClientTests.cs
come from #5990 landing on main as a squash. This branch already had all of
#5990, so they resolve to this branch's versions. Against main, the merge
leaves exactly this PR's own changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
bitsandfoxes added a commit that referenced this pull request Nov 25, 2026
It resolved the #5990 squash conflicts to this branch's versions; this keeps
it in the branch's history alongside the two review fixes made since.

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants