-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat(replay): Add ReplayCanvas
integration
#9826
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
</head> | ||
<body> | ||
<button onclick="console.log('Test log')">Click me</button> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
window.Sentry = Sentry; | ||
window.Replay = new Sentry.Replay({ | ||
flushMinDelay: 200, | ||
flushMaxDelay: 200, | ||
minReplayDuration: 0, | ||
}); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
sampleRate: 0, | ||
replaysSessionSampleRate: 1.0, | ||
replaysOnErrorSampleRate: 0.0, | ||
debug: true, | ||
|
||
integrations: [new Sentry.ReplayCanvas(), window.Replay], | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { expect } from '@playwright/test'; | ||
|
||
import { sentryTest } from '../../../../utils/fixtures'; | ||
import { getReplaySnapshot, shouldSkipReplayTest } from '../../../../utils/replayHelpers'; | ||
|
||
sentryTest('sets up canvas when adding ReplayCanvas integration first', async ({ getLocalTestUrl, page }) => { | ||
if (shouldSkipReplayTest()) { | ||
sentryTest.skip(); | ||
} | ||
|
||
await page.route('https://dsn.ingest.sentry.io/**/*', route => { | ||
return route.fulfill({ | ||
status: 200, | ||
contentType: 'application/json', | ||
body: JSON.stringify({ id: 'test-id' }), | ||
}); | ||
}); | ||
|
||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
await page.goto(url); | ||
|
||
const replay = await getReplaySnapshot(page); | ||
const canvasOptions = replay._options._experiments?.canvas; | ||
expect(canvasOptions.fps).toBe(4); | ||
expect(canvasOptions.quality).toBe(0.6); | ||
expect(replay._hasCanvas).toBe(true); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
window.Sentry = Sentry; | ||
window.Replay = new Sentry.Replay({ | ||
flushMinDelay: 200, | ||
flushMaxDelay: 200, | ||
minReplayDuration: 0, | ||
}); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
sampleRate: 0, | ||
replaysSessionSampleRate: 1.0, | ||
replaysOnErrorSampleRate: 0.0, | ||
debug: true, | ||
|
||
integrations: [window.Replay, new Sentry.ReplayCanvas()], | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { expect } from '@playwright/test'; | ||
|
||
import { sentryTest } from '../../../../utils/fixtures'; | ||
import { getReplaySnapshot, shouldSkipReplayTest } from '../../../../utils/replayHelpers'; | ||
|
||
sentryTest('sets up canvas when adding ReplayCanvas integration after Replay', async ({ getLocalTestUrl, page }) => { | ||
if (shouldSkipReplayTest()) { | ||
sentryTest.skip(); | ||
} | ||
|
||
await page.route('https://dsn.ingest.sentry.io/**/*', route => { | ||
return route.fulfill({ | ||
status: 200, | ||
contentType: 'application/json', | ||
body: JSON.stringify({ id: 'test-id' }), | ||
}); | ||
}); | ||
|
||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
await page.goto(url); | ||
|
||
const replay = await getReplaySnapshot(page); | ||
const canvasOptions = replay._options._experiments?.canvas; | ||
expect(canvasOptions.fps).toBe(4); | ||
expect(canvasOptions.quality).toBe(0.6); | ||
expect(replay._hasCanvas).toBe(true); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
window.Sentry = Sentry; | ||
window.Replay = new Sentry.Replay({ | ||
flushMinDelay: 200, | ||
flushMaxDelay: 200, | ||
minReplayDuration: 0, | ||
}); | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
sampleRate: 0, | ||
replaysSessionSampleRate: 1.0, | ||
replaysOnErrorSampleRate: 0.0, | ||
debug: true, | ||
|
||
integrations: [window.Replay], | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { expect } from '@playwright/test'; | ||
|
||
import { sentryTest } from '../../../../utils/fixtures'; | ||
import { getReplaySnapshot, shouldSkipReplayTest } from '../../../../utils/replayHelpers'; | ||
|
||
sentryTest('does not setup up canvas without ReplayCanvas integration', async ({ getLocalTestUrl, page }) => { | ||
if (shouldSkipReplayTest()) { | ||
sentryTest.skip(); | ||
} | ||
|
||
await page.route('https://dsn.ingest.sentry.io/**/*', route => { | ||
return route.fulfill({ | ||
status: 200, | ||
contentType: 'application/json', | ||
body: JSON.stringify({ id: 'test-id' }), | ||
}); | ||
}); | ||
|
||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
||
await page.goto(url); | ||
|
||
const replay = await getReplaySnapshot(page); | ||
const canvasOptions = replay._options._experiments?.canvas; | ||
expect(canvasOptions).toBe(undefined); | ||
expect(replay._hasCanvas).toBe(false); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { getCanvasManager } from '@sentry-internal/rrweb'; | ||
import type { Integration } from '@sentry/types'; | ||
import type { ReplayConfiguration } from './types'; | ||
|
||
interface ReplayCanvasOptions { | ||
quality: 'low' | 'medium' | 'high'; | ||
} | ||
|
||
/** An integration to add canvas recording to replay. */ | ||
export class ReplayCanvas implements Integration { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public static id: string = 'ReplayCanvas'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public name: string; | ||
|
||
private _canvasOptions: ReplayCanvasOptions; | ||
|
||
public constructor(options?: Partial<ReplayCanvasOptions>) { | ||
this.name = ReplayCanvas.id; | ||
this._canvasOptions = { | ||
quality: (options && options.quality) || 'medium', | ||
}; | ||
} | ||
|
||
/** @inheritdoc */ | ||
public setupOnce(): void { | ||
// noop | ||
} | ||
|
||
/** | ||
* Get the options that should be merged into replay options. | ||
* This is what is actually called by the Replay integration to setup canvas. | ||
*/ | ||
public getOptions(): Partial<ReplayConfiguration> { | ||
return { | ||
_experiments: { | ||
canvas: { | ||
...this._canvasOptions, | ||
manager: getCanvasManager, | ||
quality: this._canvasOptions.quality, | ||
}, | ||
}, | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { Replay } from './integration'; | ||
export { ReplayCanvas } from './canvas'; | ||
|
||
export type { | ||
ReplayEventType, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -318,6 +318,7 @@ Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`, | |
|
||
return this._replay.getSessionId(); | ||
} | ||
|
||
/** | ||
* Initializes replay. | ||
*/ | ||
|
@@ -326,6 +327,12 @@ Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`, | |
return; | ||
} | ||
|
||
// We have to run this in _initialize, because this runs in setTimeout | ||
// So when this runs all integrations have been added | ||
// Before this, we cannot access integrations on the client, | ||
// so we need to mutate the options here | ||
this._maybeLoadFromReplayCanvasIntegration(); | ||
|
||
this._replay.initializeSampling(); | ||
} | ||
|
||
|
@@ -339,6 +346,40 @@ Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`, | |
recordingOptions: this._recordingOptions, | ||
}); | ||
} | ||
|
||
/** Get canvas options from ReplayCanvas integration, if it is also added. */ | ||
private _maybeLoadFromReplayCanvasIntegration(): void { | ||
// If already defined, skip this... | ||
if (this._initialOptions._experiments.canvas) { | ||
return; | ||
} | ||
|
||
// To save bundle size, we skip checking for stuff here | ||
// and instead just try-catch everything - as generally this should all be defined | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
try { | ||
const client = getClient()!; | ||
const canvasIntegration = client.getIntegrationById!('ReplayCanvas') as Integration & { | ||
getOptions(): Partial<ReplayConfiguration>; | ||
}; | ||
if (!canvasIntegration) { | ||
return; | ||
} | ||
const additionalOptions = canvasIntegration.getOptions(); | ||
|
||
const mergedExperimentsOptions = { | ||
...this._initialOptions._experiments, | ||
...additionalOptions._experiments, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we excluding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh wait, this is just a property this should be safe, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, this is excluded from being mangled! as we rely on this for some things 😅 |
||
}; | ||
|
||
this._initialOptions._experiments = mergedExperimentsOptions; | ||
|
||
this._replay!.getOptions()._experiments = mergedExperimentsOptions; | ||
} catch { | ||
// ignore errors here | ||
} | ||
/* eslint-enable @typescript-eslint/no-non-null-assertion */ | ||
} | ||
} | ||
|
||
/** Parse Replay-related options from SDK options */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh it's for this, right? Only if the integration is used, the canvas manager will be added to the bundle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, exactly - basically wrapping this, but providing a nicer way to add this than to ask people to pass some method into the replay config etc.
Also, it helps with CDN bundles, where this otherwise also very hard/impossible to solve 😬
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will we have any issues creating this bundle because rrweb is devDep?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should not be the case, but something is not quite right yet here 😅 so still need to look into it a bit more!