11import type { BaseClient } from '@sentry/core' ;
22import { getCurrentHub , trace } from '@sentry/core' ;
33import type { Breadcrumbs , BrowserTracing } from '@sentry/svelte' ;
4- import { captureException , WINDOW } from '@sentry/svelte' ;
4+ import { captureException } from '@sentry/svelte' ;
55import type { ClientOptions , SanitizedRequestData } from '@sentry/types' ;
66import {
77 addExceptionMechanism ,
@@ -10,6 +10,7 @@ import {
1010 getFetchUrl ,
1111 getSanitizedUrlString ,
1212 objectify ,
13+ parseUrl ,
1314 stringMatchesSomePattern ,
1415} from '@sentry/utils' ;
1516import type { LoadEvent } from '@sveltejs/kit' ;
@@ -108,12 +109,12 @@ function instrumentSvelteKitFetch(originalFetch: SvelteKitFetch): SvelteKitFetch
108109
109110 const browserTracingIntegration =
110111 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 ;
112113
113114 const browserTracingOptions = browserTracingIntegration && browserTracingIntegration . options ;
114115
115116 const shouldTraceFetch = browserTracingOptions && browserTracingOptions . traceFetch ;
116- const shouldAddFetchBreadcrumbs = breadcrumbsIntegration && breadcrumbsIntegration . options . fetch ;
117+ const shouldAddFetchBreadcrumb = breadcrumbsIntegration && breadcrumbsIntegration . options . fetch ;
117118
118119 /* Identical check as in BrowserTracing, just that we also need to verify that BrowserTracing is actually installed */
119120 const shouldCreateSpan =
@@ -132,9 +133,10 @@ function instrumentSvelteKitFetch(originalFetch: SvelteKitFetch): SvelteKitFetch
132133 return new Proxy ( originalFetch , {
133134 apply : ( wrappingTarget , thisArg , args : Parameters < LoadEvent [ 'fetch' ] > ) => {
134135 const [ input , init ] = args ;
136+
135137 const rawUrl = getFetchUrl ( args ) ;
136138
137- const urlObject = new URL ( rawUrl , WINDOW . document . baseURI ) ;
139+ const urlObject = parseUrl ( rawUrl ) ;
138140
139141 const requestData : SanitizedRequestData = {
140142 url : getSanitizedUrlString ( urlObject ) ,
@@ -159,6 +161,7 @@ function instrumentSvelteKitFetch(originalFetch: SvelteKitFetch): SvelteKitFetch
159161 // only attach headers if we should create a span
160162 if ( attachHeaders && createSpan ) {
161163 const dsc = activeTransaction . getDynamicSamplingContext ( ) ;
164+
162165 const headers = addTracingHeadersToFetchRequest (
163166 input as string | Request ,
164167 dsc ,
@@ -176,6 +179,8 @@ function instrumentSvelteKitFetch(originalFetch: SvelteKitFetch): SvelteKitFetch
176179
177180 let fetchPromise : Promise < Response > ;
178181
182+ const patchedFetchArgs = [ input , patchedInit ] ;
183+
179184 if ( createSpan ) {
180185 fetchPromise = trace (
181186 {
@@ -184,18 +189,18 @@ function instrumentSvelteKitFetch(originalFetch: SvelteKitFetch): SvelteKitFetch
184189 data : requestData ,
185190 } ,
186191 span => {
187- const promise : Promise < Response > = wrappingTarget . apply ( thisArg , [ input , patchedInit ] ) ;
192+ const promise : Promise < Response > = wrappingTarget . apply ( thisArg , patchedFetchArgs ) ;
188193 if ( span ) {
189194 promise . then ( res => span . setHttpStatus ( res . status ) ) . catch ( _ => span . setStatus ( 'internal_error' ) ) ;
190195 }
191196 return promise ;
192197 } ,
193198 ) ;
194199 } else {
195- fetchPromise = wrappingTarget . apply ( thisArg , [ input , patchedInit ] ) ;
200+ fetchPromise = wrappingTarget . apply ( thisArg , patchedFetchArgs ) ;
196201 }
197202
198- if ( shouldAddFetchBreadcrumbs ) {
203+ if ( shouldAddFetchBreadcrumb ) {
199204 addFetchBreadcrumb ( fetchPromise , requestData , args ) ;
200205 }
201206
0 commit comments