1
1
import type { BaseClient } from '@sentry/core' ;
2
2
import { getCurrentHub , trace } from '@sentry/core' ;
3
3
import type { Breadcrumbs , BrowserTracing } from '@sentry/svelte' ;
4
- import { captureException , WINDOW } from '@sentry/svelte' ;
4
+ import { captureException } from '@sentry/svelte' ;
5
5
import type { ClientOptions , SanitizedRequestData } from '@sentry/types' ;
6
6
import {
7
7
addExceptionMechanism ,
@@ -10,6 +10,7 @@ import {
10
10
getFetchUrl ,
11
11
getSanitizedUrlString ,
12
12
objectify ,
13
+ parseUrl ,
13
14
stringMatchesSomePattern ,
14
15
} from '@sentry/utils' ;
15
16
import type { LoadEvent } from '@sveltejs/kit' ;
@@ -108,12 +109,12 @@ function instrumentSvelteKitFetch(originalFetch: SvelteKitFetch): SvelteKitFetch
108
109
109
110
const browserTracingIntegration =
110
111
client . getIntegrationById && ( client . getIntegrationById ( 'BrowserTracing' ) as BrowserTracing | undefined ) ;
111
- const breadcrumbsIntegration = client . getIntegrationById ( 'BreadCrumbs ' ) as Breadcrumbs | undefined ;
112
+ const breadcrumbsIntegration = client . getIntegrationById ( 'Breadcrumbs ' ) as Breadcrumbs | undefined ;
112
113
113
114
const browserTracingOptions = browserTracingIntegration && browserTracingIntegration . options ;
114
115
115
116
const shouldTraceFetch = browserTracingOptions && browserTracingOptions . traceFetch ;
116
- const shouldAddFetchBreadcrumbs = breadcrumbsIntegration && breadcrumbsIntegration . options . fetch ;
117
+ const shouldAddFetchBreadcrumb = breadcrumbsIntegration && breadcrumbsIntegration . options . fetch ;
117
118
118
119
/* Identical check as in BrowserTracing, just that we also need to verify that BrowserTracing is actually installed */
119
120
const shouldCreateSpan =
@@ -132,9 +133,10 @@ function instrumentSvelteKitFetch(originalFetch: SvelteKitFetch): SvelteKitFetch
132
133
return new Proxy ( originalFetch , {
133
134
apply : ( wrappingTarget , thisArg , args : Parameters < LoadEvent [ 'fetch' ] > ) => {
134
135
const [ input , init ] = args ;
136
+
135
137
const rawUrl = getFetchUrl ( args ) ;
136
138
137
- const urlObject = new URL ( rawUrl , WINDOW . document . baseURI ) ;
139
+ const urlObject = parseUrl ( rawUrl ) ;
138
140
139
141
const requestData : SanitizedRequestData = {
140
142
url : getSanitizedUrlString ( urlObject ) ,
@@ -159,6 +161,7 @@ function instrumentSvelteKitFetch(originalFetch: SvelteKitFetch): SvelteKitFetch
159
161
// only attach headers if we should create a span
160
162
if ( attachHeaders && createSpan ) {
161
163
const dsc = activeTransaction . getDynamicSamplingContext ( ) ;
164
+
162
165
const headers = addTracingHeadersToFetchRequest (
163
166
input as string | Request ,
164
167
dsc ,
@@ -176,6 +179,8 @@ function instrumentSvelteKitFetch(originalFetch: SvelteKitFetch): SvelteKitFetch
176
179
177
180
let fetchPromise : Promise < Response > ;
178
181
182
+ const patchedFetchArgs = [ input , patchedInit ] ;
183
+
179
184
if ( createSpan ) {
180
185
fetchPromise = trace (
181
186
{
@@ -184,18 +189,18 @@ function instrumentSvelteKitFetch(originalFetch: SvelteKitFetch): SvelteKitFetch
184
189
data : requestData ,
185
190
} ,
186
191
span => {
187
- const promise : Promise < Response > = wrappingTarget . apply ( thisArg , [ input , patchedInit ] ) ;
192
+ const promise : Promise < Response > = wrappingTarget . apply ( thisArg , patchedFetchArgs ) ;
188
193
if ( span ) {
189
194
promise . then ( res => span . setHttpStatus ( res . status ) ) . catch ( _ => span . setStatus ( 'internal_error' ) ) ;
190
195
}
191
196
return promise ;
192
197
} ,
193
198
) ;
194
199
} else {
195
- fetchPromise = wrappingTarget . apply ( thisArg , [ input , patchedInit ] ) ;
200
+ fetchPromise = wrappingTarget . apply ( thisArg , patchedFetchArgs ) ;
196
201
}
197
202
198
- if ( shouldAddFetchBreadcrumbs ) {
203
+ if ( shouldAddFetchBreadcrumb ) {
199
204
addFetchBreadcrumb ( fetchPromise , requestData , args ) ;
200
205
}
201
206
0 commit comments