1
+ /* eslint-disable complexity */
1
2
import { getClient } from './currentScopes' ;
2
3
import { SEMANTIC_ATTRIBUTE_SENTRY_OP , SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from './semanticAttributes' ;
3
4
import { SPAN_STATUS_ERROR , setHttpStatus , startInactiveSpan } from './tracing' ;
@@ -53,7 +54,20 @@ export function instrumentFetchRequest(
53
54
return undefined ;
54
55
}
55
56
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 ;
57
71
58
72
const hasParent = ! ! getActiveSpan ( ) ;
59
73
@@ -65,10 +79,11 @@ export function instrumentFetchRequest(
65
79
url,
66
80
type : 'fetch' ,
67
81
'http.method' : method ,
68
- 'http.url' : parsedUrl ?. href || url ,
82
+ 'http.url' : parsedUrl ?. href ,
69
83
[ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : spanOrigin ,
70
84
[ 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 } ) ,
72
87
...( parsedUrl ?. search && { 'http.query' : parsedUrl . search } ) ,
73
88
...( parsedUrl ?. hash && { 'http.fragment' : parsedUrl . hash } ) ,
74
89
} ,
0 commit comments