@@ -2,15 +2,15 @@ import type { BaseClient } from '@sentry/core';
22import { getCurrentHub , trace } from '@sentry/core' ;
33import type { Breadcrumbs , BrowserTracing } from '@sentry/svelte' ;
44import { captureException } from '@sentry/svelte' ;
5- import type { ClientOptions } from '@sentry/types' ;
5+ import type { ClientOptions , RequestSpanData } from '@sentry/types' ;
66import {
77 addExceptionMechanism ,
88 addTracingHeadersToFetchRequest ,
99 getFetchMethod ,
1010 getFetchUrl ,
11+ getSanitizedUrlString ,
1112 objectify ,
1213 stringMatchesSomePattern ,
13- stripUrlQueryAndFragment ,
1414} from '@sentry/utils' ;
1515import type { LoadEvent } from '@sveltejs/kit' ;
1616
@@ -133,23 +133,25 @@ function instrumentSvelteKitFetch(originalFetch: SvelteKitFetch): SvelteKitFetch
133133 apply : ( wrappingTarget , thisArg , args : Parameters < LoadEvent [ 'fetch' ] > ) => {
134134 const [ input , init ] = args ;
135135 const rawUrl = getFetchUrl ( args ) ;
136- const sanitizedUrl = stripUrlQueryAndFragment ( rawUrl ) ;
136+ const urlObject = new URL ( rawUrl ) ;
137+ const sanitizedUrl = getSanitizedUrlString ( urlObject ) ;
137138 const method = getFetchMethod ( args ) ;
138139
139140 // TODO: extract this to a util function (and use it in breadcrumbs integration as well)
140141 if ( rawUrl . match ( / s e n t r y _ k e y / ) && method === 'POST' ) {
141- // We will not create breadcrumbs for fetch requests that contain `sentry_key` (internal sentry requests)
142+ // We don't create spans or breadcrumbs for fetch requests that contain `sentry_key` (internal sentry requests)
142143 return wrappingTarget . apply ( thisArg , args ) ;
143144 }
144145
145146 const patchedInit : RequestInit = { ...init } || { } ;
146147 const activeSpan = getCurrentHub ( ) . getScope ( ) . getSpan ( ) ;
147148 const activeTransaction = activeSpan && activeSpan . transaction ;
148149
149- const attachHeaders = shouldAttachHeaders ( rawUrl ) ;
150- const attachSpan = shouldCreateSpan ( rawUrl ) ;
150+ const attachHeaders = activeTransaction && shouldAttachHeaders ( rawUrl ) ;
151+ const createSpan = activeTransaction && shouldCreateSpan ( rawUrl ) ;
151152
152- if ( attachHeaders && attachSpan && activeTransaction ) {
153+ // only attach headers if we should create a span
154+ if ( attachHeaders && createSpan ) {
153155 const dsc = activeTransaction . getDynamicSamplingContext ( ) ;
154156 const headers = addTracingHeadersToFetchRequest (
155157 input as string | Request ,
@@ -168,13 +170,20 @@ function instrumentSvelteKitFetch(originalFetch: SvelteKitFetch): SvelteKitFetch
168170
169171 let fetchPromise : Promise < Response > ;
170172
171- if ( attachSpan ) {
173+ if ( createSpan ) {
174+ const spanData : RequestSpanData = {
175+ url : sanitizedUrl ,
176+ method,
177+ 'http.query' : urlObject . search ,
178+ 'http.fragment' : urlObject . hash ,
179+ } ;
180+
172181 fetchPromise = trace (
173182 {
174183 name : `${ method } ${ sanitizedUrl } ` , // this will become the description of the span
175184 op : 'http.client' ,
176185 data : {
177- /* TODO: extract query data (we might actually only do this once we tackle sanitization on the browser-side) */
186+ ... spanData ,
178187 } ,
179188 parentSpanId : activeSpan && activeSpan . spanId ,
180189 } ,
0 commit comments