|
1 | 1 | import { getClient, getCurrentHub, hasTracingEnabled, setHttpStatus, withIsolationScope } from '@sentry/core';
|
2 | 2 | import { flush } from '@sentry/node-experimental';
|
3 |
| -import type { Transaction } from '@sentry/types'; |
| 3 | +import type { Hub, Transaction } from '@sentry/types'; |
4 | 4 | import { extractRequestData, fill, isString, logger } from '@sentry/utils';
|
5 | 5 |
|
6 | 6 | import { DEBUG_BUILD } from '../debug-build';
|
@@ -52,31 +52,59 @@ function wrapExpressRequestHandler(
|
52 | 52 | const resolvedBuild = build();
|
53 | 53 |
|
54 | 54 | if (resolvedBuild instanceof Promise) {
|
55 |
| - const resolved = await resolvedBuild; |
56 |
| - routes = createRoutes(resolved.routes); |
| 55 | + // eslint-disable-next-line @typescript-eslint/no-floating-promises |
| 56 | + resolvedBuild.then(resolved => { |
| 57 | + routes = createRoutes(resolved.routes); |
| 58 | + |
| 59 | + startRequestHandlerTransactionWithRoutes.call(this, origRequestHandler, routes, req, res, next, hub, url); |
| 60 | + }); |
57 | 61 | } else {
|
58 | 62 | routes = createRoutes(resolvedBuild.routes);
|
59 | 63 | }
|
| 64 | + |
| 65 | + return startRequestHandlerTransactionWithRoutes.call( |
| 66 | + this, |
| 67 | + origRequestHandler, |
| 68 | + routes, |
| 69 | + req, |
| 70 | + res, |
| 71 | + next, |
| 72 | + hub, |
| 73 | + url, |
| 74 | + ); |
60 | 75 | } else {
|
61 | 76 | routes = createRoutes(build.routes);
|
62 | 77 | }
|
63 | 78 |
|
64 |
| - const [name, source] = getTransactionName(routes, url); |
65 |
| - const transaction = startRequestHandlerTransaction(hub, name, source, { |
66 |
| - headers: { |
67 |
| - 'sentry-trace': (req.headers && isString(req.headers['sentry-trace']) && req.headers['sentry-trace']) || '', |
68 |
| - baggage: (req.headers && isString(req.headers.baggage) && req.headers.baggage) || '', |
69 |
| - }, |
70 |
| - method: request.method, |
71 |
| - }); |
72 |
| - // save a link to the transaction on the response, so that even if there's an error (landing us outside of |
73 |
| - // the domain), we can still finish it (albeit possibly missing some scope data) |
74 |
| - (res as AugmentedExpressResponse).__sentryTransaction = transaction; |
75 |
| - return origRequestHandler.call(this, req, res, next); |
| 79 | + startRequestHandlerTransactionWithRoutes.call(this, origRequestHandler, routes, req, res, next, hub, url); |
76 | 80 | });
|
77 | 81 | };
|
78 | 82 | }
|
79 | 83 |
|
| 84 | +function startRequestHandlerTransactionWithRoutes( |
| 85 | + this: unknown, |
| 86 | + origRequestHandler: ExpressRequestHandler, |
| 87 | + routes: ServerRoute[], |
| 88 | + req: ExpressRequest, |
| 89 | + res: ExpressResponse, |
| 90 | + next: ExpressNextFunction, |
| 91 | + hub: Hub, |
| 92 | + url: URL, |
| 93 | +): Transaction | undefined { |
| 94 | + const [name, source] = getTransactionName(routes, url); |
| 95 | + const transaction = startRequestHandlerTransaction(hub, name, source, { |
| 96 | + headers: { |
| 97 | + 'sentry-trace': (req.headers && isString(req.headers['sentry-trace']) && req.headers['sentry-trace']) || '', |
| 98 | + baggage: (req.headers && isString(req.headers.baggage) && req.headers.baggage) || '', |
| 99 | + }, |
| 100 | + method: req.method, |
| 101 | + }); |
| 102 | + // save a link to the transaction on the response, so that even if there's an error (landing us outside of |
| 103 | + // the domain), we can still finish it (albeit possibly missing some scope data) |
| 104 | + (res as AugmentedExpressResponse).__sentryTransaction = transaction; |
| 105 | + return origRequestHandler.call(this, req, res, next); |
| 106 | +} |
| 107 | + |
80 | 108 | function wrapGetLoadContext(origGetLoadContext: () => AppLoadContext): GetLoadContextFunction {
|
81 | 109 | return function (this: unknown, req: ExpressRequest, res: ExpressResponse): AppLoadContext {
|
82 | 110 | const loadContext = (origGetLoadContext.call(this, req, res) || {}) as AppLoadContext;
|
|
0 commit comments