Skip to content

Commit 053ab31

Browse files
committed
Delay transactions for builds as async functions
1 parent 7cbc54b commit 053ab31

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

packages/remix/src/utils/serverAdapters/express.ts

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getClient, getCurrentHub, hasTracingEnabled, setHttpStatus, withIsolationScope } from '@sentry/core';
22
import { flush } from '@sentry/node-experimental';
3-
import type { Transaction } from '@sentry/types';
3+
import type { Hub, Transaction } from '@sentry/types';
44
import { extractRequestData, fill, isString, logger } from '@sentry/utils';
55

66
import { DEBUG_BUILD } from '../debug-build';
@@ -52,31 +52,59 @@ function wrapExpressRequestHandler(
5252
const resolvedBuild = build();
5353

5454
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+
});
5761
} else {
5862
routes = createRoutes(resolvedBuild.routes);
5963
}
64+
65+
return startRequestHandlerTransactionWithRoutes.call(
66+
this,
67+
origRequestHandler,
68+
routes,
69+
req,
70+
res,
71+
next,
72+
hub,
73+
url,
74+
);
6075
} else {
6176
routes = createRoutes(build.routes);
6277
}
6378

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);
7680
});
7781
};
7882
}
7983

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+
80108
function wrapGetLoadContext(origGetLoadContext: () => AppLoadContext): GetLoadContextFunction {
81109
return function (this: unknown, req: ExpressRequest, res: ExpressResponse): AppLoadContext {
82110
const loadContext = (origGetLoadContext.call(this, req, res) || {}) as AppLoadContext;

0 commit comments

Comments
 (0)