11import type { Client , HandlerDataFetch , Scope , Span , SpanOrigin } from '@sentry/types' ;
22import { BAGGAGE_HEADER_NAME , SENTRY_BAGGAGE_KEY_PREFIX , isInstanceOf , parseUrl } from '@sentry/utils' ;
3- import { getClient , getCurrentScope } from './currentScopes' ;
43import { SEMANTIC_ATTRIBUTE_SENTRY_OP , SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from './semanticAttributes' ;
5- import { SPAN_STATUS_ERROR , getSentryHeaders , setHttpStatus , startInactiveSpan } from './tracing' ;
4+ import { SPAN_STATUS_ERROR , setHttpStatus , startInactiveSpan } from './tracing' ;
65import { SentryNonRecordingSpan } from './tracing/sentryNonRecordingSpan' ;
76import { hasTracingEnabled } from './utils/hasTracingEnabled' ;
87import { getActiveSpan } from './utils/spanUtils' ;
8+ import { getTraceData } from './utils/traceData' ;
99
1010type PolymorphicRequestHeaders =
1111 | Record < string , string | undefined >
@@ -50,9 +50,6 @@ export function instrumentFetchRequest(
5050 return undefined ;
5151 }
5252
53- const scope = getCurrentScope ( ) ;
54- const client = getClient ( ) ;
55-
5653 const { method, url } = handlerData . fetchData ;
5754
5855 const fullUrl = getFullURL ( url ) ;
@@ -79,7 +76,7 @@ export function instrumentFetchRequest(
7976 handlerData . fetchData . __span = span . spanContext ( ) . spanId ;
8077 spans [ span . spanContext ( ) . spanId ] = span ;
8178
82- if ( shouldAttachHeaders ( handlerData . fetchData . url ) && client ) {
79+ if ( shouldAttachHeaders ( handlerData . fetchData . url ) ) {
8380 const request : string | Request = handlerData . args [ 0 ] ;
8481
8582 // In case the user hasn't set the second argument of a fetch call we default it to `{}`.
@@ -88,10 +85,10 @@ export function instrumentFetchRequest(
8885 // eslint-disable-next-line @typescript-eslint/no-explicit-any
8986 const options : { [ key : string ] : any } = handlerData . args [ 1 ] ;
9087
91- options . headers = addTracingHeadersToFetchRequest (
88+ options . headers = _addTracingHeadersToFetchRequest (
9289 request ,
93- client ,
94- scope ,
90+ undefined ,
91+ undefined ,
9592 options ,
9693 // If performance is disabled (TWP) or there's no active root span (pageload/navigation/interaction),
9794 // we do not want to use the span as base for the trace headers,
@@ -104,12 +101,19 @@ export function instrumentFetchRequest(
104101}
105102
106103/**
107- * Adds sentry-trace and baggage headers to the various forms of fetch headers
104+ * Adds sentry-trace and baggage headers to the various forms of fetch headers.
105+ *
106+ * @deprecated This function will not be exported anymore in v9.
107+ */
108+ export const addTracingHeadersToFetchRequest = _addTracingHeadersToFetchRequest ;
109+
110+ /**
111+ * Adds sentry-trace and baggage headers to the various forms of fetch headers.
108112 */
109- export function addTracingHeadersToFetchRequest (
113+ function _addTracingHeadersToFetchRequest (
110114 request : string | unknown , // unknown is actually type Request but we can't export DOM types from this package,
111- client : Client ,
112- scope : Scope ,
115+ _client : Client | undefined ,
116+ _scope : Scope | undefined ,
113117 fetchOptionsObj : {
114118 headers ?:
115119 | {
@@ -119,18 +123,22 @@ export function addTracingHeadersToFetchRequest(
119123 } ,
120124 span ?: Span ,
121125) : PolymorphicRequestHeaders | undefined {
122- const { sentryTrace, baggage } = getSentryHeaders ( { span, client, scope } ) ;
126+ const traceHeaders = getTraceData ( { span } ) ;
127+ const sentryTrace = traceHeaders [ 'sentry-trace' ] ;
128+ const baggage = traceHeaders . baggage ;
123129
124130 const headers =
125131 fetchOptionsObj . headers ||
126132 ( typeof Request !== 'undefined' && isInstanceOf ( request , Request ) ? ( request as Request ) . headers : undefined ) ;
127133
128134 if ( ! headers ) {
129- return { 'sentry-trace' : sentryTrace , baggage } ;
135+ return { ... traceHeaders } ;
130136 } else if ( typeof Headers !== 'undefined' && isInstanceOf ( headers , Headers ) ) {
131137 const newHeaders = new Headers ( headers as Headers ) ;
132138
133- newHeaders . set ( 'sentry-trace' , sentryTrace ) ;
139+ if ( sentryTrace ) {
140+ newHeaders . set ( 'sentry-trace' , sentryTrace ) ;
141+ }
134142
135143 if ( baggage ) {
136144 const prevBaggageHeader = newHeaders . get ( BAGGAGE_HEADER_NAME ) ;
0 commit comments