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 ,
@@ -86,7 +87,23 @@ function prepareStackFrames(stackFrames: StackFrame[] | undefined): StackFrame[]
8687 return strippedFrames ;
8788}
8889
89- async function sendAnrEvent ( frames ?: StackFrame [ ] , traceContext ?: TraceContext ) : Promise < void > {
90+ function applyScopeToEvent ( event : Event , scope : ScopeData ) : void {
91+ applyScopeDataToEvent ( event , scope ) ;
92+
93+ if ( ! event . contexts ?. trace ) {
94+ const { traceId, spanId, parentSpanId } = scope . propagationContext ;
95+ event . contexts = {
96+ trace : {
97+ trace_id : traceId ,
98+ span_id : spanId ,
99+ parent_span_id : parentSpanId ,
100+ } ,
101+ ...event . contexts ,
102+ } ;
103+ }
104+ }
105+
106+ async function sendAnrEvent ( frames ?: StackFrame [ ] , scope ?: ScopeData ) : Promise < void > {
90107 if ( hasSentAnrEvent ) {
91108 return ;
92109 }
@@ -99,7 +116,7 @@ async function sendAnrEvent(frames?: StackFrame[], traceContext?: TraceContext):
99116
100117 const event : Event = {
101118 event_id : uuid4 ( ) ,
102- contexts : { ... options . contexts , trace : traceContext } ,
119+ contexts : options . contexts ,
103120 release : options . release ,
104121 environment : options . environment ,
105122 dist : options . dist ,
@@ -119,6 +136,10 @@ async function sendAnrEvent(frames?: StackFrame[], traceContext?: TraceContext):
119136 tags : options . staticTags ,
120137 } ;
121138
139+ if ( scope ) {
140+ applyScopeToEvent ( event , scope ) ;
141+ }
142+
122143 const envelope = createEventEnvelope ( event , options . dsn , options . sdkMetadata , options . tunnel ) ;
123144 // Log the envelope to aid in testing
124145 log ( JSON . stringify ( envelope ) ) ;
@@ -171,20 +192,23 @@ if (options.captureStackTrace) {
171192 'Runtime.evaluate' ,
172193 {
173194 // Grab the trace context from the current scope
174- expression :
175- 'var __sentry_ctx = __SENTRY__.acs?.getCurrentScope().getPropagationContext() || {}; __sentry_ctx.traceId + "-" + __sentry_ctx.spanId + "-" + __sentry_ctx.parentSpanId' ,
195+ expression : 'global.__SENTRY_GET_SCOPES__();' ,
176196 // Don't re-trigger the debugger if this causes an error
177197 silent : true ,
198+ // Serialize the result to json otherwise only primitives are supported
199+ returnByValue : true ,
178200 } ,
179- ( _ , param ) => {
180- const traceId = param && param . result ? ( param . result . value as string ) : '--' ;
181- const [ trace_id , span_id , parent_span_id ] = traceId . split ( '-' ) as ( string | undefined ) [ ] ;
201+ ( err , param ) => {
202+ if ( err ) {
203+ log ( `Error executing script: '${ err . message } '` ) ;
204+ }
205+
206+ const scopes = param && param . result ? ( param . result . value as ScopeData ) : undefined ;
182207
183208 session . post ( 'Debugger.resume' ) ;
184209 session . post ( 'Debugger.disable' ) ;
185210
186- const context = trace_id ?. length && span_id ?. length ? { trace_id, span_id, parent_span_id } : undefined ;
187- sendAnrEvent ( stackFrames , context ) . then ( null , ( ) => {
211+ sendAnrEvent ( stackFrames , scopes ) . then ( null , ( ) => {
188212 log ( 'Sending ANR event failed.' ) ;
189213 } ) ;
190214 } ,
0 commit comments