|
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; |
246 | 242 |
|
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 | + } |
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 |
|
@@ -296,19 +296,33 @@ function makeWrappedCreateRequestHandler(
|
296 | 296 | return function (this: unknown, build: ServerBuild, mode: string | undefined): RequestHandler {
|
297 | 297 | const routes: ServerRouteManifest = {};
|
298 | 298 |
|
| 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 | + |
299 | 307 | const wrappedEntry = { ...build.entry, module: { ...build.entry.module } };
|
300 | 308 |
|
301 | 309 | fill(wrappedEntry.module, 'default', makeWrappedDocumentRequestFunction);
|
302 | 310 |
|
303 | 311 | for (const [id, route] of Object.entries(build.routes)) {
|
304 | 312 | const wrappedRoute = { ...route, module: { ...route.module } };
|
305 | 313 |
|
306 |
| - fill(wrappedRoute.module, 'meta', makeWrappedMeta); |
307 |
| - |
308 | 314 | if (wrappedRoute.module.action) {
|
309 | 315 | fill(wrappedRoute.module, 'action', makeWrappedAction);
|
310 | 316 | }
|
311 | 317 |
|
| 318 | + if (!wrappedRoute.parentId) { |
| 319 | + if (!wrappedRoute.module.loader) { |
| 320 | + wrappedRoute.module.loader = () => ({}); |
| 321 | + } |
| 322 | + |
| 323 | + fill(wrappedRoute.module, 'loader', makeWrappedRootLoader); |
| 324 | + } |
| 325 | + |
312 | 326 | if (wrappedRoute.module.loader) {
|
313 | 327 | fill(wrappedRoute.module, 'loader', makeWrappedLoader);
|
314 | 328 | }
|
|
0 commit comments