Skip to content

Commit 299e93b

Browse files
committed
feat(remix): Continue transaction from request headers
1 parent 51c016f commit 299e93b

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

packages/remix/src/utils/instrumentServer.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22
import { captureException, getCurrentHub, Hub } from '@sentry/node';
33
import { getActiveTransaction, hasTracingEnabled } from '@sentry/tracing';
44
import { Transaction, WrappedFunction } from '@sentry/types';
5-
import { addExceptionMechanism, fill, isNodeEnv, loadModule, logger, serializeBaggage } from '@sentry/utils';
5+
import {
6+
addExceptionMechanism,
7+
extractTraceparentData,
8+
fill,
9+
isNodeEnv,
10+
loadModule,
11+
logger,
12+
parseBaggageSetMutability,
13+
serializeBaggage,
14+
} from '@sentry/utils';
615
import * as domain from 'domain';
716

817
import {
@@ -289,26 +298,38 @@ function matchServerRoutes(
289298
* @param pkg
290299
*/
291300
export function startRequestHandlerTransaction(
301+
hub: Hub,
292302
url: URL,
293-
method: string,
294303
routes: ServerRoute[],
295-
hub: Hub,
296-
pkg?: ReactRouterDomPkg,
304+
pkg: ReactRouterDomPkg | undefined,
305+
request: {
306+
headers: {
307+
'sentry-trace': string;
308+
baggage: string;
309+
};
310+
method: string;
311+
},
297312
): Transaction {
298313
const currentScope = hub.getScope();
299314
const matches = matchServerRoutes(routes, url.pathname, pkg);
300315

316+
// If there is a trace header set, we extract the data from it (parentSpanId, traceId, and sampling decision)
317+
const traceparentData = extractTraceparentData(request.headers['sentry-trace']);
318+
const baggage = parseBaggageSetMutability(request.headers.baggage, traceparentData);
319+
301320
const match = matches && getRequestMatch(url, matches);
302321
const name = match === null ? url.pathname : match.route.id;
303322
const source = match === null ? 'url' : 'route';
304323
const transaction = hub.startTransaction({
305324
name,
306325
op: 'http.server',
307326
tags: {
308-
method: method,
327+
method: request.method,
309328
},
329+
...traceparentData,
310330
metadata: {
311331
source,
332+
baggage,
312333
},
313334
});
314335

@@ -330,7 +351,13 @@ function wrapRequestHandler(origRequestHandler: RequestHandler, build: ServerBui
330351
}
331352

332353
const url = new URL(request.url);
333-
const transaction = startRequestHandlerTransaction(url, request.method, routes, hub, pkg);
354+
const transaction = startRequestHandlerTransaction(hub, url, routes, pkg, {
355+
headers: {
356+
'sentry-trace': request.headers.get('sentry-trace') || '',
357+
baggage: request.headers.get('baggage') || '',
358+
},
359+
method: request.method,
360+
});
334361

335362
const res = (await origRequestHandler.call(this, request, loadContext)) as Response;
336363

packages/remix/src/utils/serverAdapters/express.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getCurrentHub } from '@sentry/hub';
22
import { flush } from '@sentry/node';
33
import { hasTracingEnabled } from '@sentry/tracing';
44
import { Transaction } from '@sentry/types';
5-
import { extractRequestData, loadModule, logger } from '@sentry/utils';
5+
import { extractRequestData, isString, loadModule, logger } from '@sentry/utils';
66

77
import {
88
createRoutes,
@@ -51,7 +51,13 @@ function wrapExpressRequestHandler(
5151
}
5252

5353
const url = new URL(request.url);
54-
const transaction = startRequestHandlerTransaction(url, request.method, routes, hub, pkg);
54+
const transaction = startRequestHandlerTransaction(hub, url, routes, pkg, {
55+
headers: {
56+
'sentry-trace': (req.headers && isString(req.headers['sentry-trace']) && req.headers['sentry-trace']) || '',
57+
baggage: (req.headers && isString(req.headers.baggage) && req.headers.baggage) || '',
58+
},
59+
method: request.method,
60+
});
5561
// save a link to the transaction on the response, so that even if there's an error (landing us outside of
5662
// the domain), we can still finish it (albeit possibly missing some scope data)
5763
(res as AugmentedExpressResponse).__sentryTransaction = transaction;

0 commit comments

Comments
 (0)