|
1 | 1 | /* eslint-disable max-lines */ // TODO: We might want to split this file up |
2 | 2 | import { addGlobalEventProcessor, getCurrentHub, Scope, setContext } from '@sentry/core'; |
3 | | -import { Breadcrumb, Client, Event, Integration } from '@sentry/types'; |
| 3 | +import { Breadcrumb, Client, DataCategory, Event, EventDropReason, Integration } from '@sentry/types'; |
4 | 4 | import { addInstrumentationHandler, createEnvelope, logger } from '@sentry/utils'; |
5 | 5 | import debounce from 'lodash.debounce'; |
6 | 6 | import { PerformanceObserverEntryList } from 'perf_hooks'; |
@@ -125,6 +125,11 @@ export class Replay implements Integration { |
125 | 125 | */ |
126 | 126 | private stopRecording: ReturnType<typeof record> | null = null; |
127 | 127 |
|
| 128 | + /** |
| 129 | + * We overwrite `client.recordDroppedEvent`, but store the original method here so we can restore it. |
| 130 | + */ |
| 131 | + private _originalRecordDroppedEvent?: Client['recordDroppedEvent']; |
| 132 | + |
128 | 133 | private context: InternalEventContext = { |
129 | 134 | errorIds: new Set(), |
130 | 135 | traceIds: new Set(), |
@@ -400,6 +405,9 @@ export class Replay implements Integration { |
400 | 405 | WINDOW.addEventListener('blur', this.handleWindowBlur); |
401 | 406 | WINDOW.addEventListener('focus', this.handleWindowFocus); |
402 | 407 |
|
| 408 | + // We need to filter out dropped events captured by `addGlobalEventProcessor(this.handleGlobalEvent)` below |
| 409 | + this._overwriteRecordDroppedEvent(); |
| 410 | + |
403 | 411 | // There is no way to remove these listeners, so ensure they are only added once |
404 | 412 | if (!this.hasInitializedCoreListeners) { |
405 | 413 | // Listeners from core SDK // |
@@ -462,6 +470,8 @@ export class Replay implements Integration { |
462 | 470 | WINDOW.removeEventListener('blur', this.handleWindowBlur); |
463 | 471 | WINDOW.removeEventListener('focus', this.handleWindowFocus); |
464 | 472 |
|
| 473 | + this._restoreRecordDroppedEvent(); |
| 474 | + |
465 | 475 | if (this.performanceObserver) { |
466 | 476 | this.performanceObserver.disconnect(); |
467 | 477 | this.performanceObserver = null; |
@@ -1333,4 +1343,39 @@ export class Replay implements Integration { |
1333 | 1343 | saveSession(this.session); |
1334 | 1344 | } |
1335 | 1345 | } |
| 1346 | + |
| 1347 | + private _overwriteRecordDroppedEvent(): void { |
| 1348 | + const client = getCurrentHub().getClient(); |
| 1349 | + |
| 1350 | + if (!client) { |
| 1351 | + return; |
| 1352 | + } |
| 1353 | + |
| 1354 | + const _originalCallback = client.recordDroppedEvent.bind(client); |
| 1355 | + |
| 1356 | + const recordDroppedEvent: Client['recordDroppedEvent'] = ( |
| 1357 | + reason: EventDropReason, |
| 1358 | + category: DataCategory, |
| 1359 | + event?: Event, |
| 1360 | + ): void => { |
| 1361 | + if (event && event.event_id) { |
| 1362 | + this.context.errorIds.delete(event.event_id); |
| 1363 | + } |
| 1364 | + |
| 1365 | + return _originalCallback(reason, category, event); |
| 1366 | + }; |
| 1367 | + |
| 1368 | + client.recordDroppedEvent = recordDroppedEvent; |
| 1369 | + this._originalRecordDroppedEvent = _originalCallback; |
| 1370 | + } |
| 1371 | + |
| 1372 | + private _restoreRecordDroppedEvent(): void { |
| 1373 | + const client = getCurrentHub().getClient(); |
| 1374 | + |
| 1375 | + if (!client || !this._originalRecordDroppedEvent) { |
| 1376 | + return; |
| 1377 | + } |
| 1378 | + |
| 1379 | + client.recordDroppedEvent = this._originalRecordDroppedEvent; |
| 1380 | + } |
1336 | 1381 | } |
0 commit comments