Skip to content
  • Sponsor getsentry/sentry-javascript

  • Notifications You must be signed in to change notification settings
  • Fork 1.7k

[WIP] feat(replay): Add experimental dropRepetitiveMutations #15438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/replay-internal/src/constants.ts
Original file line number Diff line number Diff line change
@@ -53,3 +53,6 @@ export const MAX_REPLAY_DURATION = 3_600_000; // 60 minutes in ms;

/** Default attributes to be ignored when `maskAllText` is enabled */
export const DEFAULT_IGNORED_ATTRIBUTES = ['title', 'placeholder'];

// Time window in which to check for repeated DOM mutations
export const MUTATION_DEBOUNCE_TIME = 100; // ms
66 changes: 66 additions & 0 deletions packages/replay-internal/src/replay.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import {
SLOW_CLICK_SCROLL_TIMEOUT,
SLOW_CLICK_THRESHOLD,
WINDOW,
MUTATION_DEBOUNCE_TIME,
} from './constants';
import { ClickDetector } from './coreHandlers/handleClick';
import { handleKeyboardEvent } from './coreHandlers/handleKeyboardEvent';
@@ -169,6 +170,17 @@ export class ReplayContainer implements ReplayContainerInterface {
/** Ensure page remains active when a key is pressed. */
private _handleKeyboardEvent: (event: KeyboardEvent) => void;

/**
* Map to track the history for DOM node mutations
*/
private _lastMutationMap: WeakMap<
Node,
{
timestamp: number;
fingerprint: string;
}
>;

public constructor({
options,
recordingOptions,
@@ -272,6 +284,10 @@ export class ReplayContainer implements ReplayContainerInterface {
this._handleKeyboardEvent = (event: KeyboardEvent) => {
handleKeyboardEvent(this, event);
};

if (options._experiments.dropRepetitiveMutations) {
this._lastMutationMap = new WeakMap();
}
}

/** Get the event context. */
@@ -1303,10 +1319,60 @@ export class ReplayContainer implements ReplayContainerInterface {
}
}

/**
* Heuristically create an identifier for a mutation record.
* This is used for checking on repeated mutations on the same target.
*/
private _getMutationFingerprint(mutation: MutationRecord): string {
if (mutation.type === 'attributes') {
return `attr:${mutation.attributeName}`;
}
// For other mutation types, return empty string
// TODO: Should be extended to handle other mutation types
return '';
}

/** Handler for rrweb.record.onMutation */
private _onMutationHandler(mutations: unknown[]): boolean {
const count = mutations.length;

if (this._options._experiments.dropRepetitiveMutations) {
const now = Date.now();

// Filter out repeated mutations
const uniqueMutations = (mutations as MutationRecord[]).filter(mutation => {
const target = mutation.target;
const lastMutation = this._lastMutationMap.get(target);

// Create a fingerprint of this mutation
const fingerprint = this._getMutationFingerprint(mutation);

// Check if this is a repeated mutation within our debounce window
if (
fingerprint &&
lastMutation &&
lastMutation.fingerprint === fingerprint &&
now - lastMutation.timestamp < MUTATION_DEBOUNCE_TIME
) {
return false; // Skip this mutation
}

// Update mutation tracking for this target
this._lastMutationMap.set(target, {
timestamp: now,
fingerprint,
});

return true;
});

// All mutations are repetitions, do not process in rrweb
if (uniqueMutations.length === 0) {
// todo: maybe create a new breadcrumb here?
return false;
}
}

const mutationLimit = this._options.mutationLimit;
const mutationBreadcrumbLimit = this._options.mutationBreadcrumbLimit;
const overMutationLimit = mutationLimit && count > mutationLimit;
1 change: 1 addition & 0 deletions packages/replay-internal/src/types/replay.ts
Original file line number Diff line number Diff line change
@@ -230,6 +230,7 @@ export interface ReplayPluginOptions extends ReplayNetworkOptions {
traceInternals: boolean;
continuousCheckout: number;
autoFlushOnFeedback: boolean;
dropRepetitiveMutations: boolean;
}>;
}