|
1 | 1 | /* eslint-disable max-lines */
|
2 | 2 | import { captureException, getCurrentHub } from '@sentry/node';
|
3 |
| -import { getActiveTransaction } from '@sentry/tracing'; |
| 3 | +import { getActiveTransaction, hasTracingEnabled } from '@sentry/tracing'; |
4 | 4 | import {
|
5 | 5 | addExceptionMechanism,
|
6 | 6 | fill,
|
| 7 | + isNodeEnv, |
7 | 8 | loadModule,
|
8 | 9 | logger,
|
9 | 10 | serializeBaggage,
|
@@ -79,6 +80,8 @@ interface HandleDataRequestFunction {
|
79 | 80 |
|
80 | 81 | interface ServerEntryModule {
|
81 | 82 | default: HandleDocumentRequestFunction;
|
| 83 | + meta: MetaFunction; |
| 84 | + loader: DataFunction; |
82 | 85 | handleDataRequest?: HandleDataRequestFunction;
|
83 | 86 | }
|
84 | 87 |
|
@@ -232,33 +235,30 @@ function makeWrappedLoader(origAction: DataFunction): DataFunction {
|
232 | 235 | return makeWrappedDataFunction(origAction, 'loader');
|
233 | 236 | }
|
234 | 237 |
|
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; |
| 242 | + |
| 243 | + const activeTransaction = getActiveTransaction(); |
| 244 | + const currentScope = getCurrentHub().getScope(); |
| 245 | + |
| 246 | + if (isNodeEnv() && hasTracingEnabled()) { |
| 247 | + if (currentScope) { |
| 248 | + const span = currentScope.getSpan(); |
246 | 249 |
|
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 |
| - }; |
| 250 | + if (span && activeTransaction) { |
| 251 | + sentryTrace = span.toTraceparent(); |
| 252 | + sentryBaggage = serializeBaggage(activeTransaction.getBaggage()); |
| 253 | + } |
258 | 254 | }
|
259 | 255 | }
|
260 | 256 |
|
261 |
| - return origMetaResult; |
| 257 | + const res = await origLoader.call(this, args); |
| 258 | + |
| 259 | + Object.assign(res, { sentryTrace, sentryBaggage }); |
| 260 | + |
| 261 | + return res; |
262 | 262 | };
|
263 | 263 | }
|
264 | 264 |
|
@@ -303,12 +303,20 @@ function makeWrappedCreateRequestHandler(
|
303 | 303 | for (const [id, route] of Object.entries(build.routes)) {
|
304 | 304 | const wrappedRoute = { ...route, module: { ...route.module } };
|
305 | 305 |
|
306 |
| - fill(wrappedRoute.module, 'meta', makeWrappedMeta); |
307 |
| - |
308 | 306 | if (wrappedRoute.module.action) {
|
309 | 307 | fill(wrappedRoute.module, 'action', makeWrappedAction);
|
310 | 308 | }
|
311 | 309 |
|
| 310 | + // Entry module should have a loader function to provide `sentry-trace` and `baggage` |
| 311 | + // They will be available for the root `meta` function as `data.sentryTrace` and `data.sentryBaggage` |
| 312 | + if (!wrappedRoute.parentId) { |
| 313 | + if (!wrappedRoute.module.loader) { |
| 314 | + wrappedRoute.module.loader = () => ({}); |
| 315 | + } |
| 316 | + |
| 317 | + fill(wrappedRoute.module, 'loader', makeWrappedRootLoader); |
| 318 | + } |
| 319 | + |
312 | 320 | if (wrappedRoute.module.loader) {
|
313 | 321 | fill(wrappedRoute.module, 'loader', makeWrappedLoader);
|
314 | 322 | }
|
|
0 commit comments