Skip to content

Commit 1d46575

Browse files
committed
refactor logic
1 parent c3cf0d8 commit 1d46575

File tree

1 file changed

+30
-33
lines changed
  • packages/node/src/integrations

1 file changed

+30
-33
lines changed

packages/node/src/integrations/http.ts

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import type { Hub } from '@sentry/core';
22
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';
410
import {
511
dynamicSamplingContextToSentryBaggageHeader,
612
fill,
@@ -244,42 +250,15 @@ function _createWrappedRequestMethodFactory(
244250
if (shouldAttachTraceData(rawRequestUrl)) {
245251
if (requestSpan) {
246252
const sentryTraceHeader = requestSpan.toTraceparent();
247-
__DEBUG_BUILD__ &&
248-
logger.log(
249-
`[Tracing] Adding sentry-trace header ${sentryTraceHeader} to outgoing request to "${requestUrl}": `,
250-
);
251253
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);
261255
} else {
256+
const client = hub.getClient();
262257
const { traceId, sampled, dsc } = scope.getPropagationContext();
263258
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);
283262
}
284263
} else {
285264
__DEBUG_BUILD__ &&
@@ -322,6 +301,24 @@ function _createWrappedRequestMethodFactory(
322301
};
323302
}
324303

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+
325322
function getRequestSpanData(requestUrl: string, requestOptions: RequestOptions): SanitizedRequestData {
326323
const method = requestOptions.method || 'GET';
327324
const data: SanitizedRequestData = {

0 commit comments

Comments
 (0)