Skip to content

Commit a3178f7

Browse files
authored
ref(node): Extract propagation context in tracingHandler (#8425)
1 parent 9ede8a3 commit a3178f7

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

packages/node/src/handlers.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ import type { AddRequestDataToEventOptions } from '@sentry/utils';
1212
import {
1313
addExceptionMechanism,
1414
addRequestDataToTransaction,
15-
baggageHeaderToDynamicSamplingContext,
1615
dropUndefinedKeys,
1716
extractPathForTransaction,
18-
extractTraceparentData,
1917
isString,
2018
logger,
2119
normalize,
20+
tracingContextFromHeaders,
2221
} from '@sentry/utils';
2322
import type * as http from 'http';
2423

@@ -63,11 +62,13 @@ export function tracingHandler(): (
6362
return next();
6463
}
6564

66-
// If there is a trace header set, we extract the data from it (parentSpanId, traceId, and sampling decision)
67-
const traceparentData =
68-
req.headers && isString(req.headers['sentry-trace']) && extractTraceparentData(req.headers['sentry-trace']);
69-
const incomingBaggageHeaders = req.headers?.baggage;
70-
const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(incomingBaggageHeaders);
65+
const sentryTrace = req.headers && isString(req.headers['sentry-trace']) ? req.headers['sentry-trace'] : undefined;
66+
const baggage = req.headers?.baggage;
67+
const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders(
68+
sentryTrace,
69+
baggage,
70+
);
71+
hub.getScope().setPropagationContext(propagationContext);
7172

7273
const [name, source] = extractPathForTransaction(req, { path: true, method: true });
7374
const transaction = startTransaction(

packages/node/test/handlers.test.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Hub } from '@sentry/core';
22
import * as sentryCore from '@sentry/core';
33
import { setAsyncContextStrategy, Transaction } from '@sentry/core';
4-
import type { Event } from '@sentry/types';
4+
import type { Event, PropagationContext } from '@sentry/types';
55
import { SentryError } from '@sentry/utils';
66
import * as http from 'http';
77

@@ -209,6 +209,11 @@ describe('tracingHandler', () => {
209209
jest.restoreAllMocks();
210210
});
211211

212+
function getPropagationContext(): PropagationContext {
213+
// @ts-expect-error accesing private property for test
214+
return hub.getScope()._propagationContext;
215+
}
216+
212217
it('creates a transaction when handling a request', () => {
213218
const startTransaction = jest.spyOn(sentryCore, 'startTransaction');
214219

@@ -251,6 +256,13 @@ describe('tracingHandler', () => {
251256

252257
const transaction = (res as any).__sentry_transaction;
253258

259+
expect(getPropagationContext()).toEqual({
260+
traceId: '12312012123120121231201212312012',
261+
parentSpanId: '1121201211212012',
262+
spanId: expect.any(String),
263+
sampled: false,
264+
});
265+
254266
// since we have no tracesSampler defined, the default behavior (inherit if possible) applies
255267
expect(transaction.traceId).toEqual('12312012123120121231201212312012');
256268
expect(transaction.parentSpanId).toEqual('1121201211212012');
@@ -260,18 +272,26 @@ describe('tracingHandler', () => {
260272

261273
it("pulls parent's data from tracing and baggage headers on the request", () => {
262274
req.headers = {
263-
'sentry-trace': '12312012123120121231201212312012-1121201211212012-0',
275+
'sentry-trace': '12312012123120121231201212312012-1121201211212012-1',
264276
baggage: 'sentry-version=1.0,sentry-environment=production',
265277
};
266278

267279
sentryTracingMiddleware(req, res, next);
268280

281+
expect(getPropagationContext()).toEqual({
282+
traceId: '12312012123120121231201212312012',
283+
parentSpanId: '1121201211212012',
284+
spanId: expect.any(String),
285+
sampled: true,
286+
dsc: { version: '1.0', environment: 'production' },
287+
});
288+
269289
const transaction = (res as any).__sentry_transaction;
270290

271291
// since we have no tracesSampler defined, the default behavior (inherit if possible) applies
272292
expect(transaction.traceId).toEqual('12312012123120121231201212312012');
273293
expect(transaction.parentSpanId).toEqual('1121201211212012');
274-
expect(transaction.sampled).toEqual(false);
294+
expect(transaction.sampled).toEqual(true);
275295
expect(transaction.metadata?.dynamicSamplingContext).toStrictEqual({ version: '1.0', environment: 'production' });
276296
});
277297

@@ -283,6 +303,8 @@ describe('tracingHandler', () => {
283303

284304
sentryTracingMiddleware(req, res, next);
285305

306+
expect(getPropagationContext().dsc).toEqual({ version: '1.0', environment: 'production' });
307+
286308
const transaction = (res as any).__sentry_transaction;
287309
expect(transaction.metadata?.dynamicSamplingContext).toStrictEqual({ version: '1.0', environment: 'production' });
288310
});

0 commit comments

Comments
 (0)