@sentry/replay 0.6.13 → 0.6.14-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/README.md CHANGED
@@ -79,6 +79,26 @@ replay.start(); // Start recording
79
79
  replay.stop(); // Stop recording
80
80
  ```
81
81
 
82
+ ## Sessions
83
+
84
+ A session starts when the Session Replay SDK is first loaded and initialized. The session will continue until 5 minutes passes without any user interactions[^1] with the application *OR* until a maximum of 30 minutes have elapsed. Closing the browser tab will end the session immediately according to the rules for [SessionStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage).
85
+
86
+ [^1]: An 'interaction' refers to either a mouse click or a browser navigation event.
87
+
88
+ ### Replay Captures Only on Errors
89
+
90
+ Alternatively, rather than recording an entire session, you can capture a replay only when an error occurs. In this case, the integration will buffer up to one minute worth of events prior to the error being thrown. It will continue to record the session following the rules above regarding session life and activity. Read the [sampling](#Sampling) section for configuration options.
91
+
92
+ ## Sampling
93
+
94
+ Sampling allows you to control how much of your website's traffic will result in a Session Replay. There are two sample rates you can adjust to get the replays more relevant to your interests:
95
+
96
+ - `sessionSampleRate` - The sample rate for replays that begin recording immediately and last the entirety of the user's session.
97
+ - `errorSampleRate` - The sample rate for replays that are recorded when an error happens. This type of replay will record up to a minute of events prior to the error and continue recording until the session ends.
98
+
99
+ Sampling occurs when the session is first started. `sessionSampleRate` is evaluated first. If it is sampled, then the replay recording begins. Otherwise, `errorSampleRate` is evaluated and if it is sampled, the integration will begin buffering the replay and will only upload a replay to Sentry when an error occurs. The remainder of the replay will behave similarly to a whole-session replay.
100
+
101
+
82
102
  ## Configuration
83
103
 
84
104
  ### General Configuration
@@ -2,7 +2,7 @@ import { Breadcrumb, Event, Integration } from '@sentry/types';
2
2
  import { Session } from './session/Session';
3
3
  import { ReplayPerformanceEntry } from './createPerformanceEntry';
4
4
  import { IEventBuffer } from './eventBuffer';
5
- import type { AllPerformanceEntry, InitialState, InstrumentationTypeBreadcrumb, InstrumentationTypeSpan, RecordedEvents, RecordingEvent, RecordingOptions, ReplayConfiguration, ReplayPluginOptions, SendReplayRequest } from './types';
5
+ import type { AllPerformanceEntry, InitialState, InstrumentationTypeBreadcrumb, InstrumentationTypeSpan, RecordingEvent, RecordingOptions, ReplayConfiguration, ReplayPluginOptions, SendReplay } from './types';
6
6
  /**
7
7
  * Returns true to return control to calling function, otherwise continue with normal batching
8
8
  */
@@ -278,16 +278,11 @@ export declare class Replay implements Integration {
278
278
  /**
279
279
  * Send replay attachment using `fetch()`
280
280
  */
281
- sendReplayRequest({ endpoint, events, replayId: event_id, segmentId: segment_id, includeReplayStartTimestamp, }: SendReplayRequest): Promise<void>;
281
+ sendReplayRequest({ events, replayId: event_id, segmentId: segment_id, includeReplayStartTimestamp, }: SendReplay): Promise<void | undefined>;
282
282
  resetRetries(): void;
283
283
  /**
284
284
  * Finalize and send the current replay event to Sentry
285
285
  */
286
- sendReplay({ replayId, events, segmentId, includeReplayStartTimestamp, }: {
287
- replayId: string;
288
- events: RecordedEvents;
289
- segmentId: number;
290
- includeReplayStartTimestamp: boolean;
291
- }): Promise<unknown>;
286
+ sendReplay({ replayId, events, segmentId, includeReplayStartTimestamp, }: SendReplay): Promise<unknown>;
292
287
  }
293
288
  export {};
@@ -49,6 +49,7 @@ export declare class Session {
49
49
  constructor(session: Partial<SessionObject> | undefined, { stickySession, sessionSampleRate, errorSampleRate, }: StickyOption & SampleRates);
50
50
  get id(): string;
51
51
  get started(): number;
52
+ set started(newDate: number);
52
53
  get lastActivity(): number;
53
54
  set lastActivity(newDate: number);
54
55
  get segmentId(): number;
@@ -3,8 +3,7 @@ export declare type RecordingEvent = eventWithTime;
3
3
  export declare type RecordingOptions = recordOptions<eventWithTime>;
4
4
  export declare type RecordedEvents = Uint8Array | string;
5
5
  export declare type AllPerformanceEntry = PerformancePaintTiming | PerformanceResourceTiming | PerformanceNavigationTiming;
6
- export interface SendReplayRequest {
7
- endpoint: string;
6
+ export interface SendReplay {
8
7
  events: RecordedEvents;
9
8
  replayId: string;
10
9
  segmentId: number;
package/dist/index.es.js CHANGED
@@ -1,5 +1,5 @@
1
- import { getCurrentHub, addBreadcrumb, captureException, addGlobalEventProcessor, setContext, getEnvelopeEndpointWithUrlEncodedAuth } from '@sentry/core';
2
- import { htmlTreeAsString, logger as logger$1, uuid4, browserPerformanceTimeOrigin, addInstrumentationHandler, createEnvelope, serializeEnvelope } from '@sentry/utils';
1
+ import { getCurrentHub, addBreadcrumb, captureException, addGlobalEventProcessor, setContext } from '@sentry/core';
2
+ import { htmlTreeAsString, logger as logger$1, uuid4, browserPerformanceTimeOrigin, addInstrumentationHandler, createEnvelope } from '@sentry/utils';
3
3
  import debounce from 'lodash.debounce';
4
4
  import { record, EventType } from 'rrweb';
5
5
 
@@ -373,6 +373,12 @@ var Session = /** @class */ (function () {
373
373
  get: function () {
374
374
  return this._started;
375
375
  },
376
+ set: function (newDate) {
377
+ this._started = newDate;
378
+ if (this.options.stickySession) {
379
+ saveSession(this);
380
+ }
381
+ },
376
382
  enumerable: false,
377
383
  configurable: true
378
384
  });
@@ -1011,8 +1017,17 @@ var Replay = /** @class */ (function () {
1011
1017
  setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
1012
1018
  return __generator(this, function (_a) {
1013
1019
  switch (_a.label) {
1014
- case 0: return [4 /*yield*/, this.flushImmediate()];
1020
+ case 0:
1021
+ // Allow flush to complete before resuming as a session recording, otherwise
1022
+ // the checkout from `startRecording` may be included in the payload.
1023
+ // Prefer to keep the error replay as a separate (and smaller) segment
1024
+ // than the session replay.
1025
+ return [4 /*yield*/, this.flushImmediate()];
1015
1026
  case 1:
1027
+ // Allow flush to complete before resuming as a session recording, otherwise
1028
+ // the checkout from `startRecording` may be included in the payload.
1029
+ // Prefer to keep the error replay as a separate (and smaller) segment
1030
+ // than the session replay.
1016
1031
  _a.sent();
1017
1032
  if (this.stopRecording) {
1018
1033
  this.stopRecording();
@@ -1042,6 +1057,14 @@ var Replay = /** @class */ (function () {
1042
1057
  }
1043
1058
  _this.addUpdate(function () {
1044
1059
  var _a, _b;
1060
+ // The session is always started immediately on pageload/init, but for
1061
+ // error-only replays, it should reflect the most recent checkout
1062
+ // when an error occurs. Clear any state that happens before this current
1063
+ // checkout. This needs to happen before `addEvent()` which updates state
1064
+ // dependent on this reset.
1065
+ if (_this.waitForError && event.type === 2) {
1066
+ _this.setInitialState();
1067
+ }
1045
1068
  // We need to clear existing events on a checkout, otherwise they are
1046
1069
  // incremental event updates and should be appended
1047
1070
  _this.addEvent(event, isCheckout);
@@ -1059,6 +1082,11 @@ var Replay = /** @class */ (function () {
1059
1082
  if ((_a = _this.session) === null || _a === void 0 ? void 0 : _a.previousSessionId) {
1060
1083
  return true;
1061
1084
  }
1085
+ // See note above re: session start needs to reflect the most recent
1086
+ // checkout.
1087
+ if (_this.waitForError && _this.session && _this.context.earliestEvent) {
1088
+ _this.session.started = _this.context.earliestEvent;
1089
+ }
1062
1090
  // If the full snapshot is due to an initial load, we will not have
1063
1091
  // a previous session ID. In this case, we want to buffer events
1064
1092
  // for a set amount of time before flushing. This can help avoid
@@ -1846,13 +1874,13 @@ var Replay = /** @class */ (function () {
1846
1874
  * Send replay attachment using `fetch()`
1847
1875
  */
1848
1876
  Replay.prototype.sendReplayRequest = function (_a) {
1849
- var endpoint = _a.endpoint, events = _a.events, event_id = _a.replayId, segment_id = _a.segmentId, includeReplayStartTimestamp = _a.includeReplayStartTimestamp;
1877
+ var _b;
1878
+ var events = _a.events, event_id = _a.replayId, segment_id = _a.segmentId, includeReplayStartTimestamp = _a.includeReplayStartTimestamp;
1850
1879
  return __awaiter(this, void 0, void 0, function () {
1851
- var payloadWithSequence, _b, urls, errorIds, traceIds, initialState, timestamp, sdkInfo, replayEvent, envelope, response, _c, _d;
1852
- var _e;
1880
+ var payloadWithSequence, _c, urls, errorIds, traceIds, initialState, timestamp, sdkInfo, replayEvent, envelope, client;
1853
1881
  var _this = this;
1854
- return __generator(this, function (_f) {
1855
- switch (_f.label) {
1882
+ return __generator(this, function (_d) {
1883
+ switch (_d.label) {
1856
1884
  case 0:
1857
1885
  payloadWithSequence = createPayload({
1858
1886
  events: events,
@@ -1860,11 +1888,11 @@ var Replay = /** @class */ (function () {
1860
1888
  segment_id: segment_id,
1861
1889
  },
1862
1890
  });
1863
- _b = this.popEventContext(), urls = _b.urls, errorIds = _b.errorIds, traceIds = _b.traceIds, initialState = _b.initialState;
1891
+ _c = this.popEventContext(), urls = _c.urls, errorIds = _c.errorIds, traceIds = _c.traceIds, initialState = _c.initialState;
1864
1892
  timestamp = new Date().getTime();
1865
1893
  sdkInfo = {
1866
1894
  name: 'sentry.javascript.integration.replay',
1867
- version: "0.6.13",
1895
+ version: "0.6.14-0",
1868
1896
  };
1869
1897
  return [4 /*yield*/, new Promise(function (resolve) {
1870
1898
  var _a;
@@ -1889,7 +1917,7 @@ var Replay = /** @class */ (function () {
1889
1917
  }); });
1890
1918
  })];
1891
1919
  case 1:
1892
- replayEvent = _f.sent();
1920
+ replayEvent = _d.sent();
1893
1921
  envelope = createEnvelope({
1894
1922
  event_id: event_id,
1895
1923
  sent_at: new Date().toISOString(),
@@ -1907,26 +1935,14 @@ var Replay = /** @class */ (function () {
1907
1935
  payloadWithSequence,
1908
1936
  ],
1909
1937
  ]);
1910
- // Otherwise use `fetch`, which *WILL* get cancelled on page reloads/unloads
1911
- logger.log("uploading attachment via fetch()");
1912
- return [4 /*yield*/, fetch(endpoint, {
1913
- method: 'POST',
1914
- body: serializeEnvelope(envelope),
1915
- })];
1916
- case 2:
1917
- response = _f.sent();
1918
- if (!(response.status !== 200)) return [3 /*break*/, 4];
1919
- _c = setContext;
1920
- _d = ['Send Replay Response'];
1921
- _e = {
1922
- status: response.status
1923
- };
1924
- return [4 /*yield*/, response.text()];
1925
- case 3:
1926
- _c.apply(void 0, _d.concat([(_e.body = _f.sent(),
1927
- _e)]));
1928
- throw new Error(UNABLE_TO_SEND_REPLAY);
1929
- case 4: return [2 /*return*/];
1938
+ client = getCurrentHub().getClient();
1939
+ try {
1940
+ return [2 /*return*/, (_b = client === null || client === void 0 ? void 0 : client.getTransport()) === null || _b === void 0 ? void 0 : _b.send(envelope)];
1941
+ }
1942
+ catch (_e) {
1943
+ throw new Error(UNABLE_TO_SEND_REPLAY);
1944
+ }
1945
+ return [2 /*return*/];
1930
1946
  }
1931
1947
  });
1932
1948
  });
@@ -1941,7 +1957,7 @@ var Replay = /** @class */ (function () {
1941
1957
  Replay.prototype.sendReplay = function (_a) {
1942
1958
  var replayId = _a.replayId, events = _a.events, segmentId = _a.segmentId, includeReplayStartTimestamp = _a.includeReplayStartTimestamp;
1943
1959
  return __awaiter(this, void 0, void 0, function () {
1944
- var client, endpoint, ex_1;
1960
+ var ex_1;
1945
1961
  var _this = this;
1946
1962
  return __generator(this, function (_b) {
1947
1963
  switch (_b.label) {
@@ -1950,13 +1966,10 @@ var Replay = /** @class */ (function () {
1950
1966
  if (!events.length) {
1951
1967
  return [2 /*return*/];
1952
1968
  }
1953
- client = getCurrentHub().getClient();
1954
- endpoint = getEnvelopeEndpointWithUrlEncodedAuth(client.getDsn());
1955
1969
  _b.label = 1;
1956
1970
  case 1:
1957
1971
  _b.trys.push([1, 3, , 5]);
1958
1972
  return [4 /*yield*/, this.sendReplayRequest({
1959
- endpoint: endpoint,
1960
1973
  events: events,
1961
1974
  replayId: replayId,
1962
1975
  segmentId: segmentId,
package/dist/index.js CHANGED
@@ -381,6 +381,12 @@ var Session = /** @class */ (function () {
381
381
  get: function () {
382
382
  return this._started;
383
383
  },
384
+ set: function (newDate) {
385
+ this._started = newDate;
386
+ if (this.options.stickySession) {
387
+ saveSession(this);
388
+ }
389
+ },
384
390
  enumerable: false,
385
391
  configurable: true
386
392
  });
@@ -1019,8 +1025,17 @@ var Replay = /** @class */ (function () {
1019
1025
  setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
1020
1026
  return __generator(this, function (_a) {
1021
1027
  switch (_a.label) {
1022
- case 0: return [4 /*yield*/, this.flushImmediate()];
1028
+ case 0:
1029
+ // Allow flush to complete before resuming as a session recording, otherwise
1030
+ // the checkout from `startRecording` may be included in the payload.
1031
+ // Prefer to keep the error replay as a separate (and smaller) segment
1032
+ // than the session replay.
1033
+ return [4 /*yield*/, this.flushImmediate()];
1023
1034
  case 1:
1035
+ // Allow flush to complete before resuming as a session recording, otherwise
1036
+ // the checkout from `startRecording` may be included in the payload.
1037
+ // Prefer to keep the error replay as a separate (and smaller) segment
1038
+ // than the session replay.
1024
1039
  _a.sent();
1025
1040
  if (this.stopRecording) {
1026
1041
  this.stopRecording();
@@ -1050,6 +1065,14 @@ var Replay = /** @class */ (function () {
1050
1065
  }
1051
1066
  _this.addUpdate(function () {
1052
1067
  var _a, _b;
1068
+ // The session is always started immediately on pageload/init, but for
1069
+ // error-only replays, it should reflect the most recent checkout
1070
+ // when an error occurs. Clear any state that happens before this current
1071
+ // checkout. This needs to happen before `addEvent()` which updates state
1072
+ // dependent on this reset.
1073
+ if (_this.waitForError && event.type === 2) {
1074
+ _this.setInitialState();
1075
+ }
1053
1076
  // We need to clear existing events on a checkout, otherwise they are
1054
1077
  // incremental event updates and should be appended
1055
1078
  _this.addEvent(event, isCheckout);
@@ -1067,6 +1090,11 @@ var Replay = /** @class */ (function () {
1067
1090
  if ((_a = _this.session) === null || _a === void 0 ? void 0 : _a.previousSessionId) {
1068
1091
  return true;
1069
1092
  }
1093
+ // See note above re: session start needs to reflect the most recent
1094
+ // checkout.
1095
+ if (_this.waitForError && _this.session && _this.context.earliestEvent) {
1096
+ _this.session.started = _this.context.earliestEvent;
1097
+ }
1070
1098
  // If the full snapshot is due to an initial load, we will not have
1071
1099
  // a previous session ID. In this case, we want to buffer events
1072
1100
  // for a set amount of time before flushing. This can help avoid
@@ -1854,13 +1882,13 @@ var Replay = /** @class */ (function () {
1854
1882
  * Send replay attachment using `fetch()`
1855
1883
  */
1856
1884
  Replay.prototype.sendReplayRequest = function (_a) {
1857
- var endpoint = _a.endpoint, events = _a.events, event_id = _a.replayId, segment_id = _a.segmentId, includeReplayStartTimestamp = _a.includeReplayStartTimestamp;
1885
+ var _b;
1886
+ var events = _a.events, event_id = _a.replayId, segment_id = _a.segmentId, includeReplayStartTimestamp = _a.includeReplayStartTimestamp;
1858
1887
  return __awaiter(this, void 0, void 0, function () {
1859
- var payloadWithSequence, _b, urls, errorIds, traceIds, initialState, timestamp, sdkInfo, replayEvent, envelope, response, _c, _d;
1860
- var _e;
1888
+ var payloadWithSequence, _c, urls, errorIds, traceIds, initialState, timestamp, sdkInfo, replayEvent, envelope, client;
1861
1889
  var _this = this;
1862
- return __generator(this, function (_f) {
1863
- switch (_f.label) {
1890
+ return __generator(this, function (_d) {
1891
+ switch (_d.label) {
1864
1892
  case 0:
1865
1893
  payloadWithSequence = createPayload({
1866
1894
  events: events,
@@ -1868,11 +1896,11 @@ var Replay = /** @class */ (function () {
1868
1896
  segment_id: segment_id,
1869
1897
  },
1870
1898
  });
1871
- _b = this.popEventContext(), urls = _b.urls, errorIds = _b.errorIds, traceIds = _b.traceIds, initialState = _b.initialState;
1899
+ _c = this.popEventContext(), urls = _c.urls, errorIds = _c.errorIds, traceIds = _c.traceIds, initialState = _c.initialState;
1872
1900
  timestamp = new Date().getTime();
1873
1901
  sdkInfo = {
1874
1902
  name: 'sentry.javascript.integration.replay',
1875
- version: "0.6.13",
1903
+ version: "0.6.14-0",
1876
1904
  };
1877
1905
  return [4 /*yield*/, new Promise(function (resolve) {
1878
1906
  var _a;
@@ -1897,7 +1925,7 @@ var Replay = /** @class */ (function () {
1897
1925
  }); });
1898
1926
  })];
1899
1927
  case 1:
1900
- replayEvent = _f.sent();
1928
+ replayEvent = _d.sent();
1901
1929
  envelope = utils.createEnvelope({
1902
1930
  event_id: event_id,
1903
1931
  sent_at: new Date().toISOString(),
@@ -1915,26 +1943,14 @@ var Replay = /** @class */ (function () {
1915
1943
  payloadWithSequence,
1916
1944
  ],
1917
1945
  ]);
1918
- // Otherwise use `fetch`, which *WILL* get cancelled on page reloads/unloads
1919
- logger.log("uploading attachment via fetch()");
1920
- return [4 /*yield*/, fetch(endpoint, {
1921
- method: 'POST',
1922
- body: utils.serializeEnvelope(envelope),
1923
- })];
1924
- case 2:
1925
- response = _f.sent();
1926
- if (!(response.status !== 200)) return [3 /*break*/, 4];
1927
- _c = core.setContext;
1928
- _d = ['Send Replay Response'];
1929
- _e = {
1930
- status: response.status
1931
- };
1932
- return [4 /*yield*/, response.text()];
1933
- case 3:
1934
- _c.apply(void 0, _d.concat([(_e.body = _f.sent(),
1935
- _e)]));
1936
- throw new Error(UNABLE_TO_SEND_REPLAY);
1937
- case 4: return [2 /*return*/];
1946
+ client = core.getCurrentHub().getClient();
1947
+ try {
1948
+ return [2 /*return*/, (_b = client === null || client === void 0 ? void 0 : client.getTransport()) === null || _b === void 0 ? void 0 : _b.send(envelope)];
1949
+ }
1950
+ catch (_e) {
1951
+ throw new Error(UNABLE_TO_SEND_REPLAY);
1952
+ }
1953
+ return [2 /*return*/];
1938
1954
  }
1939
1955
  });
1940
1956
  });
@@ -1949,7 +1965,7 @@ var Replay = /** @class */ (function () {
1949
1965
  Replay.prototype.sendReplay = function (_a) {
1950
1966
  var replayId = _a.replayId, events = _a.events, segmentId = _a.segmentId, includeReplayStartTimestamp = _a.includeReplayStartTimestamp;
1951
1967
  return __awaiter(this, void 0, void 0, function () {
1952
- var client, endpoint, ex_1;
1968
+ var ex_1;
1953
1969
  var _this = this;
1954
1970
  return __generator(this, function (_b) {
1955
1971
  switch (_b.label) {
@@ -1958,13 +1974,10 @@ var Replay = /** @class */ (function () {
1958
1974
  if (!events.length) {
1959
1975
  return [2 /*return*/];
1960
1976
  }
1961
- client = core.getCurrentHub().getClient();
1962
- endpoint = core.getEnvelopeEndpointWithUrlEncodedAuth(client.getDsn());
1963
1977
  _b.label = 1;
1964
1978
  case 1:
1965
1979
  _b.trys.push([1, 3, , 5]);
1966
1980
  return [4 /*yield*/, this.sendReplayRequest({
1967
- endpoint: endpoint,
1968
1981
  events: events,
1969
1982
  replayId: replayId,
1970
1983
  segmentId: segmentId,