1
1
/* eslint-disable max-lines */ // TODO: We might want to split this file up
2
- import { addGlobalEventProcessor , captureException , getCurrentHub , Scope , setContext } from '@sentry/core' ;
3
- import { Breadcrumb , Client , Event } from '@sentry/types' ;
4
- import { addInstrumentationHandler , createEnvelope , logger } from '@sentry/utils' ;
2
+ import { addGlobalEventProcessor , captureException , getCurrentHub , setContext } from '@sentry/core' ;
3
+ import { Breadcrumb , Event } from '@sentry/types' ;
4
+ import { addInstrumentationHandler , logger } from '@sentry/utils' ;
5
5
import debounce from 'lodash.debounce' ;
6
6
import { EventType , record } from 'rrweb' ;
7
7
@@ -44,6 +44,8 @@ import { addMemoryEntry } from './util/addMemoryEntry';
44
44
import { createBreadcrumb } from './util/createBreadcrumb' ;
45
45
import { createPayload } from './util/createPayload' ;
46
46
import { createPerformanceSpans } from './util/createPerformanceSpans' ;
47
+ import { createReplayEnvelope } from './util/createReplayEnvelope' ;
48
+ import { getReplayEvent } from './util/getReplayEvent' ;
47
49
import { isExpired } from './util/isExpired' ;
48
50
import { isSessionExpired } from './util/isSessionExpired' ;
49
51
import { overwriteRecordDroppedEvent , restoreRecordDroppedEvent } from './util/monkeyPatchRecordDroppedEvent' ;
@@ -901,7 +903,7 @@ export class ReplayContainer implements ReplayContainerInterface {
901
903
*/
902
904
async sendReplayRequest ( {
903
905
events,
904
- replayId : event_id ,
906
+ replayId,
905
907
segmentId : segment_id ,
906
908
includeReplayStartTimestamp,
907
909
eventContext,
@@ -917,77 +919,40 @@ export class ReplayContainer implements ReplayContainerInterface {
917
919
918
920
const currentTimestamp = new Date ( ) . getTime ( ) ;
919
921
920
- const sdkInfo = {
921
- name : 'sentry.javascript.integration.replay' ,
922
- version : __SENTRY_REPLAY_VERSION__ ,
923
- } ;
922
+ const hub = getCurrentHub ( ) ;
923
+ const client = hub . getClient ( ) ;
924
+ const scope = hub . getScope ( ) ;
925
+ const transport = client ?. getTransport ( ) ;
924
926
925
- const replayEvent = await new Promise ( resolve => {
926
- getCurrentHub ( )
927
- // @ts -ignore private api
928
- ?. _withClient ( async ( client : Client , scope : Scope ) => {
929
- // XXX: This event does not trigger `beforeSend` in SDK
930
- // @ts -ignore private api
931
- const preparedEvent : Event = await client . _prepareEvent (
932
- {
933
- type : REPLAY_EVENT_NAME ,
934
- ...( includeReplayStartTimestamp ? { replay_start_timestamp : initialTimestamp / 1000 } : { } ) ,
935
- timestamp : currentTimestamp / 1000 ,
936
- error_ids : errorIds ,
937
- trace_ids : traceIds ,
938
- urls,
939
- replay_id : event_id ,
940
- segment_id,
941
- } ,
942
- { event_id } ,
943
- scope ,
944
- ) ;
945
- const session = scope && scope . getSession ( ) ;
946
- if ( session ) {
947
- // @ts -ignore private api
948
- client . _updateSessionFromEvent ( session , preparedEvent ) ;
949
- }
927
+ if ( ! client || ! scope || ! transport ) {
928
+ return ;
929
+ }
950
930
951
- preparedEvent . sdk = {
952
- ...preparedEvent . sdk ,
953
- ...sdkInfo ,
954
- } ;
931
+ const baseEvent : Event = {
932
+ // @ts -ignore private api
933
+ type : REPLAY_EVENT_NAME ,
934
+ ...( includeReplayStartTimestamp ? { replay_start_timestamp : initialTimestamp / 1000 } : { } ) ,
935
+ timestamp : currentTimestamp / 1000 ,
936
+ error_ids : errorIds ,
937
+ trace_ids : traceIds ,
938
+ urls,
939
+ replay_id : replayId ,
940
+ segment_id,
941
+ } ;
955
942
956
- preparedEvent . tags = {
957
- ...preparedEvent . tags ,
958
- sessionSampleRate : this . options . sessionSampleRate ,
959
- errorSampleRate : this . options . errorSampleRate ,
960
- replayType : this . session ?. sampled ,
961
- } ;
943
+ const replayEvent = await getReplayEvent ( { scope, client, replayId, event : baseEvent } ) ;
962
944
963
- resolve ( preparedEvent ) ;
964
- } ) ;
965
- } ) ;
945
+ replayEvent . tags = {
946
+ ...replayEvent . tags ,
947
+ sessionSampleRate : this . options . sessionSampleRate ,
948
+ errorSampleRate : this . options . errorSampleRate ,
949
+ replayType : this . session ?. sampled ,
950
+ } ;
966
951
967
- const envelope = createEnvelope (
968
- {
969
- event_id,
970
- sent_at : new Date ( ) . toISOString ( ) ,
971
- sdk : sdkInfo ,
972
- } ,
973
- [
974
- // @ts -ignore New types
975
- [ { type : 'replay_event' } , replayEvent ] ,
976
- [
977
- {
978
- // @ts -ignore setting envelope
979
- type : 'replay_recording' ,
980
- length : payloadWithSequence . length ,
981
- } ,
982
- // @ts -ignore: Type 'string' is not assignable to type 'ClientReport'.ts(2322)
983
- payloadWithSequence ,
984
- ] ,
985
- ] ,
986
- ) ;
952
+ const envelope = createReplayEnvelope ( replayId , replayEvent , payloadWithSequence ) ;
987
953
988
- const client = getCurrentHub ( ) . getClient ( ) ;
989
954
try {
990
- return client ?. getTransport ( ) ? .send ( envelope ) ;
955
+ return transport . send ( envelope ) ;
991
956
} catch {
992
957
throw new Error ( UNABLE_TO_SEND_REPLAY ) ;
993
958
}
0 commit comments