diff --git a/packages/node/src/handlers.ts b/packages/node/src/handlers.ts index 95a4cfe65e38..0f2ffca1b502 100644 --- a/packages/node/src/handlers.ts +++ b/packages/node/src/handlers.ts @@ -11,13 +11,12 @@ import type { Span } from '@sentry/types'; import type { AddRequestDataToEventOptions } from '@sentry/utils'; import { addRequestDataToTransaction, - baggageHeaderToDynamicSamplingContext, dropUndefinedKeys, extractPathForTransaction, - extractTraceparentData, isString, logger, normalize, + tracingContextFromHeaders, } from '@sentry/utils'; import type * as http from 'http'; @@ -62,11 +61,13 @@ export function tracingHandler(): ( return next(); } - // If there is a trace header set, we extract the data from it (parentSpanId, traceId, and sampling decision) - const traceparentData = - req.headers && isString(req.headers['sentry-trace']) && extractTraceparentData(req.headers['sentry-trace']); - const incomingBaggageHeaders = req.headers?.baggage; - const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(incomingBaggageHeaders); + const sentryTrace = req.headers && isString(req.headers['sentry-trace']) ? req.headers['sentry-trace'] : undefined; + const baggage = req.headers?.baggage; + const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders( + sentryTrace, + baggage, + ); + hub.getScope().setPropagationContext(propagationContext); const [name, source] = extractPathForTransaction(req, { path: true, method: true }); const transaction = startTransaction( diff --git a/packages/node/test/handlers.test.ts b/packages/node/test/handlers.test.ts index 298d61cf1aac..d464342fe396 100644 --- a/packages/node/test/handlers.test.ts +++ b/packages/node/test/handlers.test.ts @@ -1,7 +1,7 @@ import type { Hub } from '@sentry/core'; import * as sentryCore from '@sentry/core'; import { setAsyncContextStrategy, Transaction } from '@sentry/core'; -import type { Event } from '@sentry/types'; +import type { Event, PropagationContext } from '@sentry/types'; import { SentryError } from '@sentry/utils'; import * as http from 'http'; @@ -209,6 +209,11 @@ describe('tracingHandler', () => { jest.restoreAllMocks(); }); + function getPropagationContext(): PropagationContext { + // @ts-expect-error accesing private property for test + return hub.getScope()._propagationContext; + } + it('creates a transaction when handling a request', () => { const startTransaction = jest.spyOn(sentryCore, 'startTransaction'); @@ -251,6 +256,13 @@ describe('tracingHandler', () => { const transaction = (res as any).__sentry_transaction; + expect(getPropagationContext()).toEqual({ + traceId: '12312012123120121231201212312012', + parentSpanId: '1121201211212012', + spanId: expect.any(String), + sampled: false, + }); + // since we have no tracesSampler defined, the default behavior (inherit if possible) applies expect(transaction.traceId).toEqual('12312012123120121231201212312012'); expect(transaction.parentSpanId).toEqual('1121201211212012'); @@ -260,18 +272,26 @@ describe('tracingHandler', () => { it("pulls parent's data from tracing and baggage headers on the request", () => { req.headers = { - 'sentry-trace': '12312012123120121231201212312012-1121201211212012-0', + 'sentry-trace': '12312012123120121231201212312012-1121201211212012-1', baggage: 'sentry-version=1.0,sentry-environment=production', }; sentryTracingMiddleware(req, res, next); + expect(getPropagationContext()).toEqual({ + traceId: '12312012123120121231201212312012', + parentSpanId: '1121201211212012', + spanId: expect.any(String), + sampled: true, + dsc: { version: '1.0', environment: 'production' }, + }); + const transaction = (res as any).__sentry_transaction; // since we have no tracesSampler defined, the default behavior (inherit if possible) applies expect(transaction.traceId).toEqual('12312012123120121231201212312012'); expect(transaction.parentSpanId).toEqual('1121201211212012'); - expect(transaction.sampled).toEqual(false); + expect(transaction.sampled).toEqual(true); expect(transaction.metadata?.dynamicSamplingContext).toStrictEqual({ version: '1.0', environment: 'production' }); }); @@ -283,6 +303,8 @@ describe('tracingHandler', () => { sentryTracingMiddleware(req, res, next); + expect(getPropagationContext().dsc).toEqual({ version: '1.0', environment: 'production' }); + const transaction = (res as any).__sentry_transaction; expect(transaction.metadata?.dynamicSamplingContext).toStrictEqual({ version: '1.0', environment: 'production' }); });