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' ;
@@ -906,7 +908,7 @@ export class ReplayContainer implements ReplayContainerInterface {
906
908
*/
907
909
async sendReplayRequest ( {
908
910
events,
909
- replayId : event_id ,
911
+ replayId,
910
912
segmentId : segment_id ,
911
913
includeReplayStartTimestamp,
912
914
eventContext,
@@ -922,77 +924,40 @@ export class ReplayContainer implements ReplayContainerInterface {
922
924
923
925
const currentTimestamp = new Date ( ) . getTime ( ) ;
924
926
925
- const sdkInfo = {
926
- name : 'sentry.javascript.integration.replay' ,
927
- version : __SENTRY_REPLAY_VERSION__ ,
928
- } ;
927
+ const hub = getCurrentHub ( ) ;
928
+ const client = hub . getClient ( ) ;
929
+ const scope = hub . getScope ( ) ;
930
+ const transport = client ?. getTransport ( ) ;
929
931
930
- const replayEvent = await new Promise ( resolve => {
931
- getCurrentHub ( )
932
- // @ts -ignore private api
933
- ?. _withClient ( async ( client : Client , scope : Scope ) => {
934
- // XXX: This event does not trigger `beforeSend` in SDK
935
- // @ts -ignore private api
936
- const preparedEvent : Event = await client . _prepareEvent (
937
- {
938
- type : REPLAY_EVENT_NAME ,
939
- ...( includeReplayStartTimestamp ? { replay_start_timestamp : initialTimestamp / 1000 } : { } ) ,
940
- timestamp : currentTimestamp / 1000 ,
941
- error_ids : errorIds ,
942
- trace_ids : traceIds ,
943
- urls,
944
- replay_id : event_id ,
945
- segment_id,
946
- } ,
947
- { event_id } ,
948
- scope ,
949
- ) ;
950
- const session = scope && scope . getSession ( ) ;
951
- if ( session ) {
952
- // @ts -ignore private api
953
- client . _updateSessionFromEvent ( session , preparedEvent ) ;
954
- }
932
+ if ( ! client || ! scope || ! transport ) {
933
+ return ;
934
+ }
955
935
956
- preparedEvent . sdk = {
957
- ...preparedEvent . sdk ,
958
- ...sdkInfo ,
959
- } ;
936
+ const baseEvent : Event = {
937
+ // @ts -ignore private api
938
+ type : REPLAY_EVENT_NAME ,
939
+ ...( includeReplayStartTimestamp ? { replay_start_timestamp : initialTimestamp / 1000 } : { } ) ,
940
+ timestamp : currentTimestamp / 1000 ,
941
+ error_ids : errorIds ,
942
+ trace_ids : traceIds ,
943
+ urls,
944
+ replay_id : replayId ,
945
+ segment_id,
946
+ } ;
960
947
961
- preparedEvent . tags = {
962
- ...preparedEvent . tags ,
963
- sessionSampleRate : this . _options . sessionSampleRate ,
964
- errorSampleRate : this . _options . errorSampleRate ,
965
- replayType : this . session ?. sampled ,
966
- } ;
948
+ const replayEvent = await getReplayEvent ( { scope, client, replayId, event : baseEvent } ) ;
967
949
968
- resolve ( preparedEvent ) ;
969
- } ) ;
970
- } ) ;
950
+ replayEvent . tags = {
951
+ ...replayEvent . tags ,
952
+ sessionSampleRate : this . _options . sessionSampleRate ,
953
+ errorSampleRate : this . _options . errorSampleRate ,
954
+ replayType : this . session ?. sampled ,
955
+ } ;
971
956
972
- const envelope = createEnvelope (
973
- {
974
- event_id,
975
- sent_at : new Date ( ) . toISOString ( ) ,
976
- sdk : sdkInfo ,
977
- } ,
978
- [
979
- // @ts -ignore New types
980
- [ { type : 'replay_event' } , replayEvent ] ,
981
- [
982
- {
983
- // @ts -ignore setting envelope
984
- type : 'replay_recording' ,
985
- length : payloadWithSequence . length ,
986
- } ,
987
- // @ts -ignore: Type 'string' is not assignable to type 'ClientReport'.ts(2322)
988
- payloadWithSequence ,
989
- ] ,
990
- ] ,
991
- ) ;
957
+ const envelope = createReplayEnvelope ( replayId , replayEvent , payloadWithSequence ) ;
992
958
993
- const client = getCurrentHub ( ) . getClient ( ) ;
994
959
try {
995
- return client ?. getTransport ( ) ? .send ( envelope ) ;
960
+ return transport . send ( envelope ) ;
996
961
} catch {
997
962
throw new Error ( UNABLE_TO_SEND_REPLAY ) ;
998
963
}
0 commit comments