@sentry/replay 0.5.12 → 0.5.15

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.
@@ -1,6 +1,5 @@
1
1
  import { Breadcrumb, Event, Integration } from '@sentry/types';
2
- import { CaptureReplayParams } from './api/captureReplay';
3
- import { CaptureReplayUpdateParams } from './api/captureReplayUpdate';
2
+ import { CaptureReplayEventParams } from './api/captureReplayEvent';
4
3
  import { Session } from './session/Session';
5
4
  import { ReplayPerformanceEntry } from './createPerformanceEntry';
6
5
  import { IEventBuffer } from './eventBuffer';
@@ -42,12 +41,10 @@ export declare class SentryReplay implements Integration {
42
41
  private retryCount;
43
42
  private retryInterval;
44
43
  /**
45
- * Flag to make sure we only create a replay event when
46
- * necessary (i.e. we only want to have a single replay
47
- * event per session and it should only be created
48
- * immediately before sending recording)
44
+ * Flag when a new session has been created. Captured replay events behave
45
+ * slightly differently in this case: `replay_start_timestamp` is included.
49
46
  */
50
- private needsCaptureReplay;
47
+ private newSessionCreated;
51
48
  /**
52
49
  * Is the integration currently active?
53
50
  */
@@ -217,7 +214,7 @@ export declare class SentryReplay implements Integration {
217
214
  */
218
215
  popEventContext({ timestamp, }?: {
219
216
  timestamp?: number;
220
- }): CaptureReplayParams & CaptureReplayUpdateParams;
217
+ }): CaptureReplayEventParams;
221
218
  /**
222
219
  * Flushes replay event buffer to Sentry.
223
220
  *
@@ -228,11 +225,11 @@ export declare class SentryReplay implements Integration {
228
225
  /**
229
226
  * Send replay attachment using `fetch()`
230
227
  */
231
- sendReplayRequest({ endpoint, events }: ReplayRequest): Promise<void>;
228
+ sendReplayRequest({ endpoint, events, replayId: event_id, segmentId: segment_id, }: ReplayRequest): Promise<void>;
232
229
  resetRetries(): void;
233
230
  /**
234
231
  * Finalize and send the current replay event to Sentry
235
232
  */
236
- sendReplay(eventId: string, events: RecordedEvents): Promise<boolean | undefined>;
233
+ sendReplay(replayId: string, events: RecordedEvents, segmentId: number): Promise<true | undefined>;
237
234
  }
238
235
  export {};
@@ -1,5 +1,5 @@
1
1
  export declare const REPLAY_SESSION_KEY = "sentryReplaySession";
2
2
  export declare const REPLAY_EVENT_NAME = "replay_event";
3
3
  export declare const RECORDING_EVENT_NAME = "replay_recording";
4
- export declare const SESSION_IDLE_DURATION = 900000;
5
- export declare const VISIBILITY_CHANGE_TIMEOUT = 900000;
4
+ export declare const SESSION_IDLE_DURATION = 300000;
5
+ export declare const VISIBILITY_CHANGE_TIMEOUT = 300000;
@@ -6,6 +6,8 @@ export declare type RecordedEvents = Uint8Array | string;
6
6
  export interface ReplayRequest {
7
7
  endpoint: string;
8
8
  events: RecordedEvents;
9
+ replayId: string;
10
+ segmentId: number;
9
11
  }
10
12
  export declare type InstrumentationTypeBreadcrumb = 'dom' | 'scope';
11
13
  export declare type InstrumentationTypeSpan = 'fetch' | 'xhr' | 'history';
package/dist/index.es.js CHANGED
@@ -91,36 +91,17 @@ function __spreadArray(to, from, pack) {
91
91
  var REPLAY_SESSION_KEY = 'sentryReplaySession';
92
92
  var REPLAY_EVENT_NAME = 'replay_event';
93
93
  // The idle limit for a session
94
- var SESSION_IDLE_DURATION = 900000; // 15 minutes
94
+ var SESSION_IDLE_DURATION = 300000; // 5 minutes in ms
95
95
  // Grace period to keep a session when a user changes tabs or hides window
96
96
  var VISIBILITY_CHANGE_TIMEOUT = SESSION_IDLE_DURATION;
97
97
 
98
- function captureReplay(_a) {
99
- var session = _a.session, initialState = _a.initialState, errorIds = _a.errorIds, traceIds = _a.traceIds, urls = _a.urls;
100
- captureEvent({
98
+ function captureReplayEvent(_a) {
99
+ var initialState = _a.initialState, includeReplayStartTimestamp = _a.includeReplayStartTimestamp, errorIds = _a.errorIds, replay_id = _a.replayId, segment_id = _a.segmentId, timestamp = _a.timestamp, traceIds = _a.traceIds, urls = _a.urls;
100
+ captureEvent(__assign(__assign(__assign({
101
101
  // @ts-expect-error replay_event is a new event type
102
- type: REPLAY_EVENT_NAME,
103
- replay_start_timestamp: initialState.timestamp / 1000,
104
- error_ids: errorIds,
105
- trace_ids: traceIds,
106
- urls: urls,
107
- replay_id: session.id,
108
- segment_id: session.segmentId, // TODO: Should this increment?
109
- }, { event_id: session.id });
110
- }
111
-
112
- function captureReplayUpdate(_a) {
113
- var session = _a.session, timestamp = _a.timestamp, errorIds = _a.errorIds, traceIds = _a.traceIds, urls = _a.urls;
114
- captureEvent({
115
- // @ts-expect-error replay_event is a new event type
116
- type: REPLAY_EVENT_NAME,
117
- timestamp: timestamp / 1000,
118
- error_ids: errorIds,
119
- trace_ids: traceIds,
120
- urls: urls,
121
- replay_id: session.id,
122
- segment_id: ++session.segmentId,
123
- });
102
+ type: REPLAY_EVENT_NAME }, (includeReplayStartTimestamp
103
+ ? { replay_start_timestamp: initialState.timestamp / 1000 }
104
+ : {})), (timestamp ? { timestamp: timestamp / 1000 } : {})), { error_ids: errorIds, trace_ids: traceIds, urls: urls, replay_id: replay_id, segment_id: segment_id }), { event_id: replay_id });
124
105
  }
125
106
 
126
107
  function createBreadcrumb(breadcrumb) {
@@ -540,7 +521,11 @@ function createPaintEntry(entry) {
540
521
  }
541
522
  function createNavigationEntry(entry) {
542
523
  // TODO: There looks to be some more interesting bits in here (domComplete, domContentLoaded)
543
- var entryType = entry.entryType, name = entry.name, domComplete = entry.domComplete, startTime = entry.startTime, transferSize = entry.transferSize, type = entry.type;
524
+ var entryType = entry.entryType, name = entry.name, duration = entry.duration, domComplete = entry.domComplete, startTime = entry.startTime, transferSize = entry.transferSize, type = entry.type;
525
+ // Ignore entries with no duration, they do not seem to be useful and cause dupes
526
+ if (duration === 0) {
527
+ return null;
528
+ }
544
529
  return {
545
530
  type: "".concat(entryType, ".").concat(type),
546
531
  start: getAbsoluteTime(startTime),
@@ -548,11 +533,12 @@ function createNavigationEntry(entry) {
548
533
  name: name,
549
534
  data: {
550
535
  size: transferSize,
536
+ duration: duration,
551
537
  },
552
538
  };
553
539
  }
554
540
  function createResourceEntry(entry) {
555
- var entryType = entry.entryType, initiatorType = entry.initiatorType, name = entry.name, responseEnd = entry.responseEnd, startTime = entry.startTime, transferSize = entry.transferSize;
541
+ var entryType = entry.entryType, initiatorType = entry.initiatorType, name = entry.name, responseEnd = entry.responseEnd, startTime = entry.startTime, encodedBodySize = entry.encodedBodySize, transferSize = entry.transferSize;
556
542
  // Do not capture fetches to Sentry ingestion endpoint
557
543
  if (isIngestHost(name)) {
558
544
  return null;
@@ -568,6 +554,7 @@ function createResourceEntry(entry) {
568
554
  name: name,
569
555
  data: {
570
556
  size: transferSize,
557
+ encodedBodySize: encodedBodySize,
571
558
  },
572
559
  };
573
560
  }
@@ -580,6 +567,7 @@ function createLargestContentfulPaint(entry) {
580
567
  start: start,
581
568
  end: start + duration,
582
569
  data: {
570
+ duration: duration,
583
571
  size: size,
584
572
  // Not sure why this errors, Node should be correct (Argument of type 'Node' is not assignable to parameter of type 'INode')
585
573
  nodeId: record.mirror.getId(entry.element),
@@ -678,7 +666,6 @@ var EventBufferCompressionWorker = /** @class */ (function () {
678
666
  method: 'addEvent',
679
667
  args: [event],
680
668
  });
681
- logger.log('Message posted to worker');
682
669
  // XXX: See note in `get length()`
683
670
  _this.eventBufferItemLength++;
684
671
  return promise;
@@ -798,7 +785,7 @@ var EventBufferCompressionWorker = /** @class */ (function () {
798
785
  }());
799
786
 
800
787
  var BASE_RETRY_INTERVAL = 5000;
801
- var MAX_RETRY_COUNT = 5;
788
+ var MAX_RETRY_COUNT = 3;
802
789
  var UNABLE_TO_SEND_REPLAY = 'Unable to send replay';
803
790
  var SentryReplay = /** @class */ (function () {
804
791
  function SentryReplay(_a) {
@@ -823,12 +810,10 @@ var SentryReplay = /** @class */ (function () {
823
810
  this.retryCount = 0;
824
811
  this.retryInterval = BASE_RETRY_INTERVAL;
825
812
  /**
826
- * Flag to make sure we only create a replay event when
827
- * necessary (i.e. we only want to have a single replay
828
- * event per session and it should only be created
829
- * immediately before sending recording)
813
+ * Flag when a new session has been created. Captured replay events behave
814
+ * slightly differently in this case: `replay_start_timestamp` is included.
830
815
  */
831
- this.needsCaptureReplay = false;
816
+ this.newSessionCreated = false;
832
817
  /**
833
818
  * Is the integration currently active?
834
819
  */
@@ -1155,7 +1140,7 @@ var SentryReplay = /** @class */ (function () {
1155
1140
  // If session was newly created (i.e. was not loaded from storage), then
1156
1141
  // enable flag to create the root replay
1157
1142
  if (type === 'new') {
1158
- this.needsCaptureReplay = true;
1143
+ this.newSessionCreated = true;
1159
1144
  this.setInitialState();
1160
1145
  }
1161
1146
  if (session.id !== ((_b = this.session) === null || _b === void 0 ? void 0 : _b.id)) {
@@ -1172,6 +1157,7 @@ var SentryReplay = /** @class */ (function () {
1172
1157
  var urlPath = "".concat(window.location.pathname).concat(window.location.hash).concat(window.location.search);
1173
1158
  var url = "".concat(window.location.origin).concat(urlPath);
1174
1159
  this.performanceEvents = [];
1160
+ this.initialEventTimestampSinceFlush = null;
1175
1161
  // Reset context as well
1176
1162
  this.popEventContext();
1177
1163
  this.initialState = {
@@ -1303,8 +1289,11 @@ var SentryReplay = /** @class */ (function () {
1303
1289
  // requires coordination with frontend
1304
1290
  var isMs = event.timestamp > 9999999999;
1305
1291
  var timestampInMs = isMs ? event.timestamp : event.timestamp * 1000;
1306
- if (!this.context.earliestEvent ||
1307
- timestampInMs < this.context.earliestEvent) {
1292
+ // Only record earliest event if a new session was created, otherwise it
1293
+ // shouldn't be relevant
1294
+ if (this.newSessionCreated &&
1295
+ (!this.context.earliestEvent ||
1296
+ timestampInMs < this.context.earliestEvent)) {
1308
1297
  this.context.earliestEvent = timestampInMs;
1309
1298
  }
1310
1299
  this.eventBuffer.addEvent(event, isCheckout);
@@ -1427,7 +1416,6 @@ var SentryReplay = /** @class */ (function () {
1427
1416
  initialState.timestamp = this.context.earliestEvent;
1428
1417
  }
1429
1418
  var context = {
1430
- session: this.session,
1431
1419
  initialState: initialState,
1432
1420
  timestamp: timestamp,
1433
1421
  errorIds: Array.from(this.context.errorIds).filter(Boolean),
@@ -1450,7 +1438,7 @@ var SentryReplay = /** @class */ (function () {
1450
1438
  SentryReplay.prototype.flushUpdate = function () {
1451
1439
  var _a, _b;
1452
1440
  return __awaiter(this, void 0, void 0, function () {
1453
- var timestamp, recordingData, err_1;
1441
+ var timestamp, replayId, newSessionCreated, segmentId, recordingData, err_1;
1454
1442
  return __generator(this, function (_c) {
1455
1443
  switch (_c.label) {
1456
1444
  case 0:
@@ -1481,12 +1469,9 @@ var SentryReplay = /** @class */ (function () {
1481
1469
  // Only attach memory event if eventBuffer is not empty
1482
1470
  _c.sent();
1483
1471
  timestamp = new Date().getTime();
1484
- // Only want to create replay event if session is new
1485
- if (this.needsCaptureReplay) {
1486
- // This event needs to exist before calling `sendReplay`
1487
- captureReplay(this.popEventContext({ timestamp: timestamp }));
1488
- this.needsCaptureReplay = false;
1489
- }
1472
+ replayId = this.session.id;
1473
+ newSessionCreated = this.newSessionCreated;
1474
+ segmentId = this.session.segmentId++;
1490
1475
  // Reset this to null regardless of `sendReplay` result so that future
1491
1476
  // events will get flushed properly
1492
1477
  this.initialEventTimestampSinceFlush = null;
@@ -1496,7 +1481,7 @@ var SentryReplay = /** @class */ (function () {
1496
1481
  return [4 /*yield*/, this.eventBuffer.finish()];
1497
1482
  case 4:
1498
1483
  recordingData = _c.sent();
1499
- return [4 /*yield*/, this.sendReplay(this.session.id, recordingData)];
1484
+ return [4 /*yield*/, this.sendReplay(replayId, recordingData, segmentId)];
1500
1485
  case 5:
1501
1486
  _c.sent();
1502
1487
  // The below will only happen after successfully sending replay //
@@ -1505,7 +1490,8 @@ var SentryReplay = /** @class */ (function () {
1505
1490
  // of the event is passed as an argument in the case where a timeout
1506
1491
  // occurs.
1507
1492
  this.updateLastActivity(timestamp);
1508
- captureReplayUpdate(this.popEventContext({ timestamp: timestamp }));
1493
+ captureReplayEvent(__assign(__assign({}, this.popEventContext({ timestamp: timestamp })), { replayId: replayId, segmentId: segmentId, includeReplayStartTimestamp: newSessionCreated }));
1494
+ this.newSessionCreated = false;
1509
1495
  return [3 /*break*/, 7];
1510
1496
  case 6:
1511
1497
  err_1 = _c.sent();
@@ -1521,22 +1507,21 @@ var SentryReplay = /** @class */ (function () {
1521
1507
  * Send replay attachment using `fetch()`
1522
1508
  */
1523
1509
  SentryReplay.prototype.sendReplayRequest = function (_a) {
1524
- var _b, _c;
1525
- var endpoint = _a.endpoint, events = _a.events;
1510
+ var endpoint = _a.endpoint, events = _a.events, event_id = _a.replayId, segment_id = _a.segmentId;
1526
1511
  return __awaiter(this, void 0, void 0, function () {
1527
- var payloadWithSequence, envelope, response, _d, _e;
1528
- var _f;
1529
- return __generator(this, function (_g) {
1530
- switch (_g.label) {
1512
+ var payloadWithSequence, envelope, response, _b, _c;
1513
+ var _d;
1514
+ return __generator(this, function (_e) {
1515
+ switch (_e.label) {
1531
1516
  case 0:
1532
1517
  payloadWithSequence = createPayload({
1533
1518
  events: events,
1534
1519
  headers: {
1535
- segment_id: (_b = this.session) === null || _b === void 0 ? void 0 : _b.segmentId,
1520
+ segment_id: segment_id,
1536
1521
  },
1537
1522
  });
1538
1523
  envelope = createEnvelope({
1539
- event_id: (_c = this.session) === null || _c === void 0 ? void 0 : _c.id,
1524
+ event_id: event_id,
1540
1525
  sent_at: new Date().toISOString(),
1541
1526
  sdk: { name: 'sentry.javascript.integration.replay', version: '1.0.0' },
1542
1527
  }, [
@@ -1557,17 +1542,17 @@ var SentryReplay = /** @class */ (function () {
1557
1542
  body: serializeEnvelope(envelope),
1558
1543
  })];
1559
1544
  case 1:
1560
- response = _g.sent();
1545
+ response = _e.sent();
1561
1546
  if (!(response.status !== 200)) return [3 /*break*/, 3];
1562
- _d = setContext;
1563
- _e = ['Send Replay Response'];
1564
- _f = {
1547
+ _b = setContext;
1548
+ _c = ['Send Replay Response'];
1549
+ _d = {
1565
1550
  status: response.status
1566
1551
  };
1567
1552
  return [4 /*yield*/, response.text()];
1568
1553
  case 2:
1569
- _d.apply(void 0, _e.concat([(_f.body = _g.sent(),
1570
- _f)]));
1554
+ _b.apply(void 0, _c.concat([(_d.body = _e.sent(),
1555
+ _d)]));
1571
1556
  throw new Error(UNABLE_TO_SEND_REPLAY);
1572
1557
  case 3: return [2 /*return*/];
1573
1558
  }
@@ -1581,7 +1566,7 @@ var SentryReplay = /** @class */ (function () {
1581
1566
  /**
1582
1567
  * Finalize and send the current replay event to Sentry
1583
1568
  */
1584
- SentryReplay.prototype.sendReplay = function (eventId, events) {
1569
+ SentryReplay.prototype.sendReplay = function (replayId, events, segmentId) {
1585
1570
  return __awaiter(this, void 0, void 0, function () {
1586
1571
  var client, endpoint, ex_1;
1587
1572
  var _this = this;
@@ -1600,6 +1585,8 @@ var SentryReplay = /** @class */ (function () {
1600
1585
  return [4 /*yield*/, this.sendReplayRequest({
1601
1586
  endpoint: endpoint,
1602
1587
  events: events,
1588
+ replayId: replayId,
1589
+ segmentId: segmentId,
1603
1590
  })];
1604
1591
  case 2:
1605
1592
  _b.sent();
@@ -1618,29 +1605,30 @@ var SentryReplay = /** @class */ (function () {
1618
1605
  // failed, we'll can retry with the same events payload
1619
1606
  if (this.retryCount >= MAX_RETRY_COUNT) {
1620
1607
  this.resetRetries();
1621
- return [2 /*return*/, false];
1608
+ throw new Error(UNABLE_TO_SEND_REPLAY);
1622
1609
  }
1623
1610
  this.retryCount = this.retryCount + 1;
1624
- // will retry in intervals of 5, 10, 15, 20, 25 seconds
1611
+ // will retry in intervals of 5, 10, 30
1625
1612
  this.retryInterval = this.retryCount * this.retryInterval;
1626
1613
  _b.label = 4;
1627
1614
  case 4:
1628
1615
  _b.trys.push([4, 6, , 7]);
1629
1616
  return [4 /*yield*/, new Promise(function (resolve, reject) {
1630
1617
  setTimeout(function () { return __awaiter(_this, void 0, void 0, function () {
1631
- var result;
1632
- return __generator(this, function (_a) {
1633
- switch (_a.label) {
1634
- case 0: return [4 /*yield*/, this.sendReplay(eventId, events)];
1618
+ return __generator(this, function (_b) {
1619
+ switch (_b.label) {
1620
+ case 0:
1621
+ _b.trys.push([0, 2, , 3]);
1622
+ return [4 /*yield*/, this.sendReplay(replayId, events, segmentId)];
1635
1623
  case 1:
1636
- result = _a.sent();
1637
- if (result) {
1638
- resolve(true);
1639
- }
1640
- else {
1641
- reject(new Error('Could not send replay'));
1642
- }
1643
- return [2 /*return*/];
1624
+ _b.sent();
1625
+ resolve(true);
1626
+ return [3 /*break*/, 3];
1627
+ case 2:
1628
+ _b.sent();
1629
+ reject(new Error(UNABLE_TO_SEND_REPLAY));
1630
+ return [3 /*break*/, 3];
1631
+ case 3: return [2 /*return*/];
1644
1632
  }
1645
1633
  });
1646
1634
  }); }, _this.retryInterval);
@@ -1650,7 +1638,7 @@ var SentryReplay = /** @class */ (function () {
1650
1638
  return [2 /*return*/, true];
1651
1639
  case 6:
1652
1640
  _b.sent();
1653
- return [2 /*return*/, false];
1641
+ throw new Error(UNABLE_TO_SEND_REPLAY);
1654
1642
  case 7: return [3 /*break*/, 8];
1655
1643
  case 8: return [2 /*return*/];
1656
1644
  }