Skip to content

Commit 8066250

Browse files
committed
fix(remix): Provide sentry-trace and baggage via root loader.
1 parent 6ea53ed commit 8066250

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

packages/remix/src/utils/instrumentServer.ts

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/* eslint-disable max-lines */
22
import { captureException, getCurrentHub } from '@sentry/node';
3-
import { getActiveTransaction } from '@sentry/tracing';
3+
import { getActiveTransaction, hasTracingEnabled } from '@sentry/tracing';
44
import {
55
addExceptionMechanism,
66
fill,
7+
isNodeEnv,
78
loadModule,
89
logger,
910
serializeBaggage,
@@ -79,6 +80,8 @@ interface HandleDataRequestFunction {
7980

8081
interface ServerEntryModule {
8182
default: HandleDocumentRequestFunction;
83+
meta: MetaFunction;
84+
loader: DataFunction;
8285
handleDataRequest?: HandleDataRequestFunction;
8386
}
8487

@@ -232,33 +235,30 @@ function makeWrappedLoader(origAction: DataFunction): DataFunction {
232235
return makeWrappedDataFunction(origAction, 'loader');
233236
}
234237

235-
function makeWrappedMeta(origMeta: MetaFunction | HtmlMetaDescriptor = {}): MetaFunction {
236-
return function (
237-
this: unknown,
238-
args: { data: AppData; parentsData: RouteData; params: Params; location: Location },
239-
): HtmlMetaDescriptor {
240-
let origMetaResult;
241-
if (origMeta instanceof Function) {
242-
origMetaResult = origMeta.call(this, args);
243-
} else {
244-
origMetaResult = origMeta;
245-
}
238+
function makeWrappedRootLoader(origLoader: DataFunction): DataFunction {
239+
return async function (this: unknown, args: DataFunctionArgs): Promise<Response | AppData> {
240+
let sentryTrace;
241+
let sentryBaggage;
246242

247-
const scope = getCurrentHub().getScope();
248-
if (scope) {
249-
const span = scope.getSpan();
250-
const transaction = getActiveTransaction();
251-
252-
if (span && transaction) {
253-
return {
254-
...origMetaResult,
255-
'sentry-trace': span.toTraceparent(),
256-
baggage: serializeBaggage(transaction.getBaggage()),
257-
};
243+
const activeTransaction = getActiveTransaction();
244+
const currentScope = getCurrentHub().getScope();
245+
246+
if (isNodeEnv() && hasTracingEnabled()) {
247+
if (currentScope) {
248+
const span = currentScope.getSpan();
249+
250+
if (span && activeTransaction) {
251+
sentryTrace = span.toTraceparent();
252+
sentryBaggage = serializeBaggage(activeTransaction.getBaggage());
253+
}
258254
}
259255
}
260256

261-
return origMetaResult;
257+
const res = await origLoader.call(this, args);
258+
259+
Object.assign(res, { sentryTrace, sentryBaggage });
260+
261+
return res;
262262
};
263263
}
264264

@@ -296,19 +296,33 @@ function makeWrappedCreateRequestHandler(
296296
return function (this: unknown, build: ServerBuild, mode: string | undefined): RequestHandler {
297297
const routes: ServerRouteManifest = {};
298298

299+
// Entry module should have a loader function to provide `sentry-trace` and `baggage`
300+
// They will be available for the root `meta` function as `data.sentryTrace` and `data.sentryBaggage`
301+
if (!build.entry.module.loader) {
302+
build.entry.module.loader = () => ({});
303+
}
304+
305+
build.entry.module.loader = makeWrappedRootLoader(build.entry.module.loader);
306+
299307
const wrappedEntry = { ...build.entry, module: { ...build.entry.module } };
300308

301309
fill(wrappedEntry.module, 'default', makeWrappedDocumentRequestFunction);
302310

303311
for (const [id, route] of Object.entries(build.routes)) {
304312
const wrappedRoute = { ...route, module: { ...route.module } };
305313

306-
fill(wrappedRoute.module, 'meta', makeWrappedMeta);
307-
308314
if (wrappedRoute.module.action) {
309315
fill(wrappedRoute.module, 'action', makeWrappedAction);
310316
}
311317

318+
if (!wrappedRoute.parentId) {
319+
if (!wrappedRoute.module.loader) {
320+
wrappedRoute.module.loader = () => ({});
321+
}
322+
323+
fill(wrappedRoute.module, 'loader', makeWrappedRootLoader);
324+
}
325+
312326
if (wrappedRoute.module.loader) {
313327
fill(wrappedRoute.module, 'loader', makeWrappedLoader);
314328
}

0 commit comments

Comments
 (0)