-
-
Notifications
You must be signed in to change notification settings - Fork 343
feat(replay): Allow using browserReplayIntegration without isWeb guard #4858
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,49 @@ | ||
import { replayIntegration } from '@sentry/react'; | ||
|
||
const browserReplayIntegration = ( | ||
options: Parameters<typeof replayIntegration>[0] = {}, | ||
): ReturnType<typeof replayIntegration> => { | ||
import { notWeb } from '../utils/environment'; | ||
import type { Replay } from './replayInterface'; | ||
|
||
/** | ||
* ReplayConfiguration for browser replay integration. | ||
* | ||
* See the [Configuration documentation](https://docs.sentry.io/platforms/javascript/session-replay/configuration/) for more information. | ||
*/ | ||
type ReplayConfiguration = Parameters<typeof replayIntegration>[0]; | ||
|
||
// https://github.com/getsentry/sentry-javascript/blob/e00cb04f1bbf494067cd8475d392266ba296987a/packages/replay-internal/src/integration.ts#L109 | ||
const INTEGRATION_NAME = 'Replay'; | ||
|
||
/** | ||
* Browser Replay integration for React Native. | ||
* | ||
* See the [Browser Replay documentation](https://docs.sentry.io/platforms/javascript/session-replay/) for more information. | ||
*/ | ||
const browserReplayIntegration = (options: ReplayConfiguration = {}): Replay => { | ||
if (notWeb()) { | ||
// This is required because because `replayIntegration` browser check doesn't | ||
// work for React Native. | ||
return browserReplayIntegrationNoop(); | ||
} | ||
|
||
return replayIntegration({ | ||
...options, | ||
mask: ['.sentry-react-native-mask', ...(options.mask || [])], | ||
unmask: ['.sentry-react-native-unmask:not(.sentry-react-native-mask *) > *', ...(options.unmask || [])], | ||
}); | ||
}; | ||
|
||
const browserReplayIntegrationNoop = (): Replay => { | ||
return { | ||
name: INTEGRATION_NAME, | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
start: () => {}, | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
startBuffering: () => {}, | ||
stop: () => Promise.resolve(), | ||
flush: () => Promise.resolve(), | ||
getReplayId: () => undefined, | ||
getRecordingMode: () => undefined, | ||
}; | ||
}; | ||
|
||
export { browserReplayIntegration }; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { Integration, ReplayRecordingMode } from '@sentry/core'; | ||
|
||
// Based on Replay Class https://github.com/getsentry/sentry-javascript/blob/e00cb04f1bbf494067cd8475d392266ba296987a/packages/replay-internal/src/integration.ts#L50 | ||
|
||
/** | ||
* Common interface for React Native Replay integrations. | ||
* | ||
* Both browser and mobile replay integrations should implement this interface | ||
* to allow user manually control the replay. | ||
*/ | ||
export interface Replay extends Integration { | ||
/** | ||
* Start a replay regardless of sampling rate. Calling this will always | ||
* create a new session. Will log a message if replay is already in progress. | ||
* | ||
* Creates or loads a session, attaches listeners to varying events (DOM, | ||
* PerformanceObserver, Recording, Sentry SDK, etc) | ||
*/ | ||
start(): void; | ||
|
||
/** | ||
* Start replay buffering. Buffers until `flush()` is called or, if | ||
* `replaysOnErrorSampleRate` > 0, until an error occurs. | ||
*/ | ||
startBuffering(): void; | ||
|
||
/** | ||
* Currently, this needs to be manually called (e.g. for tests). Sentry SDK | ||
* does not support a teardown | ||
*/ | ||
stop(): Promise<void>; | ||
|
||
/** | ||
* If not in "session" recording mode, flush event buffer which will create a new replay. | ||
* If replay is not enabled, a new session replay is started. | ||
* Unless `continueRecording` is false, the replay will continue to record and | ||
* behave as a "session"-based replay. | ||
* | ||
* Otherwise, queue up a flush. | ||
*/ | ||
flush(options?: { continueRecording?: boolean }): Promise<void>; | ||
|
||
/** | ||
* Get the current session ID. | ||
*/ | ||
getReplayId(): string | undefined; | ||
|
||
/** | ||
* Get the current recording mode. This can be either `session` or `buffer`. | ||
* | ||
* `session`: Recording the whole session, sending it continuously | ||
* `buffer`: Always keeping the last 60s of recording, requires: | ||
* - having replaysOnErrorSampleRate > 0 to capture replay when an error occurs | ||
* - or calling `flush()` to send the replay | ||
*/ | ||
getRecordingMode(): ReplayRecordingMode | undefined; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { describe, test } from '@jest/globals'; | ||
import * as SentryReact from '@sentry/react'; | ||
import { spyOn } from 'jest-mock'; | ||
|
||
import { browserReplayIntegration } from '../../src/js/replay/browserReplay'; | ||
import * as environment from '../../src/js/utils/environment'; | ||
|
||
describe('Browser Replay', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('should not call replayIntegration if not web', () => { | ||
spyOn(environment, 'notWeb').mockReturnValue(true); | ||
spyOn(SentryReact, 'replayIntegration').mockImplementation(() => { | ||
throw new Error('replayIntegration should not be called'); | ||
}); | ||
|
||
const integration = browserReplayIntegration(); | ||
|
||
expect(integration).toBeDefined(); | ||
expect(SentryReact.replayIntegration).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.