Repository navigation
Add experimental session replay on Android - #2948
Draft
bitsandfoxes wants to merge 26 commits into
Draft
bitsandfoxes wants to merge 26 commits into
bitsandfoxes wants to merge 26 commits into
Conversation
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.
Contributor
|
…e window size before layout
1 of 4 tasks
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.
…e window size before layout
…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
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.
…ersion from sentry-java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-javasubmodule points at that PR's branch, so this PR can't merge until #6829 merges and the submodule moves to sentry-java'smain.How
Replay is turned on in the Sentry window's Advanced tab, in the Experimental section, under Session Replay:
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 source
BuildAndroidSDKbuilds and shipssentry-android-replay-release.aar.kotlin-stdlib, only when replay is on in the options used at build time. With replay off, the Gradle project is unchanged.kotlin-stdlibversion isn't hardcoded.BuildAndroidSDKreads it from sentry-android-replay's POM and ships it askotlin-stdlib.versionnext to the AARs, and the Gradle line reads it from there. A sentry-java bump updates it.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.AsyncGPUReadback.onFramecall, 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.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.IReplaySession, and Unity provides its Android side:captureReplayruns for each error event afterBeforeSendand sampling, and appliesOnErrorSampleRate.app.lifecyclebreadcrumbs, and it tracks sessions when auto session tracking is on.Why Unity captures
Pixel 6 (Android 16), session replay at 1 frame/s, about 60 s per mode, same scene:
Sample events
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 itsapp.lifecyclebreadcrumbs come from Unity.Caveats