@@ -21,11 +21,10 @@ type FetchResource = string | { toString(): string } | { url: string };
21
21
export function addFetchInstrumentationHandler (
22
22
handler : ( data : HandlerDataFetch ) => void ,
23
23
skipNativeFetchCheck ?: boolean ,
24
- httpClientInstrumented ?: boolean ,
25
24
) : void {
26
25
const type = 'fetch' ;
27
26
addHandler ( type , handler ) ;
28
- maybeInstrument ( type , ( ) => instrumentFetch ( undefined , skipNativeFetchCheck , httpClientInstrumented ) ) ;
27
+ maybeInstrument ( type , ( ) => instrumentFetch ( undefined , skipNativeFetchCheck ) ) ;
29
28
}
30
29
31
30
/**
@@ -42,17 +41,23 @@ export function addFetchEndInstrumentationHandler(handler: (data: HandlerDataFet
42
41
maybeInstrument ( type , ( ) => instrumentFetch ( streamHandler ) ) ;
43
42
}
44
43
45
- function instrumentFetch (
46
- onFetchResolved ?: ( response : Response ) => void ,
47
- skipNativeFetchCheck : boolean = false ,
48
- httpClientInstrumented : boolean = false ,
49
- ) : void {
44
+ function instrumentFetch ( onFetchResolved ?: ( response : Response ) => void , skipNativeFetchCheck : boolean = false ) : void {
50
45
if ( skipNativeFetchCheck && ! supportsNativeFetch ( ) ) {
51
46
return ;
52
47
}
53
48
54
49
fill ( GLOBAL_OBJ , 'fetch' , function ( originalFetch : ( ) => void ) : ( ) => void {
55
50
return function ( ...args : any [ ] ) : void {
51
+ // We capture the stack right here and not in the Promise error callback because Safari (and probably other
52
+ // browsers too) will wipe the stack trace up to this point, only leaving us with this file which is useless.
53
+
54
+ // NOTE: If you are a Sentry user, and you are seeing this stack frame,
55
+ // it means the error, that was caused by your fetch call did not
56
+ // have a stack trace, so the SDK backfilled the stack trace so
57
+ // you can see which fetch call failed.
58
+ const virtualError = new Error ( ) ;
59
+ const virtualStackTrace = virtualError . stack ;
60
+
56
61
const { method, url } = parseFetchArgs ( args ) ;
57
62
const handlerData : HandlerDataFetch = {
58
63
args,
@@ -61,27 +66,18 @@ function instrumentFetch(
61
66
url,
62
67
} ,
63
68
startTimestamp : timestampInSeconds ( ) * 1000 ,
69
+ stack : virtualStackTrace ,
64
70
} ;
65
71
66
72
// if there is no callback, fetch is instrumented directly
67
73
// if httpClientInstrumented is true, we are in the HttpClient instrumentation
68
74
// and we may need to capture the stacktrace even when the fetch promise is resolved
69
- if ( ! onFetchResolved && ! httpClientInstrumented ) {
75
+ if ( ! onFetchResolved ) {
70
76
triggerHandlers ( 'fetch' , {
71
77
...handlerData ,
72
78
} ) ;
73
79
}
74
80
75
- // We capture the stack right here and not in the Promise error callback because Safari (and probably other
76
- // browsers too) will wipe the stack trace up to this point, only leaving us with this file which is useless.
77
-
78
- // NOTE: If you are a Sentry user, and you are seeing this stack frame,
79
- // it means the error, that was caused by your fetch call did not
80
- // have a stack trace, so the SDK backfilled the stack trace so
81
- // you can see which fetch call failed.
82
- const virtualError = new Error ( ) ;
83
- const virtualStackTrace = virtualError . stack ;
84
-
85
81
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
86
82
return originalFetch . apply ( GLOBAL_OBJ , args ) . then (
87
83
async ( response : Response ) => {
@@ -93,7 +89,6 @@ function instrumentFetch(
93
89
...handlerData ,
94
90
endTimestamp : timestampInSeconds ( ) * 1000 ,
95
91
response,
96
- error : httpClientInstrumented ? virtualError : undefined ,
97
92
} ) ;
98
93
}
99
94
0 commit comments