|
1 | 1 | import type { Hub } from '@sentry/core';
|
2 | 2 | import { getCurrentHub, getDynamicSamplingContextFromClient } from '@sentry/core';
|
3 |
| -import type { EventProcessor, Integration, SanitizedRequestData, TracePropagationTargets } from '@sentry/types'; |
| 3 | +import type { |
| 4 | + DynamicSamplingContext, |
| 5 | + EventProcessor, |
| 6 | + Integration, |
| 7 | + SanitizedRequestData, |
| 8 | + TracePropagationTargets, |
| 9 | +} from '@sentry/types'; |
4 | 10 | import {
|
5 | 11 | dynamicSamplingContextToSentryBaggageHeader,
|
6 | 12 | fill,
|
@@ -244,42 +250,15 @@ function _createWrappedRequestMethodFactory(
|
244 | 250 | if (shouldAttachTraceData(rawRequestUrl)) {
|
245 | 251 | if (requestSpan) {
|
246 | 252 | const sentryTraceHeader = requestSpan.toTraceparent();
|
247 |
| - __DEBUG_BUILD__ && |
248 |
| - logger.log( |
249 |
| - `[Tracing] Adding sentry-trace header ${sentryTraceHeader} to outgoing request to "${requestUrl}": `, |
250 |
| - ); |
251 | 253 | const dynamicSamplingContext = requestSpan?.transaction?.getDynamicSamplingContext();
|
252 |
| - const sentryBaggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext); |
253 |
| - const sentryBaggageHeader = normalizeBaggageHeader(requestOptions, sentryBaggage); |
254 |
| - |
255 |
| - requestOptions.headers = { |
256 |
| - ...requestOptions.headers, |
257 |
| - 'sentry-trace': sentryTraceHeader, |
258 |
| - // Setting a header to `undefined` will crash in node so we only set the baggage header when it's defined |
259 |
| - ...(sentryBaggageHeader && { baggage: sentryBaggageHeader }), |
260 |
| - }; |
| 254 | + addHeadersToRequestOptions(requestOptions, requestUrl, sentryTraceHeader, dynamicSamplingContext); |
261 | 255 | } else {
|
| 256 | + const client = hub.getClient(); |
262 | 257 | const { traceId, sampled, dsc } = scope.getPropagationContext();
|
263 | 258 | const sentryTraceHeader = generateSentryTraceHeader(traceId, undefined, sampled);
|
264 |
| - __DEBUG_BUILD__ && |
265 |
| - logger.log( |
266 |
| - `[Tracing] Adding sentry-trace header ${sentryTraceHeader} to outgoing request to "${requestUrl}": `, |
267 |
| - ); |
268 |
| - requestOptions.headers = { |
269 |
| - ...requestOptions.headers, |
270 |
| - 'sentry-trace': sentryTraceHeader, |
271 |
| - }; |
272 |
| - const client = hub.getClient(); |
273 |
| - if (client) { |
274 |
| - const dynamicSamplingContext = dsc || getDynamicSamplingContextFromClient(traceId, client, scope); |
275 |
| - const sentryBaggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext); |
276 |
| - const sentryBaggageHeader = normalizeBaggageHeader(requestOptions, sentryBaggage); |
277 |
| - requestOptions.headers = { |
278 |
| - ...requestOptions.headers, |
279 |
| - // Setting a header to `undefined` will crash in node so we only set the baggage header when it's defined |
280 |
| - ...(sentryBaggageHeader && { baggage: sentryBaggageHeader }), |
281 |
| - }; |
282 |
| - } |
| 259 | + const dynamicSamplingContext = |
| 260 | + dsc || (client ? getDynamicSamplingContextFromClient(traceId, client, scope) : undefined); |
| 261 | + addHeadersToRequestOptions(requestOptions, requestUrl, sentryTraceHeader, dynamicSamplingContext); |
283 | 262 | }
|
284 | 263 | } else {
|
285 | 264 | __DEBUG_BUILD__ &&
|
@@ -322,6 +301,24 @@ function _createWrappedRequestMethodFactory(
|
322 | 301 | };
|
323 | 302 | }
|
324 | 303 |
|
| 304 | +function addHeadersToRequestOptions( |
| 305 | + requestOptions: RequestOptions, |
| 306 | + requestUrl: string, |
| 307 | + sentryTraceHeader: string, |
| 308 | + dynamicSamplingContext: Partial<DynamicSamplingContext> | undefined, |
| 309 | +): void { |
| 310 | + __DEBUG_BUILD__ && |
| 311 | + logger.log(`[Tracing] Adding sentry-trace header ${sentryTraceHeader} to outgoing request to "${requestUrl}": `); |
| 312 | + const sentryBaggage = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext); |
| 313 | + const sentryBaggageHeader = normalizeBaggageHeader(requestOptions, sentryBaggage); |
| 314 | + requestOptions.headers = { |
| 315 | + ...requestOptions.headers, |
| 316 | + 'sentry-trace': sentryTraceHeader, |
| 317 | + // Setting a header to `undefined` will crash in node so we only set the baggage header when it's defined |
| 318 | + ...(sentryBaggageHeader && { baggage: sentryBaggageHeader }), |
| 319 | + }; |
| 320 | +} |
| 321 | + |
325 | 322 | function getRequestSpanData(requestUrl: string, requestOptions: RequestOptions): SanitizedRequestData {
|
326 | 323 | const method = requestOptions.method || 'GET';
|
327 | 324 | const data: SanitizedRequestData = {
|
|
0 commit comments