Skip to content

Commit 58315e4

Browse files
committed
transaction not undefined
1 parent d2d989c commit 58315e4

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

packages/remix/src/utils/instrumentServer.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable max-lines */
2-
import { captureException, getCurrentHub } from '@sentry/node';
3-
import { getActiveTransaction, hasTracingEnabled, Span } from '@sentry/tracing';
4-
import { WrappedFunction } from '@sentry/types';
2+
import { captureException, getCurrentHub, Hub } from '@sentry/node';
3+
import { getActiveTransaction, hasTracingEnabled } from '@sentry/tracing';
4+
import { Transaction, WrappedFunction } from '@sentry/types';
55
import { addExceptionMechanism, fill, isNodeEnv, loadModule, logger, serializeBaggage } from '@sentry/utils';
66
import * as domain from 'domain';
77

@@ -292,9 +292,9 @@ export function startRequestHandlerTransaction(
292292
url: URL,
293293
method: string,
294294
routes: ServerRoute[],
295+
hub: Hub,
295296
pkg?: ReactRouterDomPkg,
296-
): Span | undefined {
297-
const hub = getCurrentHub();
297+
): Transaction {
298298
const currentScope = hub.getScope();
299299
const matches = matchServerRoutes(routes, url.pathname, pkg);
300300

@@ -312,9 +312,7 @@ export function startRequestHandlerTransaction(
312312
},
313313
});
314314

315-
if (transaction) {
316-
currentScope?.setSpan(transaction);
317-
}
315+
currentScope?.setSpan(transaction);
318316
return transaction;
319317
}
320318

@@ -324,13 +322,20 @@ function wrapRequestHandler(origRequestHandler: RequestHandler, build: ServerBui
324322
return async function (this: unknown, request: Request, loadContext?: unknown): Promise<Response> {
325323
const local = domain.create();
326324
return local.bind(async () => {
325+
const hub = getCurrentHub();
326+
const options = hub.getClient()?.getOptions();
327+
328+
if (!options || !hasTracingEnabled(options)) {
329+
return origRequestHandler.call(this, request, loadContext);
330+
}
331+
327332
const url = new URL(request.url);
328-
const transaction = startRequestHandlerTransaction(url, request.method, routes, pkg);
333+
const transaction = startRequestHandlerTransaction(url, request.method, routes, hub, pkg);
329334

330335
const res = (await origRequestHandler.call(this, request, loadContext)) as Response;
331336

332-
transaction?.setHttpStatus(res.status);
333-
transaction?.finish();
337+
transaction.setHttpStatus(res.status);
338+
transaction.finish();
334339

335340
return res;
336341
})();

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
ReactRouterDomPkg,
1818
ServerBuild,
1919
} from '../types';
20+
import { getCurrentHub } from '@sentry/hub';
21+
import { hasTracingEnabled } from '@sentry/tracing';
2022

2123
function wrapExpressRequestHandler(
2224
origRequestHandler: ExpressRequestHandler,
@@ -42,13 +44,15 @@ function wrapExpressRequestHandler(
4244

4345
local.run(async () => {
4446
const request = extractRequestData(req);
47+
const hub = getCurrentHub();
48+
const options = hub.getClient()?.getOptions();
4549

46-
if (!request.url || !request.method) {
50+
if (!options || !hasTracingEnabled(options) || !request.url || !request.method) {
4751
return origRequestHandler.call(this, req, res, next);
4852
}
4953

5054
const url = new URL(request.url);
51-
const transaction = startRequestHandlerTransaction(url, request.method, routes, pkg);
55+
const transaction = startRequestHandlerTransaction(url, request.method, routes, hub, pkg);
5256

5357
await origRequestHandler.call(this, req, res, next);
5458

0 commit comments

Comments
 (0)