1
1
import {
2
+ applyScopeDataToEvent ,
2
3
createEventEnvelope ,
3
4
createSessionEnvelope ,
4
5
getEnvelopeEndpointWithUrlEncodedAuth ,
5
6
makeSession ,
6
7
updateSession ,
7
8
} from '@sentry/core' ;
8
- import type { Event , Session , StackFrame , TraceContext } from '@sentry/types' ;
9
+ import type { Event , ScopeData , Session , StackFrame } from '@sentry/types' ;
9
10
import {
10
11
callFrameToStackFrame ,
11
12
normalizeUrlToBase ,
@@ -87,7 +88,23 @@ function prepareStackFrames(stackFrames: StackFrame[] | undefined): StackFrame[]
87
88
return strippedFrames ;
88
89
}
89
90
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 > {
91
108
if ( hasSentAnrEvent ) {
92
109
return ;
93
110
}
@@ -100,7 +117,7 @@ async function sendAnrEvent(frames?: StackFrame[], traceContext?: TraceContext):
100
117
101
118
const event : Event = {
102
119
event_id : uuid4 ( ) ,
103
- contexts : { ... options . contexts , trace : traceContext } ,
120
+ contexts : options . contexts ,
104
121
release : options . release ,
105
122
environment : options . environment ,
106
123
dist : options . dist ,
@@ -120,8 +137,12 @@ async function sendAnrEvent(frames?: StackFrame[], traceContext?: TraceContext):
120
137
tags : options . staticTags ,
121
138
} ;
122
139
140
+ if ( scope ) {
141
+ applyScopeToEvent ( event , scope ) ;
142
+ }
143
+
123
144
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
125
146
log ( JSON . stringify ( envelope ) ) ;
126
147
127
148
await transport . send ( envelope ) ;
@@ -172,20 +193,23 @@ if (options.captureStackTrace) {
172
193
'Runtime.evaluate' ,
173
194
{
174
195
// 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__();' ,
177
197
// Don't re-trigger the debugger if this causes an error
178
198
silent : true ,
199
+ // Serialize the result to json otherwise only primitives are supported
200
+ returnByValue : true ,
179
201
} ,
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 ;
183
208
184
209
session . post ( 'Debugger.resume' ) ;
185
210
session . post ( 'Debugger.disable' ) ;
186
211
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 , ( ) => {
189
213
log ( 'Sending ANR event failed.' ) ;
190
214
} ) ;
191
215
} ,
0 commit comments