@sentry/replay 0.2.0-0 → 0.2.0-3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -26
- package/dist/index.es.js +287 -97
- package/dist/index.js +287 -97
- package/dist/index.js.map +1 -1
- package/dist/jest.config.d.ts +3 -0
- package/dist/rollup.config.d.ts +3 -0
- package/dist/src/createPerformanceEntry.d.ts +23 -0
- package/dist/src/flags.d.ts +2 -0
- package/dist/src/index.d.ts +120 -0
- package/dist/src/session/constants.d.ts +1 -0
- package/dist/src/session/createSession.d.ts +14 -0
- package/dist/src/session/createSession.test.d.ts +1 -0
- package/dist/src/session/fetchSession.d.ts +2 -0
- package/dist/src/session/fetchSession.test.d.ts +1 -0
- package/dist/src/session/getSession.d.ts +15 -0
- package/dist/src/session/getSession.test.d.ts +1 -0
- package/dist/src/session/index.d.ts +2 -0
- package/dist/src/session/saveSession.d.ts +2 -0
- package/dist/src/session/saveSession.test.d.ts +1 -0
- package/dist/src/session/types.d.ts +7 -0
- package/dist/src/session/updateSessionActivity.d.ts +3 -0
- package/dist/src/session/updateSessionActivity.test.d.ts +1 -0
- package/dist/src/util/isExpired.d.ts +5 -0
- package/dist/src/util/isExpired.test.d.ts +1 -0
- package/dist/src/util/isSessionExpired.d.ts +5 -0
- package/dist/src/util/isSessionExpired.test.d.ts +1 -0
- package/dist/src/util/logger.d.ts +6 -0
- package/dist/tests/index.spec.d.ts +1 -0
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -13,54 +13,37 @@ To install the stable version:
|
|
|
13
13
|
with npm:
|
|
14
14
|
|
|
15
15
|
```shell
|
|
16
|
-
npm install --save @sentry/replay rrweb
|
|
16
|
+
npm install --save @sentry/browser @sentry/replay @sentry/tracing rrweb
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
with yarn:
|
|
20
20
|
|
|
21
21
|
```shell
|
|
22
|
-
yarn add @sentry/replay rrweb
|
|
22
|
+
yarn add @sentry/replay @sentry/browser @sentry/replay @sentry/tracing rrweb
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
## Setup
|
|
26
26
|
|
|
27
|
-
To set up the integration add the following to your Sentry initialization
|
|
27
|
+
To set up the integration add the following to your Sentry initialization. Several options are supported and passable via the integration constructor.
|
|
28
|
+
See the rrweb documentation for advice on configuring these values.
|
|
28
29
|
|
|
29
|
-
```javascript
|
|
30
|
-
import * as Sentry from '@sentry/browser';
|
|
31
|
-
import { SentryReplay } from '@sentry/replay';
|
|
32
|
-
|
|
33
|
-
Sentry.init({
|
|
34
|
-
dsn: '__DSN__',
|
|
35
|
-
integrations: [
|
|
36
|
-
new SentryReplay({
|
|
37
|
-
// ...options
|
|
38
|
-
}),
|
|
39
|
-
],
|
|
40
|
-
// ...
|
|
41
|
-
});
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
Several options are supported and passable via the integration constructor:
|
|
45
30
|
|
|
46
31
|
```javascript
|
|
47
32
|
import * as Sentry from '@sentry/browser';
|
|
48
33
|
import { SentryReplay } from '@sentry/replay';
|
|
34
|
+
import '@sentry/tracing';
|
|
49
35
|
|
|
50
36
|
Sentry.init({
|
|
51
37
|
dsn: '__DSN__',
|
|
52
38
|
integrations: [
|
|
53
39
|
new SentryReplay({
|
|
54
|
-
//
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
// on by default
|
|
59
|
-
maskAllInputs: false,
|
|
40
|
+
stickySession: true, // Default is false
|
|
41
|
+
rrwebConfig: {
|
|
42
|
+
maskAllInputs: false, // Default is true
|
|
43
|
+
},
|
|
60
44
|
}),
|
|
61
45
|
],
|
|
62
46
|
// ...
|
|
63
47
|
});
|
|
64
48
|
```
|
|
65
49
|
|
|
66
|
-
See the rrweb documentation for advice on configuring these values.
|
package/dist/index.es.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as Sentry from '@sentry/browser';
|
|
2
|
-
import { isDebugBuild, logger, uuid4 } from '@sentry/utils';
|
|
3
2
|
import { record } from 'rrweb';
|
|
3
|
+
import { logger as logger$1 } from '@sentry/utils';
|
|
4
4
|
|
|
5
5
|
/*! *****************************************************************************
|
|
6
6
|
Copyright (c) Microsoft Corporation.
|
|
@@ -158,12 +158,159 @@ function createLargestContentfulPaint(entry) {
|
|
|
158
158
|
};
|
|
159
159
|
}
|
|
160
160
|
|
|
161
|
-
var
|
|
161
|
+
var REPLAY_SESSION_KEY = 'sentryReplaySession';
|
|
162
|
+
var ROOT_REPLAY_NAME = 'sentry-replay';
|
|
163
|
+
var REPLAY_EVENT_NAME = 'sentry-replay-event';
|
|
164
|
+
// Grace period to keep a session when a user changes tabs or hides window
|
|
165
|
+
var VISIBILITY_CHANGE_TIMEOUT = 60000; // 1 minute
|
|
166
|
+
// The idle limit for a session
|
|
167
|
+
var SESSION_IDLE_DURATION = 900000; // 15 minutes
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Given an initial timestamp and an expiry duration, checks to see if current
|
|
171
|
+
* time should be considered as expired.
|
|
172
|
+
*/
|
|
173
|
+
function isExpired(initialTime, expiry, targetTime) {
|
|
174
|
+
if (targetTime === void 0) { targetTime = +new Date(); }
|
|
175
|
+
// Always expired if < 0
|
|
176
|
+
if (expiry < 0) {
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
// Never expires if == 0
|
|
180
|
+
if (expiry === 0) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
return initialTime + expiry <= targetTime;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Checks to see if session is expired
|
|
188
|
+
*/
|
|
189
|
+
function isSessionExpired(session, expiry, targetTime) {
|
|
190
|
+
if (targetTime === void 0) { targetTime = +new Date(); }
|
|
191
|
+
return isExpired(session.lastActivity, expiry, targetTime);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function wrapLogger(logFn) {
|
|
195
|
+
return function wrappedLog() {
|
|
196
|
+
var args = [];
|
|
197
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
198
|
+
args[_i] = arguments[_i];
|
|
199
|
+
}
|
|
200
|
+
{
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
var logger = __assign(__assign({}, logger$1), { error: wrapLogger(logger$1.error), warn: wrapLogger(logger$1.warn), log: wrapLogger(logger$1.log) });
|
|
206
|
+
|
|
207
|
+
function saveSession(session) {
|
|
208
|
+
var hasSessionStorage = 'sessionStorage' in window;
|
|
209
|
+
if (!hasSessionStorage) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
try {
|
|
213
|
+
window.sessionStorage.setItem(REPLAY_SESSION_KEY, JSON.stringify(session));
|
|
214
|
+
}
|
|
215
|
+
catch (_a) {
|
|
216
|
+
// this shouldn't happen
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Create a new session, which in its current implementation is a Sentry event
|
|
222
|
+
* that all replays will be saved to as attachments. Currently, we only expect
|
|
223
|
+
* one of these Sentry events per "replay session".
|
|
224
|
+
*/
|
|
225
|
+
function createSession(_a) {
|
|
226
|
+
var _b = _a.stickySession, stickySession = _b === void 0 ? false : _b;
|
|
227
|
+
var currentDate = new Date().getTime();
|
|
228
|
+
// Create root replay event, this is where attachments will be saved
|
|
229
|
+
var transaction = Sentry.getCurrentHub().startTransaction({
|
|
230
|
+
name: ROOT_REPLAY_NAME,
|
|
231
|
+
tags: {
|
|
232
|
+
isReplayRoot: 'yes',
|
|
233
|
+
},
|
|
234
|
+
});
|
|
235
|
+
// We have to finish the transaction to get an event ID to be able to
|
|
236
|
+
// upload an attachment for that event
|
|
237
|
+
// @ts-expect-error This returns an eventId (string), but is not typed as such
|
|
238
|
+
var id = transaction.finish();
|
|
239
|
+
logger.log("Creating new session: ".concat(id));
|
|
240
|
+
var session = {
|
|
241
|
+
id: id,
|
|
242
|
+
spanId: transaction.spanId,
|
|
243
|
+
traceId: transaction.traceId,
|
|
244
|
+
started: currentDate,
|
|
245
|
+
lastActivity: currentDate,
|
|
246
|
+
};
|
|
247
|
+
if (stickySession) {
|
|
248
|
+
saveSession(session);
|
|
249
|
+
}
|
|
250
|
+
return session;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function fetchSession() {
|
|
254
|
+
var hasSessionStorage = 'sessionStorage' in window;
|
|
255
|
+
if (!hasSessionStorage) {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
try {
|
|
259
|
+
return JSON.parse(window.sessionStorage.getItem(REPLAY_SESSION_KEY));
|
|
260
|
+
}
|
|
261
|
+
catch (_a) {
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Get or create a session
|
|
268
|
+
*/
|
|
269
|
+
function getSession(_a) {
|
|
270
|
+
var expiry = _a.expiry, stickySession = _a.stickySession;
|
|
271
|
+
var session = stickySession && fetchSession();
|
|
272
|
+
if (session) {
|
|
273
|
+
// If there is a session, check if it is valid (e.g. "last activity" time should be within the "session idle time")
|
|
274
|
+
// TODO: We should probably set a max age on this as well
|
|
275
|
+
var isExpired = isSessionExpired(session, expiry);
|
|
276
|
+
if (!isExpired) {
|
|
277
|
+
logger.log("Using existing session: ".concat(session.id));
|
|
278
|
+
return session;
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
logger.log("Session has expired");
|
|
282
|
+
}
|
|
283
|
+
// Otherwise continue to create a new session
|
|
284
|
+
}
|
|
285
|
+
var newSession = createSession({ stickySession: stickySession });
|
|
286
|
+
return newSession;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function updateSessionActivity(_a) {
|
|
290
|
+
var stickySession = _a.stickySession;
|
|
291
|
+
// Nothing to do if there are no sticky sessions
|
|
292
|
+
if (!stickySession) {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
var existingSession = fetchSession();
|
|
296
|
+
// If user manually deleted from session storage, create a new session
|
|
297
|
+
if (!existingSession) {
|
|
298
|
+
// TBD: There was an issue here where sessions weren't saving and this
|
|
299
|
+
// caused lots of transactions to be created
|
|
300
|
+
return createSession({ stickySession: stickySession });
|
|
301
|
+
}
|
|
302
|
+
var newSession = __assign(__assign({}, existingSession), { lastActivity: new Date().getTime() });
|
|
303
|
+
saveSession(newSession);
|
|
304
|
+
return newSession;
|
|
305
|
+
}
|
|
306
|
+
|
|
162
307
|
var SentryReplay = /** @class */ (function () {
|
|
163
308
|
function SentryReplay(_a) {
|
|
164
309
|
if (_a === void 0) { _a = {}; }
|
|
165
310
|
var _this = this;
|
|
166
|
-
var _b = _a.
|
|
311
|
+
var _b = _a.uploadMinDelay, uploadMinDelay = _b === void 0 ? 5000 : _b, _c = _a.uploadMaxDelay, uploadMaxDelay = _c === void 0 ? 15000 : _c, _d = _a.stickySession, stickySession = _d === void 0 ? false : _d, // TBD: Making this opt-in for now
|
|
312
|
+
_e = _a.rrwebConfig, // TBD: Making this opt-in for now
|
|
313
|
+
_f = _e === void 0 ? {} : _e, _g = _f.maskAllInputs, maskAllInputs = _g === void 0 ? true : _g, rrwebRecordOptions = __rest(_f, ["maskAllInputs"]);
|
|
167
314
|
/**
|
|
168
315
|
* @inheritDoc
|
|
169
316
|
*/
|
|
@@ -173,6 +320,11 @@ var SentryReplay = /** @class */ (function () {
|
|
|
173
320
|
*/
|
|
174
321
|
this.events = [];
|
|
175
322
|
this.performanceEvents = [];
|
|
323
|
+
/**
|
|
324
|
+
* The timestamp of the first event since the last flush.
|
|
325
|
+
* This is used to determine if the maximum allowed time has passed before we should flush events again.
|
|
326
|
+
*/
|
|
327
|
+
this.initialEventTimestampSinceFlush = null;
|
|
176
328
|
this.performanceObserver = null;
|
|
177
329
|
/**
|
|
178
330
|
* Keep a list of performance entries that will be sent with a replay
|
|
@@ -186,70 +338,45 @@ var SentryReplay = /** @class */ (function () {
|
|
|
186
338
|
* Handle when visibility of the page changes. (e.g. new tab is opened)
|
|
187
339
|
*/
|
|
188
340
|
this.handleVisibilityChange = function () {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
341
|
+
var isExpired = isSessionExpired(_this.session, VISIBILITY_CHANGE_TIMEOUT);
|
|
342
|
+
if (isExpired) {
|
|
343
|
+
if (document.visibilityState === 'visible') {
|
|
344
|
+
// If the user has come back to the page within VISIBILITY_CHANGE_TIMEOUT
|
|
345
|
+
// ms, we will re-use the existing session, otherwise create a new
|
|
346
|
+
// session
|
|
347
|
+
logger.log('Document has become active, but session has expired');
|
|
348
|
+
_this.triggerFullSnapshot();
|
|
349
|
+
}
|
|
350
|
+
// We definitely want to return if visibilityState is "visible", and I
|
|
351
|
+
// think we also want to do the same when "hidden", as there shouldn't be
|
|
352
|
+
// any action to take if user has gone idle for a long period and then
|
|
353
|
+
// comes back to hide the tab. We don't trigger a full snapshot because
|
|
354
|
+
// we don't want to start a new session as they immediately have hidden
|
|
355
|
+
// the tab.
|
|
198
356
|
return;
|
|
199
357
|
}
|
|
200
|
-
//
|
|
201
|
-
|
|
202
|
-
//
|
|
203
|
-
//
|
|
204
|
-
|
|
205
|
-
_this.
|
|
206
|
-
}
|
|
358
|
+
// Otherwise if session is not expired...
|
|
359
|
+
// Update with current timestamp as the last session activity
|
|
360
|
+
// Only updating session on visibility change to be conservative about
|
|
361
|
+
// writing to session storage. This could be changed in the future.
|
|
362
|
+
updateSessionActivity({
|
|
363
|
+
stickySession: _this.options.stickySession,
|
|
364
|
+
});
|
|
365
|
+
// Send replay when the page/tab becomes hidden. There is no reason to send
|
|
366
|
+
// replay if it becomes visible, since no actions we care about were done
|
|
367
|
+
// while it was hidden
|
|
368
|
+
if (document.visibilityState !== 'visible') {
|
|
369
|
+
_this.finishReplayEvent();
|
|
370
|
+
}
|
|
207
371
|
};
|
|
208
372
|
this.rrwebRecordOptions = __assign({ maskAllInputs: maskAllInputs }, rrwebRecordOptions);
|
|
209
|
-
|
|
210
|
-
// TBD on behavior here (e.g. should this be saved to localStorage/cookies)
|
|
211
|
-
this.replayId = uuid4();
|
|
373
|
+
this.options = { uploadMinDelay: uploadMinDelay, uploadMaxDelay: uploadMaxDelay, stickySession: stickySession };
|
|
212
374
|
this.events = [];
|
|
213
|
-
record(__assign(__assign({}, this.rrwebRecordOptions), { emit: function (event, isCheckout) {
|
|
214
|
-
// "debounce" by `idleTimeout`, how often we save replay events i.e. we
|
|
215
|
-
// will save events only if 15 seconds have elapsed since the last
|
|
216
|
-
// event
|
|
217
|
-
//
|
|
218
|
-
// TODO: We probably want to have a hard timeout where we save
|
|
219
|
-
// so that it does not grow infinitely and we never have a replay
|
|
220
|
-
// saved
|
|
221
|
-
if (_this.timeout) {
|
|
222
|
-
window.clearTimeout(_this.timeout);
|
|
223
|
-
}
|
|
224
|
-
// Always create a new Sentry event on checkouts and clear existing rrweb events
|
|
225
|
-
if (isCheckout) {
|
|
226
|
-
console.log('$$$$$ IS CHECKOUT');
|
|
227
|
-
_this.events = [event];
|
|
228
|
-
}
|
|
229
|
-
else {
|
|
230
|
-
_this.events.push(event);
|
|
231
|
-
}
|
|
232
|
-
// Set timer to send attachment to Sentry, will be cancelled if an
|
|
233
|
-
// event happens before `idleTimeout` elapses
|
|
234
|
-
_this.timeout = window.setTimeout(function () {
|
|
235
|
-
_this.isDebug &&
|
|
236
|
-
logger.log('[Replay] rrweb timeout hit, finishing replay event');
|
|
237
|
-
_this.finishReplayEvent();
|
|
238
|
-
}, idleTimeout);
|
|
239
|
-
} }));
|
|
240
|
-
this.addListeners();
|
|
241
375
|
}
|
|
242
376
|
SentryReplay.attachmentUrlFromDsn = function (dsn, eventId) {
|
|
243
377
|
var host = dsn.host, path = dsn.path, projectId = dsn.projectId, port = dsn.port, protocol = dsn.protocol, user = dsn.user;
|
|
244
378
|
return "".concat(protocol, "://").concat(host).concat(port !== '' ? ":".concat(port) : '').concat(path !== '' ? "/".concat(path) : '', "/api/").concat(projectId, "/events/").concat(eventId, "/attachments/?sentry_key=").concat(user, "&sentry_version=7&sentry_client=replay");
|
|
245
379
|
};
|
|
246
|
-
Object.defineProperty(SentryReplay.prototype, "isDebug", {
|
|
247
|
-
get: function () {
|
|
248
|
-
return isDebugBuild();
|
|
249
|
-
},
|
|
250
|
-
enumerable: false,
|
|
251
|
-
configurable: true
|
|
252
|
-
});
|
|
253
380
|
Object.defineProperty(SentryReplay.prototype, "instance", {
|
|
254
381
|
/**
|
|
255
382
|
* Get integration's instance on the current hub
|
|
@@ -262,17 +389,91 @@ var SentryReplay = /** @class */ (function () {
|
|
|
262
389
|
});
|
|
263
390
|
SentryReplay.prototype.setupOnce = function () {
|
|
264
391
|
var _this = this;
|
|
392
|
+
/**
|
|
393
|
+
* Because we create a transaction in `setupOnce`, we can potentially create a
|
|
394
|
+
* transaction before some native SDK integrations have run and applied their
|
|
395
|
+
* own global event processor. An example is:
|
|
396
|
+
* https://github.com/getsentry/sentry-javascript/blob/b47ceafbdac7f8b99093ce6023726ad4687edc48/packages/browser/src/integrations/useragent.ts
|
|
397
|
+
*
|
|
398
|
+
* So we do this as a workaround to wait for other global event processors to finish
|
|
399
|
+
*/
|
|
400
|
+
window.setTimeout(function () { return _this.setup(); });
|
|
401
|
+
};
|
|
402
|
+
SentryReplay.prototype.setup = function () {
|
|
403
|
+
var _this = this;
|
|
404
|
+
this.loadSession({ expiry: SESSION_IDLE_DURATION });
|
|
405
|
+
// If there is no session, then something bad has happened - can't continue
|
|
406
|
+
if (!this.session) {
|
|
407
|
+
throw new Error('Invalid session');
|
|
408
|
+
}
|
|
265
409
|
// Tag all (non replay) events that get sent to Sentry with the current
|
|
266
410
|
// replay ID so that we can reference them later in the UI
|
|
267
411
|
Sentry.addGlobalEventProcessor(function (event) {
|
|
268
|
-
|
|
412
|
+
// Do not apply replayId to the root transaction
|
|
413
|
+
if (event.transaction === ROOT_REPLAY_NAME) {
|
|
414
|
+
return event;
|
|
415
|
+
}
|
|
416
|
+
event.tags = __assign(__assign({}, event.tags), { replayId: _this.session.id });
|
|
269
417
|
return event;
|
|
270
418
|
});
|
|
419
|
+
record(__assign(__assign({}, this.rrwebRecordOptions), { emit: function (event, isCheckout) {
|
|
420
|
+
// We want to batch uploads of replay events. Save events only if
|
|
421
|
+
// `<uploadMinDelay>` milliseconds have elapsed since the last event
|
|
422
|
+
// *OR* if `<uploadMaxDelay>` milliseconds have elapsed.
|
|
423
|
+
var now = new Date().getTime();
|
|
424
|
+
// Timestamp of the first replay event since the last flush, this gets
|
|
425
|
+
// reset when we finish the replay event
|
|
426
|
+
if (!_this.initialEventTimestampSinceFlush) {
|
|
427
|
+
_this.initialEventTimestampSinceFlush = now;
|
|
428
|
+
}
|
|
429
|
+
var uploadMaxDelayExceeded = isExpired(_this.initialEventTimestampSinceFlush, _this.options.uploadMaxDelay, now);
|
|
430
|
+
// Do not finish the replay event if we receive a new replay event
|
|
431
|
+
// unless `<uploadMaxDelay>` ms have elapsed since the last time we
|
|
432
|
+
// finished the replay
|
|
433
|
+
if (_this.timeout && !uploadMaxDelayExceeded) {
|
|
434
|
+
window.clearTimeout(_this.timeout);
|
|
435
|
+
}
|
|
436
|
+
// We need to clear existing events on a checkout, otherwise they are
|
|
437
|
+
// incremental event updates and should be appended
|
|
438
|
+
if (isCheckout) {
|
|
439
|
+
_this.events = [event];
|
|
440
|
+
}
|
|
441
|
+
else {
|
|
442
|
+
_this.events.push(event);
|
|
443
|
+
}
|
|
444
|
+
// This event type is a fullsnapshot, we should save immediately when this occurs
|
|
445
|
+
// See https://github.com/rrweb-io/rrweb/blob/d8f9290ca496712aa1e7d472549480c4e7876594/packages/rrweb/src/types.ts#L16
|
|
446
|
+
if (event.type === 2) {
|
|
447
|
+
// A fullsnapshot happens on initial load and if we need to start a
|
|
448
|
+
// new replay due to idle timeout. In the later case we will need to
|
|
449
|
+
// create a new session before finishing the replay.
|
|
450
|
+
_this.loadSession({ expiry: SESSION_IDLE_DURATION });
|
|
451
|
+
_this.finishReplayEvent();
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
// Set timer to finish replay event and send replay attachment to
|
|
455
|
+
// Sentry. Will be cancelled if an event happens before `uploadMinDelay`
|
|
456
|
+
// elapses.
|
|
457
|
+
_this.timeout = window.setTimeout(function () {
|
|
458
|
+
logger.log('rrweb timeout hit, finishing replay event');
|
|
459
|
+
_this.finishReplayEvent();
|
|
460
|
+
}, _this.options.uploadMinDelay);
|
|
461
|
+
} }));
|
|
462
|
+
this.addListeners();
|
|
271
463
|
// XXX: this needs to be in `setupOnce` vs `constructor`, otherwise SDK is
|
|
272
464
|
// not fully initialized and the event will not get properly sent to Sentry
|
|
273
|
-
this.createRootEvent();
|
|
274
465
|
this.createReplayEvent();
|
|
275
466
|
};
|
|
467
|
+
/**
|
|
468
|
+
* Loads a session from storage, or creates a new one
|
|
469
|
+
*/
|
|
470
|
+
SentryReplay.prototype.loadSession = function (_a) {
|
|
471
|
+
var expiry = _a.expiry;
|
|
472
|
+
this.session = getSession({
|
|
473
|
+
expiry: expiry,
|
|
474
|
+
stickySession: this.options.stickySession,
|
|
475
|
+
});
|
|
476
|
+
};
|
|
276
477
|
SentryReplay.prototype.addListeners = function () {
|
|
277
478
|
document.addEventListener('visibilitychange', this.handleVisibilityChange);
|
|
278
479
|
if ('PerformanceObserver' in window) {
|
|
@@ -284,36 +485,13 @@ var SentryReplay = /** @class */ (function () {
|
|
|
284
485
|
}
|
|
285
486
|
};
|
|
286
487
|
/**
|
|
287
|
-
*
|
|
288
|
-
*
|
|
488
|
+
* Trigger rrweb to take a full snapshot which will cause this plugin to
|
|
489
|
+
* create a new Replay event.
|
|
289
490
|
*/
|
|
290
|
-
SentryReplay.prototype.
|
|
291
|
-
|
|
491
|
+
SentryReplay.prototype.triggerFullSnapshot = function () {
|
|
492
|
+
logger.log('Taking full rrweb snapshot');
|
|
292
493
|
record.takeFullSnapshot(true);
|
|
293
494
|
};
|
|
294
|
-
/**
|
|
295
|
-
* Creates the Sentry event that all replays will be saved to as attachments.
|
|
296
|
-
* Currently, we only expect one of these per "replay session" (which is not
|
|
297
|
-
* explicitly defined yet).
|
|
298
|
-
*/
|
|
299
|
-
SentryReplay.prototype.createRootEvent = function () {
|
|
300
|
-
// TODO: Figure out if we need to do this, when this gets called from `setupOnce`, `this.instance` is still undefined.
|
|
301
|
-
// if (!this.instance) return;
|
|
302
|
-
this.isDebug && logger.log("[Replay] creating root replay event");
|
|
303
|
-
// Create a transaction to attach event to
|
|
304
|
-
var transaction = Sentry.getCurrentHub().startTransaction({
|
|
305
|
-
name: 'sentry-replay',
|
|
306
|
-
tags: {
|
|
307
|
-
hasReplay: 'yes',
|
|
308
|
-
replayId: this.replayId,
|
|
309
|
-
},
|
|
310
|
-
});
|
|
311
|
-
// We have to finish the transaction to get an event ID to be able to
|
|
312
|
-
// upload an attachment for that event
|
|
313
|
-
// @ts-expect-error This returns an eventId (string), but is not typed as such
|
|
314
|
-
this.eventId = transaction.finish();
|
|
315
|
-
return this.eventId;
|
|
316
|
-
};
|
|
317
495
|
/**
|
|
318
496
|
* This is our pseudo replay event disguised as a transaction. It will be
|
|
319
497
|
* used to store performance entries and breadcrumbs for every incremental
|
|
@@ -321,12 +499,14 @@ var SentryReplay = /** @class */ (function () {
|
|
|
321
499
|
**/
|
|
322
500
|
SentryReplay.prototype.createReplayEvent = function () {
|
|
323
501
|
var _this = this;
|
|
324
|
-
|
|
502
|
+
logger.log('CreateReplayEvent rootReplayId', this.session.id);
|
|
503
|
+
console.error(new Error('creating replay event'), new Date());
|
|
325
504
|
this.replayEvent = Sentry.startTransaction({
|
|
326
|
-
name:
|
|
505
|
+
name: REPLAY_EVENT_NAME,
|
|
506
|
+
parentSpanId: this.session.spanId,
|
|
507
|
+
traceId: this.session.traceId,
|
|
327
508
|
tags: {
|
|
328
|
-
|
|
329
|
-
replayId: this.replayId,
|
|
509
|
+
replayId: this.session.id,
|
|
330
510
|
},
|
|
331
511
|
});
|
|
332
512
|
Sentry.configureScope(function (scope) { return scope.setSpan(_this.replayEvent); });
|
|
@@ -365,12 +545,22 @@ var SentryReplay = /** @class */ (function () {
|
|
|
365
545
|
SentryReplay.prototype.finishReplayEvent = function () {
|
|
366
546
|
// if (!this.instance) return;
|
|
367
547
|
var _a;
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
548
|
+
// Ensure that our existing session has not expired
|
|
549
|
+
var isExpired = isSessionExpired(this.session, SESSION_IDLE_DURATION);
|
|
550
|
+
if (isExpired) {
|
|
551
|
+
// TBD: If it is expired, we do not send any events...we could send to
|
|
552
|
+
// the expired session, but not sure if that's great
|
|
553
|
+
console.error(new Error('Attempting to finish replay event after session expired.'));
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
if (!this.session.id) {
|
|
557
|
+
console.error(new Error('[Sentry]: No transaction, no replay'));
|
|
371
558
|
return;
|
|
372
559
|
}
|
|
373
|
-
this.sendReplay(
|
|
560
|
+
this.sendReplay(this.session.id);
|
|
561
|
+
this.initialEventTimestampSinceFlush = null;
|
|
562
|
+
// TBD: Alternatively we could update this after every rrweb event
|
|
563
|
+
this.session.lastActivity = new Date().getTime();
|
|
374
564
|
// include performance entries
|
|
375
565
|
this.addPerformanceEntries();
|
|
376
566
|
// Close out existing replay event and create a new one
|
|
@@ -397,17 +587,17 @@ var SentryReplay = /** @class */ (function () {
|
|
|
397
587
|
formData.append('rrweb', new Blob([stringifiedPayload], {
|
|
398
588
|
type: 'application/json',
|
|
399
589
|
}), "rrweb-".concat(new Date().getTime(), ".json"));
|
|
590
|
+
console.log('sendReplayRequest: ', endpoint);
|
|
400
591
|
// If sendBeacon is supported and payload is smol enough...
|
|
401
592
|
if (this.hasSendBeacon() && stringifiedPayload.length <= 65536) {
|
|
402
|
-
|
|
403
|
-
logger.log("[Replay] uploading attachment via sendBeacon()");
|
|
593
|
+
logger.log("uploading attachment via sendBeacon()");
|
|
404
594
|
window.navigator.sendBeacon(endpoint, formData);
|
|
405
595
|
return [2 /*return*/];
|
|
406
596
|
}
|
|
407
597
|
_a.label = 1;
|
|
408
598
|
case 1:
|
|
409
599
|
_a.trys.push([1, 3, , 4]);
|
|
410
|
-
|
|
600
|
+
logger.log("uploading attachment via fetch()");
|
|
411
601
|
// Otherwise use `fetch`, which *WILL* get cancelled on page reloads/unloads
|
|
412
602
|
return [4 /*yield*/, fetch(endpoint, {
|
|
413
603
|
method: 'POST',
|