Skip to content

Commit 6246e3a

Browse files
committed
ref(node): Extract propagation context from request
1 parent 6b4b044 commit 6246e3a

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

packages/node/src/handlers.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ import type { Span } from '@sentry/types';
1111
import type { AddRequestDataToEventOptions } from '@sentry/utils';
1212
import {
1313
addRequestDataToTransaction,
14-
baggageHeaderToDynamicSamplingContext,
1514
dropUndefinedKeys,
1615
extractPathForTransaction,
17-
extractTraceparentData,
1816
isString,
1917
logger,
2018
normalize,
19+
tracingContextFromHeaders,
2120
} from '@sentry/utils';
2221
import type * as http from 'http';
2322

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

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

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

packages/node/test/handlers.test.ts

Lines changed: 27 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,28 @@ 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+
console.log(getPropagationContext());
282+
283+
expect(getPropagationContext()).toEqual({
284+
traceId: '12312012123120121231201212312012',
285+
parentSpanId: '1121201211212012',
286+
spanId: expect.any(String),
287+
sampled: true,
288+
dsc: { version: '1.0', environment: 'production' },
289+
});
290+
269291
const transaction = (res as any).__sentry_transaction;
270292

271293
// since we have no tracesSampler defined, the default behavior (inherit if possible) applies
272294
expect(transaction.traceId).toEqual('12312012123120121231201212312012');
273295
expect(transaction.parentSpanId).toEqual('1121201211212012');
274-
expect(transaction.sampled).toEqual(false);
296+
expect(transaction.sampled).toEqual(true);
275297
expect(transaction.metadata?.dynamicSamplingContext).toStrictEqual({ version: '1.0', environment: 'production' });
276298
});
277299

@@ -283,6 +305,8 @@ describe('tracingHandler', () => {
283305

284306
sentryTracingMiddleware(req, res, next);
285307

308+
expect(getPropagationContext().dsc).toEqual({ version: '1.0', environment: 'production' });
309+
286310
const transaction = (res as any).__sentry_transaction;
287311
expect(transaction.metadata?.dynamicSamplingContext).toStrictEqual({ version: '1.0', environment: 'production' });
288312
});

0 commit comments

Comments
 (0)