|
1 | 1 | /* eslint-disable max-lines */
|
2 | 2 | import { captureException, getCurrentHub } from '@sentry/node';
|
3 |
| -import { getActiveTransaction } from '@sentry/tracing'; |
4 |
| -import { addExceptionMechanism, fill, loadModule, logger, serializeBaggage } from '@sentry/utils'; |
| 3 | +import { getActiveTransaction, hasTracingEnabled } from '@sentry/tracing'; |
| 4 | +import { addExceptionMechanism, fill, isNodeEnv, loadModule, logger, serializeBaggage } from '@sentry/utils'; |
5 | 5 |
|
6 | 6 | // Types vendored from @remix-run/[email protected]:
|
7 | 7 | // https://github.com/remix-run/remix/blob/f3691d51027b93caa3fd2cdfe146d7b62a6eb8f2/packages/remix-server-runtime/server.ts
|
@@ -72,6 +72,8 @@ interface HandleDataRequestFunction {
|
72 | 72 |
|
73 | 73 | interface ServerEntryModule {
|
74 | 74 | default: HandleDocumentRequestFunction;
|
| 75 | + meta: MetaFunction; |
| 76 | + loader: DataFunction; |
75 | 77 | handleDataRequest?: HandleDataRequestFunction;
|
76 | 78 | }
|
77 | 79 |
|
@@ -237,33 +239,30 @@ function makeWrappedLoader(origAction: DataFunction): DataFunction {
|
237 | 239 | return makeWrappedDataFunction(origAction, 'loader');
|
238 | 240 | }
|
239 | 241 |
|
240 |
| -function makeWrappedMeta(origMeta: MetaFunction | HtmlMetaDescriptor = {}): MetaFunction { |
241 |
| - return function ( |
242 |
| - this: unknown, |
243 |
| - args: { data: AppData; parentsData: RouteData; params: Params; location: Location }, |
244 |
| - ): HtmlMetaDescriptor { |
245 |
| - let origMetaResult; |
246 |
| - if (origMeta instanceof Function) { |
247 |
| - origMetaResult = origMeta.call(this, args); |
248 |
| - } else { |
249 |
| - origMetaResult = origMeta; |
250 |
| - } |
| 242 | +function makeWrappedRootLoader(origLoader: DataFunction): DataFunction { |
| 243 | + return async function (this: unknown, args: DataFunctionArgs): Promise<Response | AppData> { |
| 244 | + let sentryTrace; |
| 245 | + let sentryBaggage; |
| 246 | + |
| 247 | + const activeTransaction = getActiveTransaction(); |
| 248 | + const currentScope = getCurrentHub().getScope(); |
| 249 | + |
| 250 | + if (isNodeEnv() && hasTracingEnabled()) { |
| 251 | + if (currentScope) { |
| 252 | + const span = currentScope.getSpan(); |
251 | 253 |
|
252 |
| - const scope = getCurrentHub().getScope(); |
253 |
| - if (scope) { |
254 |
| - const span = scope.getSpan(); |
255 |
| - const transaction = getActiveTransaction(); |
256 |
| - |
257 |
| - if (span && transaction) { |
258 |
| - return { |
259 |
| - ...origMetaResult, |
260 |
| - 'sentry-trace': span.toTraceparent(), |
261 |
| - baggage: serializeBaggage(transaction.getBaggage()), |
262 |
| - }; |
| 254 | + if (span && activeTransaction) { |
| 255 | + sentryTrace = span.toTraceparent(); |
| 256 | + sentryBaggage = serializeBaggage(activeTransaction.getBaggage()); |
| 257 | + } |
263 | 258 | }
|
264 | 259 | }
|
265 | 260 |
|
266 |
| - return origMetaResult; |
| 261 | + const res = await origLoader.call(this, args); |
| 262 | + |
| 263 | + Object.assign(res, { sentryTrace, sentryBaggage }); |
| 264 | + |
| 265 | + return res; |
267 | 266 | };
|
268 | 267 | }
|
269 | 268 |
|
@@ -378,12 +377,20 @@ function makeWrappedCreateRequestHandler(
|
378 | 377 | for (const [id, route] of Object.entries(build.routes)) {
|
379 | 378 | const wrappedRoute = { ...route, module: { ...route.module } };
|
380 | 379 |
|
381 |
| - fill(wrappedRoute.module, 'meta', makeWrappedMeta); |
382 |
| - |
383 | 380 | if (wrappedRoute.module.action) {
|
384 | 381 | fill(wrappedRoute.module, 'action', makeWrappedAction);
|
385 | 382 | }
|
386 | 383 |
|
| 384 | + // Entry module should have a loader function to provide `sentry-trace` and `baggage` |
| 385 | + // They will be available for the root `meta` function as `data.sentryTrace` and `data.sentryBaggage` |
| 386 | + if (!wrappedRoute.parentId) { |
| 387 | + if (!wrappedRoute.module.loader) { |
| 388 | + wrappedRoute.module.loader = () => ({}); |
| 389 | + } |
| 390 | + |
| 391 | + fill(wrappedRoute.module, 'loader', makeWrappedRootLoader); |
| 392 | + } |
| 393 | + |
387 | 394 | if (wrappedRoute.module.loader) {
|
388 | 395 | fill(wrappedRoute.module, 'loader', makeWrappedLoader);
|
389 | 396 | }
|
|
0 commit comments