Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(replay): clamp down to 15000 mutations in second (#90945)

Hayne Ryu32294ca6ce3b
diff --git a/frontend/src/scenes/session-recordings/player/sessionRecordingDataCoordinatorLogic.test.ts b/frontend/src/scenes/session-recordings/player/sessionRecordingDataCoordinatorLogic.test.ts
index f521223a51b3..233fadc6f456 100644
--- a/frontend/src/scenes/session-recordings/player/sessionRecordingDataCoordinatorLogic.test.ts
+++ b/frontend/src/scenes/session-recordings/player/sessionRecordingDataCoordinatorLogic.test.ts
@@ -129,29 +129,29 @@ describe('sessionRecordingDataCoordinatorLogic', () => {
.toMatchValues({ recordingTooLargeToPlay: false })
})
- const mutationSnapshots = (eventCount: number, addsPerEvent: number): RecordingSnapshot[] =>
+ const mutationSnapshots = (eventCount: number, addsPerEvent: number, gapMs: number = 1): RecordingSnapshot[] =>
Array.from(
{ length: eventCount },
(_, i) =>
({
windowId: '1',
- timestamp: 1000 + i,
+ timestamp: 1000 + i * gapMs,
type: 3,
data: { source: 0, adds: new Array(addsPerEvent).fill({}) },
}) as unknown as RecordingSnapshot
)
it.each([
- ['enough giant mutations', mutationSnapshots(3, 2000), true, true],
- ['too few giant mutations', mutationSnapshots(2, 5000), true, false],
- ['many ordinary mutations', mutationSnapshots(100, 100), true, false],
+ ['a concentrated burst of adds', mutationSnapshots(10, 5000), true, true],
+ ['the same adds spread over minutes', mutationSnapshots(10, 5000, 30_000), true, false],
+ ['a single large render', mutationSnapshots(2, 5000), true, false],
[
'malformed mutations without adds',
- mutationSnapshots(3, 0).map((s) => ({ ...s, data: { source: 0 } }) as unknown as RecordingSnapshot),
+ mutationSnapshots(10, 0).map((s) => ({ ...s, data: { source: 0 } }) as unknown as RecordingSnapshot),
true,
false,
],
- ['the flag is disabled', mutationSnapshots(3, 2000), false, false],
+ ['the flag is disabled', mutationSnapshots(10, 5000), false, false],
])('detects oversized mutations with %s', (_name, snapshots, flagEnabled, expected) => {
featureFlagLogic.mount()
featureFlagLogic.actions.setFeatureFlags(
diff --git a/frontend/src/scenes/session-recordings/player/sessionRecordingDataCoordinatorLogic.ts b/frontend/src/scenes/session-recordings/player/sessionRecordingDataCoordinatorLogic.ts
index 89c1597f3e95..ab178f1fa8f7 100644
--- a/frontend/src/scenes/session-recordings/player/sessionRecordingDataCoordinatorLogic.ts
+++ b/frontend/src/scenes/session-recordings/player/sessionRecordingDataCoordinatorLogic.ts
@@ -62,8 +62,9 @@ import { createSegments, mapSnapshotsToWindowId } from './utils/segmenter'
// mutations); most large recordings are ordinary small events and play fine, so both must trip
export const OVERSIZED_RECORDING_AUTOLOAD_LIMIT_BYTES = 30 * 1024 * 1024
export const OVERSIZED_RECORDING_AVG_EVENT_BYTES = 100 * 1024
-export const OVERSIZED_MUTATION_MIN_ADDED_NODES = 2000
-export const OVERSIZED_MUTATION_EVENT_COUNT = 3
+// Adds concentrated in one playback second freeze the tab; the same adds spread out play fine
+export const OVERSIZED_MUTATION_WINDOW_MS = 1000
+export const OVERSIZED_MUTATION_WINDOW_ADDED_NODES = 15000
export interface SessionRecordingDataCoordinatorLogicProps {
sessionRecordingId: SessionRecordingId
@@ -701,26 +702,34 @@ export const sessionRecordingDataCoordinatorLogic = kea<sessionRecordingDataCoor
},
],
- // Catches recordings the metadata gate cannot see: ClickHouse only stores
- // sum and count, so a short burst of giant mutations hides under a small average
hasOversizedMutations: [
(s) => [s.snapshots, s.featureFlags],
(snapshots: RecordingSnapshot[], featureFlags: FeatureFlagsSet): boolean => {
if (!featureFlags[FEATURE_FLAGS.REPLAY_OVERSIZED_RECORDING_GATE]) {
return false
}
- let oversizedEvents = 0
+ const mutations: { timestamp: number; adds: number }[] = []
for (const snapshot of snapshots) {
if (
snapshot.type === EventType.IncrementalSnapshot &&
snapshot.data?.source === IncrementalSource.Mutation &&
Array.isArray(snapshot.data.adds) &&
- snapshot.data.adds.length >= OVERSIZED_MUTATION_MIN_ADDED_NODES
+ snapshot.data.adds.length > 0
) {
- oversizedEvents += 1
- if (oversizedEvents >= OVERSIZED_MUTATION_EVENT_COUNT) {
- return true
- }
+ mutations.push({ timestamp: snapshot.timestamp, adds: snapshot.data.adds.length })
+ }
+ }
+ // processedSnapshots are sorted by timestamp
+ let lo = 0
+ let windowAdds = 0
+ for (const mutation of mutations) {
+ windowAdds += mutation.adds
+ while (mutation.timestamp - mutations[lo].timestamp > OVERSIZED_MUTATION_WINDOW_MS) {
+ windowAdds -= mutations[lo].adds
+ lo += 1
+ }
+ if (windowAdds >= OVERSIZED_MUTATION_WINDOW_ADDED_NODES) {
+ return true
}
}
return false
diff --git a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts
index 7e622a0d0a3f..832d0a43caf8 100644
--- a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts
+++ b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts
@@ -2112,6 +2112,10 @@ export const sessionRecordingPlayerLogic = kea<sessionRecordingPlayerLogicType>(
actions.tryInitReplayer()
},
tryInitReplayer: () => {
+ if (values.hasOversizedMutations) {
+ actions.setPlayer(null)
+ return
+ }
// Tries to initialize a new player
const windowId = values.segmentForTimestamp(values.currentTimestamp)?.windowId
@@ -2492,6 +2496,10 @@ export const sessionRecordingPlayerLogic = kea<sessionRecordingPlayerLogicType>(
}
},
syncSnapshotsWithPlayer: async (_, breakpoint) => {
+ // Never feed the replayer events it cannot survive applying
+ if (values.hasOversizedMutations) {
+ return
+ }
// On loading more of the recording, trigger some state changes
const currentEvents = values.player?.replayer?.service.state.context.events ?? []
const eventsToAdd: eventWithTime[] = []
@@ -2616,7 +2624,6 @@ export const sessionRecordingPlayerLogic = kea<sessionRecordingPlayerLogicType>(
actions.retrySnapshotLoading()
},
setPlay: () => {
- // Clicking or pressing space on the "unable to play" card must not start the load anyway
if (values.recordingTooLargeToPlay || values.hasOversizedMutations) {
return
}