Skip to content

Commit eeae961

Browse files
committed
run replay in afterAllSetup
1 parent acef6e5 commit eeae961

File tree

6 files changed

+31
-48
lines changed

6 files changed

+31
-48
lines changed

packages/replay-internal/src/integration.ts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { getClient, parseSampleRate } from '@sentry/core';
2-
import type { BrowserClientReplayOptions, Integration, IntegrationFn } from '@sentry/types';
1+
import { parseSampleRate } from '@sentry/core';
2+
import type { BrowserClientReplayOptions, Client, Integration, IntegrationFn } from '@sentry/types';
33
import { consoleSandbox, dropUndefinedKeys, isBrowser } from '@sentry/utils';
44

55
import {
@@ -215,13 +215,13 @@ export class Replay implements Integration {
215215
/**
216216
* Setup and initialize replay container
217217
*/
218-
public setupOnce(): void {
219-
if (!isBrowser()) {
218+
public afterAllSetup(client: Client): void {
219+
if (!isBrowser() || this._replay) {
220220
return;
221221
}
222222

223-
this._setup();
224-
this._initialize();
223+
this._setup(client);
224+
this._initialize(client);
225225
}
226226

227227
/**
@@ -292,24 +292,19 @@ export class Replay implements Integration {
292292
/**
293293
* Initializes replay.
294294
*/
295-
protected _initialize(): void {
295+
protected _initialize(client: Client): void {
296296
if (!this._replay) {
297297
return;
298298
}
299299

300-
// We have to run this in _initialize, because this runs in setTimeout
301-
// So when this runs all integrations have been added
302-
// Before this, we cannot access integrations on the client,
303-
// so we need to mutate the options here
304-
this._maybeLoadFromReplayCanvasIntegration();
305-
300+
this._maybeLoadFromReplayCanvasIntegration(client);
306301
this._replay.initializeSampling();
307302
}
308303

309304
/** Setup the integration. */
310-
private _setup(): void {
305+
private _setup(client: Client): void {
311306
// Client is not available in constructor, so we need to wait until setupOnce
312-
const finalOptions = loadReplayOptionsFromClient(this._initialOptions);
307+
const finalOptions = loadReplayOptionsFromClient(this._initialOptions, client);
313308

314309
this._replay = new ReplayContainer({
315310
options: finalOptions,
@@ -318,12 +313,11 @@ export class Replay implements Integration {
318313
}
319314

320315
/** Get canvas options from ReplayCanvas integration, if it is also added. */
321-
private _maybeLoadFromReplayCanvasIntegration(): void {
316+
private _maybeLoadFromReplayCanvasIntegration(client: Client): void {
322317
// To save bundle size, we skip checking for stuff here
323318
// and instead just try-catch everything - as generally this should all be defined
324319
/* eslint-disable @typescript-eslint/no-non-null-assertion */
325320
try {
326-
const client = getClient()!;
327321
const canvasIntegration = client.getIntegrationByName('ReplayCanvas') as Integration & {
328322
getOptions(): ReplayCanvasIntegrationOptions;
329323
};
@@ -340,24 +334,15 @@ export class Replay implements Integration {
340334
}
341335

342336
/** Parse Replay-related options from SDK options */
343-
function loadReplayOptionsFromClient(initialOptions: InitialReplayPluginOptions): ReplayPluginOptions {
344-
const client = getClient();
345-
const opt = client && (client.getOptions() as BrowserClientReplayOptions);
337+
function loadReplayOptionsFromClient(initialOptions: InitialReplayPluginOptions, client: Client): ReplayPluginOptions {
338+
const opt = client.getOptions() as BrowserClientReplayOptions;
346339

347340
const finalOptions: ReplayPluginOptions = {
348341
sessionSampleRate: 0,
349342
errorSampleRate: 0,
350343
...dropUndefinedKeys(initialOptions),
351344
};
352345

353-
if (!opt) {
354-
consoleSandbox(() => {
355-
// eslint-disable-next-line no-console
356-
console.warn('SDK client is not available.');
357-
});
358-
return finalOptions;
359-
}
360-
361346
const replaysSessionSampleRate = parseSampleRate(opt.replaysSessionSampleRate);
362347
const replaysOnErrorSampleRate = parseSampleRate(opt.replaysOnErrorSampleRate);
363348

packages/replay-internal/test/integration/coreHandlers/handleGlobalEvent.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getClient } from '@sentry/core';
12
import type { Event } from '@sentry/types';
23

34
import { REPLAY_EVENT_NAME, SESSION_IDLE_EXPIRE_DURATION } from '../../../src/constants';
@@ -133,8 +134,8 @@ describe('Integration | coreHandlers | handleGlobalEvent', () => {
133134

134135
it('tags errors and transactions with replay id for session samples', async () => {
135136
const { replay, integration } = await resetSdkMock({});
136-
// @ts-expect-error protected but ok to use for testing
137-
integration._initialize();
137+
integration['_initialize'](getClient()!);
138+
138139
const transaction = Transaction();
139140
const error = Error();
140141
expect(handleGlobalEventListener(replay)(transaction, {})).toEqual(

packages/replay-internal/test/integration/errorSampleRate.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ describe('Integration | errorSampleRate', () => {
866866
},
867867
autoStart: false,
868868
});
869-
integration['_initialize']();
869+
integration['_initialize'](getClient()!);
870870

871871
expect(replay.recordingMode).toBe('session');
872872
const sessionId = replay.getSessionId();
@@ -899,7 +899,7 @@ describe('Integration | errorSampleRate', () => {
899899
},
900900
autoStart: false,
901901
});
902-
integration['_initialize']();
902+
integration['_initialize'](getClient()!);
903903

904904
vi.runAllTimers();
905905

@@ -943,7 +943,7 @@ describe('Integration | errorSampleRate', () => {
943943
},
944944
autoStart: false,
945945
});
946-
integration['_initialize']();
946+
integration['_initialize'](getClient()!);
947947
const optionsEvent = createOptionsEvent(replay);
948948

949949
const TEST_EVENT = getTestEventIncremental({ timestamp: BASE_TIMESTAMP });

packages/replay-internal/test/integration/sampling.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getClient } from '@sentry/core';
12
import { resetSdkMock } from '../mocks/resetSdkMock';
23
import { useFakeTimers } from '../utils/use-fake-timers';
34

@@ -57,8 +58,7 @@ describe('Integration | sampling', () => {
5758
// @ts-expect-error private API
5859
const spyAddListeners = vi.spyOn(replay, '_addListeners');
5960

60-
// @ts-expect-error protected
61-
integration._initialize();
61+
integration['_initialize'](getClient()!);
6262

6363
vi.runAllTimers();
6464

packages/replay-internal/test/mocks/mockSdk.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,8 @@ export async function mockSdk({ replayOptions, sentryOptions, autoStart = true }
6161
_initialized = value;
6262
}
6363

64-
public setupOnce(): void {
65-
// do nothing
66-
}
67-
68-
public initialize(): void {
69-
return super._initialize();
64+
public afterAllSetup(): void {
65+
// do nothing, we need to manually initialize this
7066
}
7167
}
7268

@@ -76,7 +72,7 @@ export async function mockSdk({ replayOptions, sentryOptions, autoStart = true }
7672
...replayOptions,
7773
});
7874

79-
init({
75+
const client = init({
8076
...getDefaultClientOptions(),
8177
dsn: 'https://[email protected]/1',
8278
autoSessionTracking: false,
@@ -86,14 +82,14 @@ export async function mockSdk({ replayOptions, sentryOptions, autoStart = true }
8682
replaysOnErrorSampleRate: 0.0,
8783
...sentryOptions,
8884
integrations: [replayIntegration],
89-
});
85+
})!;
9086

91-
// Instead of `setupOnce`, which is tricky to test, we call this manually here
92-
replayIntegration['_setup']();
87+
// Instead of `afterAllSetup`, which is tricky to test, we call this manually here
88+
replayIntegration['_setup'](client);
9389

9490
if (autoStart) {
9591
// Only exists in our mock
96-
replayIntegration.initialize();
92+
replayIntegration['_initialize'](client);
9793
}
9894

9995
const replay = replayIntegration['_replay']!;

packages/replay-internal/test/utils/TestClient.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BaseClient, createTransport, initAndBind } from '@sentry/core';
22
import type {
33
BrowserClientReplayOptions,
4+
Client,
45
ClientOptions,
56
Event,
67
ParameterizedString,
@@ -33,8 +34,8 @@ export class TestClient extends BaseClient<TestClientOptions> {
3334
}
3435
}
3536

36-
export function init(options: TestClientOptions): void {
37-
initAndBind(TestClient, options);
37+
export function init(options: TestClientOptions): Client {
38+
return initAndBind(TestClient, options);
3839
}
3940

4041
export function getDefaultClientOptions(options: Partial<TestClientOptions> = {}): ClientOptions {

0 commit comments

Comments
 (0)