Skip to content

Commit 25885df

Browse files
committed
read tracestate data from incoming node requests
1 parent 19f0bdf commit 25885df

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

packages/node/src/handlers.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable max-lines */
22
/* eslint-disable @typescript-eslint/no-explicit-any */
33
import { captureException, getCurrentHub, startTransaction, withScope } from '@sentry/core';
4-
import { extractSentrytraceData, Span } from '@sentry/tracing';
4+
import { extractSentrytraceData, extractTracestateData, Span } from '@sentry/tracing';
55
import { Event, ExtractedNodeRequestData, Transaction } from '@sentry/types';
66
import { forget, isPlainObject, isString, logger, normalize, stripUrlQueryAndFragment } from '@sentry/utils';
77
import * as cookie from 'cookie';
@@ -55,17 +55,21 @@ export function tracingHandler(): (
5555
res: http.ServerResponse,
5656
next: (error?: any) => void,
5757
): void {
58-
// If there is a trace header set, we extract the data from it (parentSpanId, traceId, and sampling decision)
59-
let traceparentData;
60-
if (req.headers && isString(req.headers['sentry-trace'])) {
58+
// Extract data from trace headers
59+
let traceparentData, tracestateData;
60+
if (req.headers?.['sentry-trace']) {
6161
traceparentData = extractSentrytraceData(req.headers['sentry-trace'] as string);
6262
}
63+
if (req.headers?.tracestate) {
64+
tracestateData = extractTracestateData(req.headers.tracestate as string);
65+
}
6366

6467
const transaction = startTransaction(
6568
{
6669
name: extractExpressTransactionName(req, { path: true, method: true }),
6770
op: 'http.server',
6871
...traceparentData,
72+
metadata: { tracestate: tracestateData },
6973
},
7074
{ request: extractRequestData(req) },
7175
);

packages/node/test/handlers.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,11 @@ describe('tracingHandler', () => {
221221
expect(startTransaction).toHaveBeenCalled();
222222
});
223223

224-
it("pulls parent's data from tracing header on the request", () => {
225-
req.headers = { 'sentry-trace': '12312012123120121231201212312012-1121201211212012-0' };
224+
it('pulls data from tracing headers on the request', () => {
225+
req.headers = {
226+
'sentry-trace': '12312012123120121231201212312012-1121201211212012-0',
227+
tracestate: 'sentry=doGsaREgReaT',
228+
};
226229

227230
sentryTracingMiddleware(req, res, next);
228231

@@ -232,6 +235,7 @@ describe('tracingHandler', () => {
232235
expect(transaction.traceId).toEqual('12312012123120121231201212312012');
233236
expect(transaction.parentSpanId).toEqual('1121201211212012');
234237
expect(transaction.sampled).toEqual(false);
238+
expect(transaction.metadata?.tracestate).toEqual({ sentry: 'sentry=doGsaREgReaT' });
235239
});
236240

237241
it('extracts request data for sampling context', () => {

0 commit comments

Comments
 (0)