1
1
import type { Client , HandlerDataFetch , Scope , Span , SpanOrigin } from '@sentry/types' ;
2
2
import { BAGGAGE_HEADER_NAME , SENTRY_BAGGAGE_KEY_PREFIX , isInstanceOf , parseUrl } from '@sentry/utils' ;
3
- import { getClient , getCurrentScope } from './currentScopes' ;
4
3
import { 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' ;
6
5
import { SentryNonRecordingSpan } from './tracing/sentryNonRecordingSpan' ;
7
6
import { hasTracingEnabled } from './utils/hasTracingEnabled' ;
8
7
import { getActiveSpan } from './utils/spanUtils' ;
8
+ import { getTraceData } from './utils/traceData' ;
9
9
10
10
type PolymorphicRequestHeaders =
11
11
| Record < string , string | undefined >
@@ -50,9 +50,6 @@ export function instrumentFetchRequest(
50
50
return undefined ;
51
51
}
52
52
53
- const scope = getCurrentScope ( ) ;
54
- const client = getClient ( ) ;
55
-
56
53
const { method, url } = handlerData . fetchData ;
57
54
58
55
const fullUrl = getFullURL ( url ) ;
@@ -79,7 +76,7 @@ export function instrumentFetchRequest(
79
76
handlerData . fetchData . __span = span . spanContext ( ) . spanId ;
80
77
spans [ span . spanContext ( ) . spanId ] = span ;
81
78
82
- if ( shouldAttachHeaders ( handlerData . fetchData . url ) && client ) {
79
+ if ( shouldAttachHeaders ( handlerData . fetchData . url ) ) {
83
80
const request : string | Request = handlerData . args [ 0 ] ;
84
81
85
82
// 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(
88
85
// eslint-disable-next-line @typescript-eslint/no-explicit-any
89
86
const options : { [ key : string ] : any } = handlerData . args [ 1 ] ;
90
87
91
- options . headers = addTracingHeadersToFetchRequest (
88
+ options . headers = _addTracingHeadersToFetchRequest (
92
89
request ,
93
- client ,
94
- scope ,
90
+ undefined ,
91
+ undefined ,
95
92
options ,
96
93
// If performance is disabled (TWP) or there's no active root span (pageload/navigation/interaction),
97
94
// we do not want to use the span as base for the trace headers,
@@ -104,12 +101,19 @@ export function instrumentFetchRequest(
104
101
}
105
102
106
103
/**
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.
108
112
*/
109
- export function addTracingHeadersToFetchRequest (
113
+ function _addTracingHeadersToFetchRequest (
110
114
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 ,
113
117
fetchOptionsObj : {
114
118
headers ?:
115
119
| {
@@ -119,18 +123,22 @@ export function addTracingHeadersToFetchRequest(
119
123
} ,
120
124
span ?: Span ,
121
125
) : 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 ;
123
129
124
130
const headers =
125
131
fetchOptionsObj . headers ||
126
132
( typeof Request !== 'undefined' && isInstanceOf ( request , Request ) ? ( request as Request ) . headers : undefined ) ;
127
133
128
134
if ( ! headers ) {
129
- return { 'sentry-trace' : sentryTrace , baggage } ;
135
+ return { ... traceHeaders } ;
130
136
} else if ( typeof Headers !== 'undefined' && isInstanceOf ( headers , Headers ) ) {
131
137
const newHeaders = new Headers ( headers as Headers ) ;
132
138
133
- newHeaders . set ( 'sentry-trace' , sentryTrace ) ;
139
+ if ( sentryTrace ) {
140
+ newHeaders . set ( 'sentry-trace' , sentryTrace ) ;
141
+ }
134
142
135
143
if ( baggage ) {
136
144
const prevBaggageHeader = newHeaders . get ( BAGGAGE_HEADER_NAME ) ;
0 commit comments