1+ /* eslint-disable complexity */
12import { getClient } from './currentScopes' ;
23import { SEMANTIC_ATTRIBUTE_SENTRY_OP , SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from './semanticAttributes' ;
34import { SPAN_STATUS_ERROR , setHttpStatus , startInactiveSpan } from './tracing' ;
@@ -53,7 +54,20 @@ export function instrumentFetchRequest(
5354 return undefined ;
5455 }
5556
56- const parsedUrl = parseStringToURL ( url ) ;
57+ // Curious about `thismessage:/`? See: https://www.rfc-editor.org/rfc/rfc2557.html
58+ // > When the methods above do not yield an absolute URI, a base URL
59+ // > of "thismessage:/" MUST be employed. This base URL has been
60+ // > defined for the sole purpose of resolving relative references
61+ // > within a multipart/related structure when no other base URI is
62+ // > specified.
63+ //
64+ // We need to provide a base URL to `parseStringToURL` because the fetch API gives us a
65+ // relative URL sometimes.
66+ //
67+ // This is the only case where we need to provide a base URL to `parseStringToURL`
68+ // because the relative URL is not valid on its own.
69+ const parsedUrl = url . startsWith ( '/' ) ? parseStringToURL ( url , 'thismessage:/' ) : parseStringToURL ( url ) ;
70+ const fullUrl = url . startsWith ( '/' ) ? undefined : parsedUrl ?. href ;
5771
5872 const hasParent = ! ! getActiveSpan ( ) ;
5973
@@ -65,10 +79,11 @@ export function instrumentFetchRequest(
6579 url,
6680 type : 'fetch' ,
6781 'http.method' : method ,
68- 'http.url' : parsedUrl ?. href || url ,
82+ 'http.url' : parsedUrl ?. href ,
6983 [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : spanOrigin ,
7084 [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'http.client' ,
71- ...( parsedUrl ?. host && { 'server.address' : parsedUrl . host } ) ,
85+ ...( fullUrl && { 'http.url' : fullUrl } ) ,
86+ ...( fullUrl && parsedUrl ?. host && { 'server.address' : parsedUrl . host } ) ,
7287 ...( parsedUrl ?. search && { 'http.query' : parsedUrl . search } ) ,
7388 ...( parsedUrl ?. hash && { 'http.fragment' : parsedUrl . hash } ) ,
7489 } ,
0 commit comments