-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
integration.ts
220 lines (186 loc) · 6.27 KB
/
integration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { getCurrentHub } from '@sentry/core';
import type { BrowserClientReplayOptions, Integration } from '@sentry/types';
import {
DEFAULT_ERROR_SAMPLE_RATE,
DEFAULT_FLUSH_MAX_DELAY,
DEFAULT_FLUSH_MIN_DELAY,
DEFAULT_SESSION_SAMPLE_RATE,
MASK_ALL_TEXT_SELECTOR,
} from './constants';
import { ReplayContainer } from './replay';
import type { RecordingOptions, ReplayConfiguration, ReplayPluginOptions } from './types';
import { isBrowser } from './util/isBrowser';
const MEDIA_SELECTORS = 'img,image,svg,path,rect,area,video,object,picture,embed,map,audio';
let _initialized = false;
/**
* The main replay integration class, to be passed to `init({ integrations: [] })`.
*/
export class Replay implements Integration {
/**
* @inheritDoc
*/
public static id: string = 'Replay';
/**
* @inheritDoc
*/
public name: string = Replay.id;
/**
* Options to pass to `rrweb.record()`
*/
private readonly _recordingOptions: RecordingOptions;
private readonly _options: ReplayPluginOptions;
private _replay?: ReplayContainer;
public constructor({
flushMinDelay = DEFAULT_FLUSH_MIN_DELAY,
flushMaxDelay = DEFAULT_FLUSH_MAX_DELAY,
stickySession = true,
useCompression = true,
sessionSampleRate,
errorSampleRate,
maskAllText,
maskTextSelector,
maskAllInputs = true,
blockAllMedia = true,
_experiments = {},
blockClass = 'sentry-block',
ignoreClass = 'sentry-ignore',
maskTextClass = 'sentry-mask',
blockSelector = '[data-sentry-block]',
..._recordingOptions
}: ReplayConfiguration = {}) {
this._recordingOptions = {
maskAllInputs,
blockClass,
ignoreClass,
maskTextClass,
maskTextSelector,
blockSelector,
..._recordingOptions,
};
this._options = {
flushMinDelay,
flushMaxDelay,
stickySession,
sessionSampleRate: DEFAULT_SESSION_SAMPLE_RATE,
errorSampleRate: DEFAULT_ERROR_SAMPLE_RATE,
useCompression,
maskAllText: typeof maskAllText === 'boolean' ? maskAllText : !maskTextSelector,
blockAllMedia,
_experiments,
};
if (typeof sessionSampleRate === 'number') {
// eslint-disable-next-line
console.warn(
`[Replay] You are passing \`sessionSampleRate\` to the Replay integration.
This option is deprecated and will be removed soon.
Instead, configure \`replaysSessionSampleRate\` directly in the SDK init options, e.g.:
Sentry.init({ replaysSessionSampleRate: ${sessionSampleRate} })`,
);
this._options.sessionSampleRate = sessionSampleRate;
}
if (typeof errorSampleRate === 'number') {
// eslint-disable-next-line
console.warn(
`[Replay] You are passing \`errorSampleRate\` to the Replay integration.
This option is deprecated and will be removed soon.
Instead, configure \`replaysOnErrorSampleRate\` directly in the SDK init options, e.g.:
Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`,
);
this._options.errorSampleRate = errorSampleRate;
}
if (this._options.maskAllText) {
// `maskAllText` is a more user friendly option to configure
// `maskTextSelector`. This means that all nodes will have their text
// content masked.
this._recordingOptions.maskTextSelector = MASK_ALL_TEXT_SELECTOR;
}
if (this._options.blockAllMedia) {
// `blockAllMedia` is a more user friendly option to configure blocking
// embedded media elements
this._recordingOptions.blockSelector = !this._recordingOptions.blockSelector
? MEDIA_SELECTORS
: `${this._recordingOptions.blockSelector},${MEDIA_SELECTORS}`;
}
if (this._isInitialized && isBrowser()) {
throw new Error('Multiple Sentry Session Replay instances are not supported');
}
this._isInitialized = true;
}
/** If replay has already been initialized */
protected get _isInitialized(): boolean {
return _initialized;
}
/** Update _isInitialized */
protected set _isInitialized(value: boolean) {
_initialized = value;
}
/**
* We previously used to create a transaction in `setupOnce` and it would
* potentially create a transaction before some native SDK integrations have run
* and applied their own global event processor. An example is:
* https://github.com/getsentry/sentry-javascript/blob/b47ceafbdac7f8b99093ce6023726ad4687edc48/packages/browser/src/integrations/useragent.ts
*
* So we call `replay.setup` in next event loop as a workaround to wait for other
* global event processors to finish. This is no longer needed, but keeping it
* here to avoid any future issues.
*/
public setupOnce(): void {
if (!isBrowser()) {
return;
}
this._setup();
// XXX: See method comments above
setTimeout(() => this.start());
}
/**
* Initializes the plugin.
*
* Creates or loads a session, attaches listeners to varying events (DOM,
* PerformanceObserver, Recording, Sentry SDK, etc)
*/
public start(): void {
if (!this._replay) {
return;
}
this._replay.start();
}
/**
* Currently, this needs to be manually called (e.g. for tests). Sentry SDK
* does not support a teardown
*/
public stop(): void {
if (!this._replay) {
return;
}
this._replay.stop();
}
/**
* Immediately send all pending events.
*/
public flush(): Promise<void> | void {
if (!this._replay || !this._replay.isEnabled()) {
return;
}
return this._replay.flushImmediate();
}
/** Setup the integration. */
private _setup(): void {
// Client is not available in constructor, so we need to wait until setupOnce
this._loadReplayOptionsFromClient();
this._replay = new ReplayContainer({
options: this._options,
recordingOptions: this._recordingOptions,
});
}
/** Parse Replay-related options from SDK options */
private _loadReplayOptionsFromClient(): void {
const client = getCurrentHub().getClient();
const opt = client && (client.getOptions() as BrowserClientReplayOptions | undefined);
if (opt && typeof opt.replaysSessionSampleRate === 'number') {
this._options.sessionSampleRate = opt.replaysSessionSampleRate;
}
if (opt && typeof opt.replaysOnErrorSampleRate === 'number') {
this._options.errorSampleRate = opt.replaysOnErrorSampleRate;
}
}
}