Skip to content

Add experimental session replay on Android - #2948

Draft
bitsandfoxes wants to merge 26 commits into
spire/upgrade-sentry-unity-to-sentry-dotnet-7from
spire/session-replay-for-unity
Draft

bitsandfoxes wants to merge 26 commits into
spire/upgrade-sentry-unity-to-sentry-dotnet-7from
spire/session-replay-for-unity

Conversation

@bitsandfoxes

@bitsandfoxes bitsandfoxes commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Unity games can't record session replays. sentry-java's replay isn't bundled, and C# exceptions go through the .NET SDK, so they never get a replay. This PR adds experimental, opt-in session replay for Android games, including replays for C# errors. It is off by default.

This PR is stacked on #2967 (sentry-dotnet 7.x), which brings the .NET SDK's replay hook.

It also depends on getsentry/sentry-java#6829, which adds internal replay hooks for hybrid SDKs. The modules/sentry-java submodule points at that PR's branch, so this PR can't merge until #6829 merges and the submodule moves to sentry-java's main.

How

Replay is turned on in the Sentry window's Advanced tab, in the Experimental section, under Session Replay:

  • "Session Sample Rate" and "On Error Sample Rate" set how often a replay is recorded. Replay is on when either rate is above 0.
  • "Quality" sets the image quality.

Code can override these through options.Experimental.SessionReplay. Both the build and the runtime read them.

Replay works only with runtime initialization. With build-time or external initialization, the SDK logs that replay is ignored.

Diagram

Diagram source
flowchart LR
    A([Game starts with replay on]) --> B{Sampled for session?}
    B -->|yes| C[Record the whole session]
    B -->|no| D[Buffer the last 30 s]
    D --> E{C# or Java error, sampled?}
    E -->|no| D
    E -->|yes| F[Send the replay, linked to the error]:::hot
    C --> G{Back after a pause?}
    F --> G
    G -->|short absence| H([Same replay continues])
    G -->|longer than session interval| I([Replay ends at the pause, a new one starts])
  • Packaging:
    • BuildAndroidSDK builds and ships sentry-android-replay-release.aar.
    • The exported Gradle project adds it, plus kotlin-stdlib, only when replay is on in the options used at build time. With replay off, the Gradle project is unchanged.
    • The kotlin-stdlib version isn't hardcoded. BuildAndroidSDK reads it from sentry-android-replay's POM and ships it as kotlin-stdlib.version next to the AARs, and the Gradle line reads it from there. A sentry-java bump updates it.
  • Unity captures the frames; sentry-java encodes and uploads them:
    • In the options callback, Unity sets a recorder factory on sentry-java's replay (setRecorderFactory). When the replay registers, it asks the factory for its recorder and gets Unity's. The factory also receives the replay's frame receiver.
    • At the end of a frame, the recorder captures into an RGBA texture at the size sentry-java asks for, and reads it back with AsyncGPUReadback.
    • The SDK's existing scope sync thread hands each frame's RGBA bytes to sentry-java in one onFrame call, and sentry-java builds the bitmap on its own replay thread. Unity's main thread only captures and copies, and the recorder starts no thread of its own. At most one frame waits to be handed over.
    • sentry-java gets the real window size, so recorded touches line up when the game renders below the screen's resolution.
    • sentry-java never captures the frames itself. On a device without async GPU readback (SystemInfo.supportsAsyncGPUReadback), or when Unity's recorder can't be installed, replay doesn't start and the SDK logs an error. Errors and sessions are reported as usual.
  • Replays for C# errors: sentry-java sends a buffered replay only for errors it captures itself. The .NET SDK (7.1) has an internal replay hook, IReplaySession, and Unity provides its Android side:
    • sentry-java's captureReplay runs for each error event after BeforeSend and sampling, and applies OnErrorSampleRate.
    • Each event carries the active replay id in its trace header. While a replay is only buffered and hasn't been sent, events carry no replay id.
  • Background and foreground:
    • Unity's own pause and resume events drive the replay. The SDK turns off sentry-java's app lifecycle breadcrumbs at runtime, so sentry-java doesn't watch the app's lifecycle. Unity's lifecycle integration adds the app.lifecycle breadcrumbs, and it tracks sessions when auto session tracking is on.
    • A pause uses sentry-java's manual pause, so a network change while the game is away doesn't resume recording.
    • No segment covers the time away.
  • ProGuard: keep rules cover the classes that string-based JNI and the Java proxies reach, including sentry-java's recorder factory and frame receiver.
  • Sample: Unity of Bugs sends a replay with each error on Android ("On Error Sample Rate" is 1 in its options asset).

Why Unity captures

Pixel 6 (Android 16), session replay at 1 frame/s, about 60 s per mode, same scene:

Mode Avg frame App CPU Memory (PSS)
Replay off 16.7–17.1 ms ~80% 270 MB
sentry-java captures 16.7 ms ~88% 316 MB
Unity captures 16.6–16.7 ms ~82% 320 MB
  • Both modes keep the frame rate.
  • Unity capture costs about a quarter of the extra CPU that sentry-java's capture costs.
  • Each captured frame costs about 0.55–0.65 ms on Unity's main thread: about 0.12 ms to capture and 0.4–0.5 ms to copy the readback. Handing it to sentry-java takes 3–4 ms on the scope sync thread.

Sample events

  • Unhandled C# exception from Unity of Bugs on a Pixel 6: recorded through sentry-java's recorder factory and onFrame. The event carries the replay context, and its replay is linked to it. The game went to the background for 30 s before the error, and its app.lifecycle breadcrumbs come from Unity.

Caveats

  • An error replay keeps recording while the game stays open, up to the 60-minute cap, as the replay spec describes.
  • Nothing is masked yet.
  • These haven't been run on a device yet:
    • a minified release build, to exercise the keep rules;
    • a device without async GPU readback, where replay stays off.

Session replay is set from code through options.Experimental.SessionReplay
and is off unless a sample rate is above 0. The Android SDK records the
game, capturing Unity's SurfaceView. Builds with replay enabled ship the
sentry-android-replay AAR and the Kotlin standard library in the
unityLibrary dependencies; without it, the generated Gradle is unchanged.

Errors captured in C# get the replay of the moments before them: Unity
supplies the .NET SDK's replay session, which asks the Android SDK to
capture the replay for each error event and puts its id on the event's
dynamic sampling context. Unity builds don't run androidx's process
lifecycle, so Unity's pause and resume pause and resume the replay.

Replay requires runtime initialization of the Android SDK. The sample
enables it for errors and picks the mode from a launch extra.
With options.Experimental.SessionReplay.Capture set to Unity, the Android
SDK's replay records through a Recorder that Unity implements: sentry-java
calls it on Android's UI thread, Unity's main thread captures the screen at
end of frame at the replay's frame rate and reads it back from the GPU, and
a worker thread hands each frame to the replay as a bitmap. The frame size
comes from sentry-java, which Unity asks for after every start or reset.

The sample spins a cube above its UI on Android, so replays show motion,
and logs frame timings under the ReplayBenchmark tag. A launch extra picks
the capture mode.
…window, refresh replay ids

ProGuard keeps ICurrentDateProvider, Integration and Function1, the classes
in the JNI signatures Unity capture looks up, so minified builds still find
the ReplayIntegration constructor and addIntegration.

Unity capture gives the replay the activity window's size instead of
Unity's screen size, which can be a lower render resolution: the replay
scales recorded touches, in window pixels, by the config it computes.

sentry-dotnet gives events without an exception the replay that's active
when they're sent, instead of the replay cached with the propagation
context's dynamic sampling context.
Without androidx's process lifecycle, Unity's pause and resume drive the
replay. The pause is now the replay's manual pause, so sentry-java no
longer resumes it in the background when the device reconnects, which
recorded the time away. Resuming moves the replay's timeline to the
return, so no segment covers the time away. After a background longer
than the session interval, the resumed replay ends right away, without a
segment for the time away, and a new one starts. Unity capture drops
frames read back or handed over after the pause.
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor
Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 5d0b37d

bitsandfoxes and others added 10 commits November 30, 2026 10:47
Session replay is set from code through options.Experimental.SessionReplay
and is off unless a sample rate is above 0. The Android SDK records the
game, capturing Unity's SurfaceView. Builds with replay enabled ship the
sentry-android-replay AAR and the Kotlin standard library in the
unityLibrary dependencies; without it, the generated Gradle is unchanged.

Errors captured in C# get the replay of the moments before them: Unity
supplies the .NET SDK's replay session, which asks the Android SDK to
capture the replay for each error event and puts its id on the event's
dynamic sampling context. Unity builds don't run androidx's process
lifecycle, so Unity's pause and resume pause and resume the replay.

Replay requires runtime initialization of the Android SDK. The sample
enables it for errors and picks the mode from a launch extra.
With options.Experimental.SessionReplay.Capture set to Unity, the Android
SDK's replay records through a Recorder that Unity implements: sentry-java
calls it on Android's UI thread, Unity's main thread captures the screen at
end of frame at the replay's frame rate and reads it back from the GPU, and
a worker thread hands each frame to the replay as a bitmap. The frame size
comes from sentry-java, which Unity asks for after every start or reset.

The sample spins a cube above its UI on Android, so replays show motion,
and logs frame timings under the ReplayBenchmark tag. A launch extra picks
the capture mode.
…window, refresh replay ids

ProGuard keeps ICurrentDateProvider, Integration and Function1, the classes
in the JNI signatures Unity capture looks up, so minified builds still find
the ReplayIntegration constructor and addIntegration.

Unity capture gives the replay the activity window's size instead of
Unity's screen size, which can be a lower render resolution: the replay
scales recorded touches, in window pixels, by the config it computes.

sentry-dotnet gives events without an exception the replay that's active
when they're sent, instead of the replay cached with the propagation
context's dynamic sampling context.
Without androidx's process lifecycle, Unity's pause and resume drive the
replay. The pause is now the replay's manual pause, so sentry-java no
longer resumes it in the background when the device reconnects, which
recorded the time away. Resuming moves the replay's timeline to the
return, so no segment covers the time away. After a background longer
than the session interval, the resumed replay ends right away, without a
segment for the time away, and a new one starts. Unity capture drops
frames read back or handed over after the pause.
…n the sample

Unity always captures the frames. Without async GPU readback, or when
Unity's recorder can't be installed, the Android SDK captures them
instead. The Capture option, the replay benchmark and its launch
arguments are removed, and the sample sends a replay with each error
on Android.
…java's integration

A failed install then leaves sentry-java's replay integration in place,
so the fallback to sentry-java capture records the game.
@bitsandfoxes
bitsandfoxes changed the base branch from main to spire/upgrade-sentry-unity-to-sentry-dotnet-7 November 30, 2026 12:01
…ntry-dotnet-7' into spire/session-replay-for-unity
Without async GPU readback, or when Unity's recorder fails to install,
the SDK logs an error and leaves the replay's sample rates unset, so the
Android SDK never starts a replay. Errors and sessions are reported as
usual. The Android SDK no longer captures Unity's SurfaceView itself.
…ntry-dotnet-7' into spire/session-replay-for-unity
- SessionReplayOptions replaces ExperimentalSessionReplayOptions; the
  experimental marker stays on options.Experimental.SessionReplay.
- The recorder queues each frame's hand-over on SentryJava's scope sync
  thread instead of its own, and times its work with timestamps.
- Unity always drives the replay's pause and resume: the Android SDK's
  app lifecycle breadcrumbs are off at runtime too, so it doesn't watch
  the app's lifecycle.
Unity no longer looks up ProcessLifecycleOwner or Lifecycle through JNI.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant