|
| 1 | +import { Event } from '@sentry/types'; |
| 2 | + |
| 3 | +import { REPLAY_EVENT_NAME, UNABLE_TO_SEND_REPLAY } from '../constants'; |
| 4 | +import type { ReplayContainer } from '../types'; |
| 5 | +import { addInternalBreadcrumb } from '../util/addInternalBreadcrumb'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Returns a listener to be added to `addGlobalEventProcessor(listener)`. |
| 9 | + */ |
| 10 | +export function handleGlobalEventListener(replay: ReplayContainer): (event: Event) => Event { |
| 11 | + return (event: Event) => { |
| 12 | + // Do not apply replayId to the root event |
| 13 | + if ( |
| 14 | + // @ts-ignore new event type |
| 15 | + event.type === REPLAY_EVENT_NAME |
| 16 | + ) { |
| 17 | + // Replays have separate set of breadcrumbs, do not include breadcrumbs |
| 18 | + // from core SDK |
| 19 | + delete event.breadcrumbs; |
| 20 | + return event; |
| 21 | + } |
| 22 | + |
| 23 | + // Only tag transactions with replayId if not waiting for an error |
| 24 | + // @ts-ignore private |
| 25 | + if (event.type !== 'transaction' || !replay._waitForError) { |
| 26 | + event.tags = { ...event.tags, replayId: replay.session?.id }; |
| 27 | + } |
| 28 | + |
| 29 | + // Collect traceIds in _context regardless of `_waitForError` - if it's true, |
| 30 | + // _context gets cleared on every checkout |
| 31 | + if (event.type === 'transaction' && event.contexts && event.contexts.trace && event.contexts.trace.trace_id) { |
| 32 | + replay.getContext().traceIds.add(event.contexts.trace.trace_id as string); |
| 33 | + return event; |
| 34 | + } |
| 35 | + |
| 36 | + // no event type means error |
| 37 | + if (!event.type) { |
| 38 | + replay.getContext().errorIds.add(event.event_id as string); |
| 39 | + } |
| 40 | + |
| 41 | + const exc = event.exception?.values?.[0]; |
| 42 | + addInternalBreadcrumb({ |
| 43 | + message: `Tagging event (${event.event_id}) - ${event.message} - ${exc?.type || 'Unknown'}: ${ |
| 44 | + exc?.value || 'n/a' |
| 45 | + }`, |
| 46 | + }); |
| 47 | + |
| 48 | + // Need to be very careful that this does not cause an infinite loop |
| 49 | + if ( |
| 50 | + // @ts-ignore private |
| 51 | + replay._waitForError && |
| 52 | + event.exception && |
| 53 | + event.message !== UNABLE_TO_SEND_REPLAY // ignore this error because otherwise we could loop indefinitely with trying to capture replay and failing |
| 54 | + ) { |
| 55 | + setTimeout(async () => { |
| 56 | + // Allow flush to complete before resuming as a session recording, otherwise |
| 57 | + // the checkout from `startRecording` may be included in the payload. |
| 58 | + // Prefer to keep the error replay as a separate (and smaller) segment |
| 59 | + // than the session replay. |
| 60 | + await replay.flushImmediate(); |
| 61 | + |
| 62 | + if (replay.stopRecording()) { |
| 63 | + // Reset all "capture on error" configuration before |
| 64 | + // starting a new recording |
| 65 | + // @ts-ignore private |
| 66 | + replay._waitForError = false; |
| 67 | + replay.startRecording(); |
| 68 | + } |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + return event; |
| 73 | + }; |
| 74 | +} |
0 commit comments