Repository navigation
feat(unity): let Unity supply the replay session that captures error replays - #5990
Conversation
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>
PR risk: mediumFor commit Main concerns: data sent to Sentry, reach, host performance. This label is a hint for reviewers, not an approval. |
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
|
I've opened #5998 for the MAUI side. Implementing |
jamescrosswell
left a comment
There was a problem hiding this comment.
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.
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>
|
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 linesStryker couldn't build 50 mutants on the changed lines, so no test ran against them. |
Co-Authored-By: Claude <noreply@anthropic.com>
|
Missed that, thanks! The transaction part is in: when a capture returns an id and the scope's transaction has no I went ahead and did the MAUI part as well in #6001 so we don't have to guess. |
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>
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>
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 areplay_idto 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.ReplaySessionto its ownIReplaySession. sentry-unity does this on Android (getsentry/sentry-unity#2948). ItsCaptureReplayasks 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 source
IReplaySessiongainsCaptureReplay(SentryEvent).SentryClientcalls the session afterBeforeSend, sampling and redaction: it callsCaptureReplayfor events with an exception and readsActiveReplayIdfor the others. When the id differs from the event's DSC, the event gets its own copy of the DSC withreplay_idset 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 source
The hub takes the transaction's replay id from
options.ReplaySessionwhen the transaction starts. When an error'sCaptureReplayreturns an id and the scope's transaction (sampled or not) has noreplay_id, the transaction gets a copy of its DSC with that id, as sentry-java does withBaggage.forceSetReplayId.Hub.GetBaggagereads 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
options.ReplaySession, nothing changes: the built-inReplaySessionreturns no replay fromCaptureReplay.options.ReplaySessionisn't set.#skip-changelog:
IReplaySessionandSentryOptions.ReplaySessionare internal and only sentry-unity uses them.🤖 Generated with Claude Code