@sentry/replay 0.6.10-0 → 0.6.11-0

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/dist/index.js CHANGED
@@ -260,6 +260,11 @@ var SESSION_IDLE_DURATION = 300000; // 5 minutes in ms
260
260
  var VISIBILITY_CHANGE_TIMEOUT = SESSION_IDLE_DURATION;
261
261
  // The maximum length of a session
262
262
  var MAX_SESSION_LIFE = 1800000; // 30 minutes
263
+ /**
264
+ * Defaults for sampling rates
265
+ */
266
+ var DEFAULT_SESSION_SAMPLE_RATE = 0.1;
267
+ var DEFAULT_ERROR_SAMPLE_RATE = 1.0;
263
268
 
264
269
  /**
265
270
  * Deletes a session from storage
@@ -338,6 +343,9 @@ function saveSession(session) {
338
343
  * 0.0 = 0% sampling
339
344
  */
340
345
  function isSampled(sampleRate) {
346
+ if (sampleRate === undefined) {
347
+ return false;
348
+ }
341
349
  // Math.random() returns a number in range of 0 to 1 (inclusive of 0, but not 1)
342
350
  return Math.random() < sampleRate;
343
351
  }
@@ -345,14 +353,15 @@ function isSampled(sampleRate) {
345
353
  var Session = /** @class */ (function () {
346
354
  function Session(session, _a) {
347
355
  if (session === void 0) { session = {}; }
348
- var _b = _a === void 0 ? {} : _a, _c = _b.stickySession, stickySession = _c === void 0 ? false : _c, _d = _b.samplingRate, samplingRate = _d === void 0 ? 1.0 : _d;
349
- var _e, _f, _g, _h;
356
+ var stickySession = _a.stickySession, sessionSampleRate = _a.sessionSampleRate, errorSampleRate = _a.errorSampleRate;
357
+ var _b, _c, _d, _e;
350
358
  var now = new Date().getTime();
351
359
  this._id = session.id || utils.uuid4();
352
- this._started = (_e = session.started) !== null && _e !== void 0 ? _e : now;
353
- this._lastActivity = (_f = session.lastActivity) !== null && _f !== void 0 ? _f : now;
354
- this._segmentId = (_g = session.segmentId) !== null && _g !== void 0 ? _g : 0;
355
- this._sampled = (_h = session.sampled) !== null && _h !== void 0 ? _h : isSampled(samplingRate);
360
+ this._started = (_b = session.started) !== null && _b !== void 0 ? _b : now;
361
+ this._lastActivity = (_c = session.lastActivity) !== null && _c !== void 0 ? _c : now;
362
+ this._segmentId = (_d = session.segmentId) !== null && _d !== void 0 ? _d : 0;
363
+ this._sampled =
364
+ (_e = session.sampled) !== null && _e !== void 0 ? _e : (isSampled(sessionSampleRate) || isSampled(errorSampleRate));
356
365
  this.options = {
357
366
  stickySession: stickySession,
358
367
  };
@@ -435,8 +444,12 @@ var Session = /** @class */ (function () {
435
444
  * one of these Sentry events per "replay session".
436
445
  */
437
446
  function createSession(_a) {
438
- var _b = _a.stickySession, stickySession = _b === void 0 ? false : _b, _c = _a.samplingRate, samplingRate = _c === void 0 ? 1.0 : _c;
439
- var session = new Session(undefined, { stickySession: stickySession, samplingRate: samplingRate });
447
+ var sessionSampleRate = _a.sessionSampleRate, errorSampleRate = _a.errorSampleRate, _b = _a.stickySession, stickySession = _b === void 0 ? false : _b;
448
+ var session = new Session(undefined, {
449
+ stickySession: stickySession,
450
+ errorSampleRate: errorSampleRate,
451
+ sessionSampleRate: sessionSampleRate,
452
+ });
440
453
  logger.log("Creating new session: ".concat(session.id));
441
454
  if (stickySession) {
442
455
  saveSession(session);
@@ -444,23 +457,41 @@ function createSession(_a) {
444
457
  return session;
445
458
  }
446
459
 
460
+ /**
461
+ * Captures exceptions to Sentry only when it occurs on sentry.io
462
+ */
463
+ function captureInternalException(err) {
464
+ if (!isInternal()) {
465
+ return;
466
+ }
467
+ core.captureException(err);
468
+ }
469
+
447
470
  /**
448
471
  * Fetches a session from storage
449
472
  */
450
- function fetchSession() {
473
+ function fetchSession(_a) {
474
+ var sessionSampleRate = _a.sessionSampleRate, errorSampleRate = _a.errorSampleRate;
451
475
  var hasSessionStorage = 'sessionStorage' in window;
452
476
  if (!hasSessionStorage) {
453
477
  return null;
454
478
  }
479
+ var sessionStringFromStorage = window.sessionStorage.getItem(REPLAY_SESSION_KEY);
480
+ if (!sessionStringFromStorage) {
481
+ return null;
482
+ }
455
483
  try {
456
- return new Session(
457
- // @ts-expect-error: Type 'null' is not assignable to type 'string'.ts(2345)
458
- JSON.parse(window.sessionStorage.getItem(REPLAY_SESSION_KEY)),
484
+ var sessionObj = JSON.parse(sessionStringFromStorage);
485
+ // NOTE: This shouldn't happen
486
+ if (sessionObj.segmentId === 0) {
487
+ captureInternalException(new Error('Session storage object with segmentId = 0'));
488
+ }
489
+ return new Session(sessionObj,
459
490
  // We are assuming that if there is a saved item, then the session is sticky,
460
491
  // however this could break down if we used a different storage mechanism (e.g. localstorage)
461
- { stickySession: true });
492
+ { stickySession: true, sessionSampleRate: sessionSampleRate, errorSampleRate: errorSampleRate });
462
493
  }
463
- catch (_a) {
494
+ catch (_b) {
464
495
  return null;
465
496
  }
466
497
  }
@@ -469,9 +500,10 @@ function fetchSession() {
469
500
  * Get or create a session
470
501
  */
471
502
  function getSession(_a) {
472
- var expiry = _a.expiry, currentSession = _a.currentSession, stickySession = _a.stickySession, samplingRate = _a.samplingRate;
503
+ var expiry = _a.expiry, currentSession = _a.currentSession, stickySession = _a.stickySession, sessionSampleRate = _a.sessionSampleRate, errorSampleRate = _a.errorSampleRate;
473
504
  // If session exists and is passed, use it instead of always hitting session storage
474
- var session = currentSession || (stickySession && fetchSession());
505
+ var session = currentSession ||
506
+ (stickySession && fetchSession({ sessionSampleRate: sessionSampleRate, errorSampleRate: errorSampleRate }));
475
507
  if (session) {
476
508
  // If there is a session, check if it is valid (e.g. "last activity" time
477
509
  // should be within the "session idle time", and "session started" time is
@@ -485,7 +517,11 @@ function getSession(_a) {
485
517
  }
486
518
  // Otherwise continue to create a new session
487
519
  }
488
- var newSession = createSession({ stickySession: stickySession, samplingRate: samplingRate });
520
+ var newSession = createSession({
521
+ stickySession: stickySession,
522
+ sessionSampleRate: sessionSampleRate,
523
+ errorSampleRate: errorSampleRate,
524
+ });
489
525
  return { type: 'new', session: newSession };
490
526
  }
491
527
 
@@ -500,16 +536,6 @@ function addInternalBreadcrumb(arg) {
500
536
  core.addBreadcrumb(__assign({ category: category || 'console', level: level || 'debug', message: "[debug]: ".concat(message) }, rest));
501
537
  }
502
538
 
503
- /**
504
- * Captures exceptions to Sentry only when it occurs on sentry.io
505
- */
506
- function captureInternalException(err) {
507
- if (!isInternal()) {
508
- return;
509
- }
510
- core.captureException(err);
511
- }
512
-
513
539
  function createPayload(_a) {
514
540
  var events = _a.events, headers = _a.headers;
515
541
  var payloadWithSequence;
@@ -910,7 +936,7 @@ var Replay = /** @class */ (function () {
910
936
  function Replay(_a) {
911
937
  if (_a === void 0) { _a = {}; }
912
938
  var _this = this;
913
- var _b = _a.flushMinDelay, flushMinDelay = _b === void 0 ? 5000 : _b, _c = _a.flushMaxDelay, flushMaxDelay = _c === void 0 ? 15000 : _c, _d = _a.initialFlushDelay, initialFlushDelay = _d === void 0 ? 5000 : _d, _e = _a.stickySession, stickySession = _e === void 0 ? true : _e, _f = _a.useCompression, useCompression = _f === void 0 ? true : _f, _g = _a.captureOnlyOnError, captureOnlyOnError = _g === void 0 ? false : _g, _h = _a.replaysSamplingRate, replaysSamplingRate = _h === void 0 ? 1.0 : _h, _j = _a.maskAllText, maskAllText = _j === void 0 ? true : _j, _k = _a.maskAllInputs, maskAllInputs = _k === void 0 ? true : _k, _l = _a.blockAllMedia, blockAllMedia = _l === void 0 ? true : _l, _m = _a.blockClass, blockClass = _m === void 0 ? 'sentry-block' : _m, _o = _a.ignoreClass, ignoreClass = _o === void 0 ? 'sentry-ignore' : _o, _p = _a.maskTextClass, maskTextClass = _p === void 0 ? 'sentry-mask' : _p, _q = _a.blockSelector, blockSelector = _q === void 0 ? '[data-sentry-block]' : _q, recordingOptions = __rest(_a, ["flushMinDelay", "flushMaxDelay", "initialFlushDelay", "stickySession", "useCompression", "captureOnlyOnError", "replaysSamplingRate", "maskAllText", "maskAllInputs", "blockAllMedia", "blockClass", "ignoreClass", "maskTextClass", "blockSelector"]);
939
+ var _b = _a.flushMinDelay, flushMinDelay = _b === void 0 ? 5000 : _b, _c = _a.flushMaxDelay, flushMaxDelay = _c === void 0 ? 15000 : _c, _d = _a.initialFlushDelay, initialFlushDelay = _d === void 0 ? 5000 : _d, _e = _a.stickySession, stickySession = _e === void 0 ? true : _e, _f = _a.useCompression, useCompression = _f === void 0 ? true : _f, _g = _a.sessionSampleRate, sessionSampleRate = _g === void 0 ? DEFAULT_SESSION_SAMPLE_RATE : _g, _h = _a.errorSampleRate, errorSampleRate = _h === void 0 ? DEFAULT_ERROR_SAMPLE_RATE : _h, _j = _a.maskAllText, maskAllText = _j === void 0 ? true : _j, _k = _a.maskAllInputs, maskAllInputs = _k === void 0 ? true : _k, _l = _a.blockAllMedia, blockAllMedia = _l === void 0 ? true : _l, _m = _a.blockClass, blockClass = _m === void 0 ? 'sentry-block' : _m, _o = _a.ignoreClass, ignoreClass = _o === void 0 ? 'sentry-ignore' : _o, _p = _a.maskTextClass, maskTextClass = _p === void 0 ? 'sentry-mask' : _p, _q = _a.blockSelector, blockSelector = _q === void 0 ? '[data-sentry-block]' : _q, replaysSamplingRate = _a.replaysSamplingRate, captureOnlyOnError = _a.captureOnlyOnError, recordingOptions = __rest(_a, ["flushMinDelay", "flushMaxDelay", "initialFlushDelay", "stickySession", "useCompression", "sessionSampleRate", "errorSampleRate", "maskAllText", "maskAllInputs", "blockAllMedia", "blockClass", "ignoreClass", "maskTextClass", "blockSelector", "replaysSamplingRate", "captureOnlyOnError"]);
914
940
  /**
915
941
  * @inheritDoc
916
942
  */
@@ -922,11 +948,6 @@ var Replay = /** @class */ (function () {
922
948
  this.performanceObserver = null;
923
949
  this.retryCount = 0;
924
950
  this.retryInterval = BASE_RETRY_INTERVAL;
925
- /**
926
- * Flag when a new session has been created. Captured replay events behave
927
- * slightly differently in this case: `replay_start_timestamp` is included.
928
- */
929
- this.newSessionCreated = false;
930
951
  this.flushLock = null;
931
952
  /**
932
953
  * Timestamp of the last user activity. This lives across sessions.
@@ -937,12 +958,16 @@ var Replay = /** @class */ (function () {
937
958
  */
938
959
  this.isEnabled = false;
939
960
  /**
940
- *
941
961
  * Paused is a state where:
942
962
  * - DOM Recording is not listening at all
943
963
  * - Nothing will be added to event buffer (e.g. core SDK events)
944
964
  */
945
965
  this.isPaused = false;
966
+ /**
967
+ * Integration will wait until an error occurs before creating and sending a
968
+ * replay.
969
+ */
970
+ this.waitForError = false;
946
971
  /**
947
972
  * Have we attached listeners to the core SDK?
948
973
  * Note we have to track this as there is no way to remove instrumentation handlers.
@@ -987,13 +1012,28 @@ var Replay = /** @class */ (function () {
987
1012
  message: "Tagging event (".concat(event.event_id, ") - ").concat(event.message, " - ").concat((exc === null || exc === void 0 ? void 0 : exc.type) || 'Unknown', ": ").concat((exc === null || exc === void 0 ? void 0 : exc.value) || 'n/a'),
988
1013
  });
989
1014
  // Need to be very careful that this does not cause an infinite loop
990
- if (_this.options.captureOnlyOnError &&
1015
+ if (_this.waitForError &&
991
1016
  event.exception &&
992
- event.message !== UNABLE_TO_SEND_REPLAY // ignore this error because other we could loop indefinitely with trying to capture replay and failing
1017
+ event.message !== UNABLE_TO_SEND_REPLAY // ignore this error because otherwise we could loop indefinitely with trying to capture replay and failing
993
1018
  ) {
994
- // TODO: Do we continue to record after?
995
- // TODO: What happens if another error happens? Do we record in the same session?
996
- setTimeout(function () { return _this.flushImmediate(); });
1019
+ setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
1020
+ return __generator(this, function (_a) {
1021
+ switch (_a.label) {
1022
+ case 0: return [4 /*yield*/, this.flushImmediate()];
1023
+ case 1:
1024
+ _a.sent();
1025
+ if (this.stopRecording) {
1026
+ this.stopRecording();
1027
+ // Reset all "capture on error" configuration before
1028
+ // starting a new recording
1029
+ delete this.recordingOptions.checkoutEveryNms;
1030
+ this.waitForError = false;
1031
+ this.startRecording();
1032
+ }
1033
+ return [2 /*return*/];
1034
+ }
1035
+ });
1036
+ }); });
997
1037
  }
998
1038
  return event;
999
1039
  };
@@ -1206,21 +1246,37 @@ var Replay = /** @class */ (function () {
1206
1246
  });
1207
1247
  }); };
1208
1248
  this.recordingOptions = __assign({ maskAllInputs: maskAllInputs, blockClass: blockClass, ignoreClass: ignoreClass, maskTextClass: maskTextClass, blockSelector: blockSelector }, recordingOptions);
1249
+ var usingDeprecatedReplaysSamplingRate = replaysSamplingRate !== undefined;
1250
+ var usingDeprecatedCaptureOnlyOnError = captureOnlyOnError !== undefined;
1209
1251
  this.options = {
1210
1252
  flushMinDelay: flushMinDelay,
1211
1253
  flushMaxDelay: flushMaxDelay,
1212
1254
  stickySession: stickySession,
1213
1255
  initialFlushDelay: initialFlushDelay,
1214
- captureOnlyOnError: captureOnlyOnError,
1215
- replaysSamplingRate: replaysSamplingRate,
1256
+ sessionSampleRate: usingDeprecatedReplaysSamplingRate
1257
+ ? replaysSamplingRate
1258
+ : sessionSampleRate,
1259
+ errorSampleRate: usingDeprecatedCaptureOnlyOnError
1260
+ ? 1.0
1261
+ : errorSampleRate,
1216
1262
  useCompression: useCompression,
1217
1263
  maskAllText: maskAllText,
1218
1264
  blockAllMedia: blockAllMedia,
1219
1265
  };
1220
- // Modify rrweb options to checkoutEveryNthSecond if this is defined, as we don't know when an error occurs, so we want to try to minimize the number of events captured.
1221
- if (this.options.captureOnlyOnError) {
1266
+ // Modify recording options to checkoutEveryNthSecond if this is defined, as we
1267
+ // don't know when an error will occur, so we need to keep a buffer of replay events.
1268
+ if (this.options.errorSampleRate > 0 &&
1269
+ this.options.sessionSampleRate < 1.0) {
1222
1270
  // Checkout every minute, meaning we only get up-to one minute of events before the error happens
1223
1271
  this.recordingOptions.checkoutEveryNms = 60000;
1272
+ this.waitForError = true;
1273
+ }
1274
+ // TODO(deprecated): Maintain backwards compatibility for alpha users
1275
+ if (usingDeprecatedCaptureOnlyOnError) {
1276
+ console.warn('[@sentry/replay]: The `captureOnlyOnError` option is deprecated! Please configure `errorSampleRate` instead.');
1277
+ }
1278
+ if (usingDeprecatedReplaysSamplingRate) {
1279
+ console.warn('[@sentry/replay]: The `replaysSamplingRate` option is deprecated! Please configure `sessionSampleRate` instead.');
1224
1280
  }
1225
1281
  if (this.options.maskAllText) {
1226
1282
  // `maskAllText` is a more user friendly option to configure
@@ -1353,12 +1409,12 @@ var Replay = /** @class */ (function () {
1353
1409
  expiry: expiry,
1354
1410
  stickySession: Boolean(this.options.stickySession),
1355
1411
  currentSession: this.session,
1356
- samplingRate: this.options.replaysSamplingRate,
1412
+ sessionSampleRate: this.options.sessionSampleRate,
1413
+ errorSampleRate: this.options.errorSampleRate,
1357
1414
  }), type = _d.type, session = _d.session;
1358
1415
  // If session was newly created (i.e. was not loaded from storage), then
1359
1416
  // enable flag to create the root replay
1360
1417
  if (type === 'new') {
1361
- this.newSessionCreated = true;
1362
1418
  this.setInitialState();
1363
1419
  }
1364
1420
  if (session.id !== ((_b = this.session) === null || _b === void 0 ? void 0 : _b.id)) {
@@ -1453,11 +1509,11 @@ var Replay = /** @class */ (function () {
1453
1509
  * processing and hand back control to caller.
1454
1510
  */
1455
1511
  Replay.prototype.addUpdate = function (cb) {
1456
- // We need to always run `cb` (e.g. in the case of captureOnlyOnError == true)
1512
+ // We need to always run `cb` (e.g. in the case of `this.waitForError == true`)
1457
1513
  var cbResult = cb === null || cb === void 0 ? void 0 : cb();
1458
1514
  // If this option is turned on then we will only want to call `flush`
1459
1515
  // explicitly
1460
- if (this.options.captureOnlyOnError) {
1516
+ if (this.waitForError) {
1461
1517
  return;
1462
1518
  }
1463
1519
  // If callback is true, we do not want to continue with flushing -- the
@@ -1518,6 +1574,7 @@ var Replay = /** @class */ (function () {
1518
1574
  * Add an event to the event buffer
1519
1575
  */
1520
1576
  Replay.prototype.addEvent = function (event, isCheckout) {
1577
+ var _a;
1521
1578
  if (!this.eventBuffer) {
1522
1579
  // This implies that `isEnabled` is false
1523
1580
  return;
@@ -1539,7 +1596,7 @@ var Replay = /** @class */ (function () {
1539
1596
  }
1540
1597
  // Only record earliest event if a new session was created, otherwise it
1541
1598
  // shouldn't be relevant
1542
- if (this.newSessionCreated &&
1599
+ if (((_a = this.session) === null || _a === void 0 ? void 0 : _a.segmentId) === 0 &&
1543
1600
  (!this.context.earliestEvent ||
1544
1601
  timestampInMs < this.context.earliestEvent)) {
1545
1602
  this.context.earliestEvent = timestampInMs;
@@ -1687,10 +1744,10 @@ var Replay = /** @class */ (function () {
1687
1744
  return false;
1688
1745
  };
1689
1746
  /**
1690
- * Only flush if `captureOnlyOnError` is false.
1747
+ * Only flush if `this.waitForError` is false.
1691
1748
  */
1692
1749
  Replay.prototype.conditionalFlush = function () {
1693
- if (this.options.captureOnlyOnError) {
1750
+ if (this.waitForError) {
1694
1751
  return;
1695
1752
  }
1696
1753
  this.flushImmediate();
@@ -1734,7 +1791,7 @@ var Replay = /** @class */ (function () {
1734
1791
  Replay.prototype.runFlush = function () {
1735
1792
  var _a;
1736
1793
  return __awaiter(this, void 0, void 0, function () {
1737
- var replayId, newSessionCreated, segmentId, recordingData, err_2;
1794
+ var replayId, segmentId, recordingData, err_2;
1738
1795
  return __generator(this, function (_b) {
1739
1796
  switch (_b.label) {
1740
1797
  case 0:
@@ -1754,7 +1811,6 @@ var Replay = /** @class */ (function () {
1754
1811
  // Only attach memory event if eventBuffer is not empty
1755
1812
  _b.sent();
1756
1813
  replayId = this.session.id;
1757
- newSessionCreated = this.newSessionCreated;
1758
1814
  segmentId = this.session.segmentId++;
1759
1815
  _b.label = 3;
1760
1816
  case 3:
@@ -1766,11 +1822,10 @@ var Replay = /** @class */ (function () {
1766
1822
  replayId: replayId,
1767
1823
  events: recordingData,
1768
1824
  segmentId: segmentId,
1769
- includeReplayStartTimestamp: newSessionCreated,
1825
+ includeReplayStartTimestamp: segmentId === 0,
1770
1826
  })];
1771
1827
  case 5:
1772
1828
  _b.sent();
1773
- this.newSessionCreated = false;
1774
1829
  return [3 /*break*/, 7];
1775
1830
  case 6:
1776
1831
  err_2 = _b.sent();
@@ -1791,7 +1846,7 @@ var Replay = /** @class */ (function () {
1791
1846
  Replay.prototype.flushImmediate = function () {
1792
1847
  this.debouncedFlush();
1793
1848
  // `.flush` is provided by lodash.debounce
1794
- this.debouncedFlush.flush();
1849
+ return this.debouncedFlush.flush();
1795
1850
  };
1796
1851
  /**
1797
1852
  * Send replay attachment using `fetch()`
@@ -1815,7 +1870,7 @@ var Replay = /** @class */ (function () {
1815
1870
  timestamp = new Date().getTime();
1816
1871
  sdkInfo = {
1817
1872
  name: 'sentry.javascript.integration.replay',
1818
- version: "0.6.10-0",
1873
+ version: "0.6.11-0",
1819
1874
  };
1820
1875
  return [4 /*yield*/, new Promise(function (resolve) {
1821
1876
  var _a;