11import {
2+ applyScopeDataToEvent ,
23 createEventEnvelope ,
34 createSessionEnvelope ,
45 getEnvelopeEndpointWithUrlEncodedAuth ,
56 makeSession ,
67 updateSession ,
78} from '@sentry/core' ;
8- import type { Event , Session , StackFrame , TraceContext } from '@sentry/types' ;
9+ import type { Event , ScopeData , Session , StackFrame } from '@sentry/types' ;
910import {
1011 callFrameToStackFrame ,
1112 normalizeUrlToBase ,
@@ -87,7 +88,23 @@ function prepareStackFrames(stackFrames: StackFrame[] | undefined): StackFrame[]
8788 return strippedFrames ;
8889}
8990
90- async function sendAnrEvent ( frames ?: StackFrame [ ] , traceContext ?: TraceContext ) : Promise < void > {
91+ function applyScopeToEvent ( event : Event , scope : ScopeData ) : void {
92+ applyScopeDataToEvent ( event , scope ) ;
93+
94+ if ( ! event . contexts ?. trace ) {
95+ const { traceId, spanId, parentSpanId } = scope . propagationContext ;
96+ event . contexts = {
97+ trace : {
98+ trace_id : traceId ,
99+ span_id : spanId ,
100+ parent_span_id : parentSpanId ,
101+ } ,
102+ ...event . contexts ,
103+ } ;
104+ }
105+ }
106+
107+ async function sendAnrEvent ( frames ?: StackFrame [ ] , scope ?: ScopeData ) : Promise < void > {
91108 if ( hasSentAnrEvent ) {
92109 return ;
93110 }
@@ -100,7 +117,7 @@ async function sendAnrEvent(frames?: StackFrame[], traceContext?: TraceContext):
100117
101118 const event : Event = {
102119 event_id : uuid4 ( ) ,
103- contexts : { ... options . contexts , trace : traceContext } ,
120+ contexts : options . contexts ,
104121 release : options . release ,
105122 environment : options . environment ,
106123 dist : options . dist ,
@@ -120,8 +137,12 @@ async function sendAnrEvent(frames?: StackFrame[], traceContext?: TraceContext):
120137 tags : options . staticTags ,
121138 } ;
122139
140+ if ( scope ) {
141+ applyScopeToEvent ( event , scope ) ;
142+ }
143+
123144 const envelope = createEventEnvelope ( event , options . dsn , options . sdkMetadata ) ;
124- // Log the envelope so to aid in testing
145+ // Log the envelope to aid in testing
125146 log ( JSON . stringify ( envelope ) ) ;
126147
127148 await transport . send ( envelope ) ;
@@ -172,20 +193,23 @@ if (options.captureStackTrace) {
172193 'Runtime.evaluate' ,
173194 {
174195 // Grab the trace context from the current scope
175- expression :
176- 'var __sentry_ctx = __SENTRY__.hub.getScope().getPropagationContext(); __sentry_ctx.traceId + "-" + __sentry_ctx.spanId + "-" + __sentry_ctx.parentSpanId' ,
196+ expression : 'global.__SENTRY_GET_SCOPES__();' ,
177197 // Don't re-trigger the debugger if this causes an error
178198 silent : true ,
199+ // Serialize the result to json otherwise only primitives are supported
200+ returnByValue : true ,
179201 } ,
180- ( _ , param ) => {
181- const traceId = param && param . result ? ( param . result . value as string ) : '--' ;
182- const [ trace_id , span_id , parent_span_id ] = traceId . split ( '-' ) as ( string | undefined ) [ ] ;
202+ ( err , param ) => {
203+ if ( err ) {
204+ log ( `Error executing script: '${ err . message } '` ) ;
205+ }
206+
207+ const scopes = param && param . result ? ( param . result . value as ScopeData ) : undefined ;
183208
184209 session . post ( 'Debugger.resume' ) ;
185210 session . post ( 'Debugger.disable' ) ;
186211
187- const context = trace_id ?. length && span_id ?. length ? { trace_id, span_id, parent_span_id } : undefined ;
188- sendAnrEvent ( stackFrames , context ) . then ( null , ( ) => {
212+ sendAnrEvent ( stackFrames , scopes ) . then ( null , ( ) => {
189213 log ( 'Sending ANR event failed.' ) ;
190214 } ) ;
191215 } ,
0 commit comments