@sentry/replay 0.6.10 → 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);
@@ -457,7 +470,8 @@ function captureInternalException(err) {
457
470
  /**
458
471
  * Fetches a session from storage
459
472
  */
460
- function fetchSession() {
473
+ function fetchSession(_a) {
474
+ var sessionSampleRate = _a.sessionSampleRate, errorSampleRate = _a.errorSampleRate;
461
475
  var hasSessionStorage = 'sessionStorage' in window;
462
476
  if (!hasSessionStorage) {
463
477
  return null;
@@ -475,9 +489,9 @@ function fetchSession() {
475
489
  return new Session(sessionObj,
476
490
  // We are assuming that if there is a saved item, then the session is sticky,
477
491
  // however this could break down if we used a different storage mechanism (e.g. localstorage)
478
- { stickySession: true });
492
+ { stickySession: true, sessionSampleRate: sessionSampleRate, errorSampleRate: errorSampleRate });
479
493
  }
480
- catch (_a) {
494
+ catch (_b) {
481
495
  return null;
482
496
  }
483
497
  }
@@ -486,9 +500,10 @@ function fetchSession() {
486
500
  * Get or create a session
487
501
  */
488
502
  function getSession(_a) {
489
- 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;
490
504
  // If session exists and is passed, use it instead of always hitting session storage
491
- var session = currentSession || (stickySession && fetchSession());
505
+ var session = currentSession ||
506
+ (stickySession && fetchSession({ sessionSampleRate: sessionSampleRate, errorSampleRate: errorSampleRate }));
492
507
  if (session) {
493
508
  // If there is a session, check if it is valid (e.g. "last activity" time
494
509
  // should be within the "session idle time", and "session started" time is
@@ -502,7 +517,11 @@ function getSession(_a) {
502
517
  }
503
518
  // Otherwise continue to create a new session
504
519
  }
505
- var newSession = createSession({ stickySession: stickySession, samplingRate: samplingRate });
520
+ var newSession = createSession({
521
+ stickySession: stickySession,
522
+ sessionSampleRate: sessionSampleRate,
523
+ errorSampleRate: errorSampleRate,
524
+ });
506
525
  return { type: 'new', session: newSession };
507
526
  }
508
527
 
@@ -917,7 +936,7 @@ var Replay = /** @class */ (function () {
917
936
  function Replay(_a) {
918
937
  if (_a === void 0) { _a = {}; }
919
938
  var _this = this;
920
- 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"]);
921
940
  /**
922
941
  * @inheritDoc
923
942
  */
@@ -939,12 +958,16 @@ var Replay = /** @class */ (function () {
939
958
  */
940
959
  this.isEnabled = false;
941
960
  /**
942
- *
943
961
  * Paused is a state where:
944
962
  * - DOM Recording is not listening at all
945
963
  * - Nothing will be added to event buffer (e.g. core SDK events)
946
964
  */
947
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;
948
971
  /**
949
972
  * Have we attached listeners to the core SDK?
950
973
  * Note we have to track this as there is no way to remove instrumentation handlers.
@@ -989,13 +1012,28 @@ var Replay = /** @class */ (function () {
989
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'),
990
1013
  });
991
1014
  // Need to be very careful that this does not cause an infinite loop
992
- if (_this.options.captureOnlyOnError &&
1015
+ if (_this.waitForError &&
993
1016
  event.exception &&
994
- 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
995
1018
  ) {
996
- // TODO: Do we continue to record after?
997
- // TODO: What happens if another error happens? Do we record in the same session?
998
- 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
+ }); });
999
1037
  }
1000
1038
  return event;
1001
1039
  };
@@ -1208,21 +1246,37 @@ var Replay = /** @class */ (function () {
1208
1246
  });
1209
1247
  }); };
1210
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;
1211
1251
  this.options = {
1212
1252
  flushMinDelay: flushMinDelay,
1213
1253
  flushMaxDelay: flushMaxDelay,
1214
1254
  stickySession: stickySession,
1215
1255
  initialFlushDelay: initialFlushDelay,
1216
- captureOnlyOnError: captureOnlyOnError,
1217
- replaysSamplingRate: replaysSamplingRate,
1256
+ sessionSampleRate: usingDeprecatedReplaysSamplingRate
1257
+ ? replaysSamplingRate
1258
+ : sessionSampleRate,
1259
+ errorSampleRate: usingDeprecatedCaptureOnlyOnError
1260
+ ? 1.0
1261
+ : errorSampleRate,
1218
1262
  useCompression: useCompression,
1219
1263
  maskAllText: maskAllText,
1220
1264
  blockAllMedia: blockAllMedia,
1221
1265
  };
1222
- // 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.
1223
- 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) {
1224
1270
  // Checkout every minute, meaning we only get up-to one minute of events before the error happens
1225
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.');
1226
1280
  }
1227
1281
  if (this.options.maskAllText) {
1228
1282
  // `maskAllText` is a more user friendly option to configure
@@ -1355,7 +1409,8 @@ var Replay = /** @class */ (function () {
1355
1409
  expiry: expiry,
1356
1410
  stickySession: Boolean(this.options.stickySession),
1357
1411
  currentSession: this.session,
1358
- samplingRate: this.options.replaysSamplingRate,
1412
+ sessionSampleRate: this.options.sessionSampleRate,
1413
+ errorSampleRate: this.options.errorSampleRate,
1359
1414
  }), type = _d.type, session = _d.session;
1360
1415
  // If session was newly created (i.e. was not loaded from storage), then
1361
1416
  // enable flag to create the root replay
@@ -1454,11 +1509,11 @@ var Replay = /** @class */ (function () {
1454
1509
  * processing and hand back control to caller.
1455
1510
  */
1456
1511
  Replay.prototype.addUpdate = function (cb) {
1457
- // 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`)
1458
1513
  var cbResult = cb === null || cb === void 0 ? void 0 : cb();
1459
1514
  // If this option is turned on then we will only want to call `flush`
1460
1515
  // explicitly
1461
- if (this.options.captureOnlyOnError) {
1516
+ if (this.waitForError) {
1462
1517
  return;
1463
1518
  }
1464
1519
  // If callback is true, we do not want to continue with flushing -- the
@@ -1689,10 +1744,10 @@ var Replay = /** @class */ (function () {
1689
1744
  return false;
1690
1745
  };
1691
1746
  /**
1692
- * Only flush if `captureOnlyOnError` is false.
1747
+ * Only flush if `this.waitForError` is false.
1693
1748
  */
1694
1749
  Replay.prototype.conditionalFlush = function () {
1695
- if (this.options.captureOnlyOnError) {
1750
+ if (this.waitForError) {
1696
1751
  return;
1697
1752
  }
1698
1753
  this.flushImmediate();
@@ -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",
1873
+ version: "0.6.11-0",
1819
1874
  };
1820
1875
  return [4 /*yield*/, new Promise(function (resolve) {
1821
1876
  var _a;