Skip to content

Commit dc40e7e

Browse files
committed
fix(react-router): Don't patch when loaded via require(esm)
1 parent 3604a08 commit dc40e7e

File tree

1 file changed

+48
-53
lines changed

1 file changed

+48
-53
lines changed

packages/react-router/src/server/instrumentation/reactRouter.ts

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ export class ReactRouterInstrumentation extends InstrumentationBase<Instrumentat
3737
COMPONENT,
3838
supportedVersions,
3939
(moduleExports: ReactRouterModuleExports) => {
40-
return this._createPatchedModuleProxy(moduleExports);
41-
},
42-
(_moduleExports: unknown) => {
43-
// nothing to unwrap here
44-
return _moduleExports;
40+
try {
41+
return this._createPatchedModuleProxy(moduleExports);
42+
} catch (_) {
43+
return moduleExports;
44+
}
4545
},
4646
);
4747

@@ -53,59 +53,54 @@ export class ReactRouterInstrumentation extends InstrumentationBase<Instrumentat
5353
* This allows us to wrap the request handler to add performance monitoring for data loaders and actions.
5454
*/
5555
private _createPatchedModuleProxy(moduleExports: ReactRouterModuleExports): ReactRouterModuleExports {
56-
return new Proxy(moduleExports, {
57-
get(target, prop, receiver) {
58-
if (prop === 'createRequestHandler') {
59-
const original = target[prop];
60-
return function sentryWrappedCreateRequestHandler(this: unknown, ...args: unknown[]) {
61-
const originalRequestHandler = original.apply(this, args);
56+
const original = moduleExports.createRequestHandler;
57+
moduleExports.createRequestHandler = function (this: unknown, ...args: unknown[]) {
58+
const originalRequestHandler = original.apply(this, args);
59+
60+
return async function sentryWrappedRequestHandler(request: Request, initialContext?: unknown) {
61+
let url: URL;
62+
try {
63+
url = new URL(request.url);
64+
} catch (error) {
65+
return originalRequestHandler(request, initialContext);
66+
}
6267

63-
return async function sentryWrappedRequestHandler(request: Request, initialContext?: unknown) {
64-
let url: URL;
65-
try {
66-
url = new URL(request.url);
67-
} catch (error) {
68-
return originalRequestHandler(request, initialContext);
69-
}
68+
// We currently just want to trace loaders and actions
69+
if (!isDataRequest(url.pathname)) {
70+
return originalRequestHandler(request, initialContext);
71+
}
7072

71-
// We currently just want to trace loaders and actions
72-
if (!isDataRequest(url.pathname)) {
73-
return originalRequestHandler(request, initialContext);
74-
}
73+
const activeSpan = getActiveSpan();
74+
const rootSpan = activeSpan && getRootSpan(activeSpan);
7575

76-
const activeSpan = getActiveSpan();
77-
const rootSpan = activeSpan && getRootSpan(activeSpan);
76+
if (!rootSpan) {
77+
DEBUG_BUILD && logger.debug('No active root span found, skipping tracing for data request');
78+
return originalRequestHandler(request, initialContext);
79+
}
7880

79-
if (!rootSpan) {
80-
DEBUG_BUILD && logger.debug('No active root span found, skipping tracing for data request');
81-
return originalRequestHandler(request, initialContext);
82-
}
81+
// Set the source and overwrite attributes on the root span to ensure the transaction name
82+
// is derived from the raw URL pathname rather than any parameterized route that may be set later
83+
// TODO: try to set derived parameterized route from build here (args[0])
84+
rootSpan.setAttributes({
85+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
86+
[SEMANTIC_ATTRIBUTE_SENTRY_OVERWRITE]: `${request.method} ${url.pathname}`,
87+
});
8388

84-
// Set the source and overwrite attributes on the root span to ensure the transaction name
85-
// is derived from the raw URL pathname rather than any parameterized route that may be set later
86-
// TODO: try to set derived parameterized route from build here (args[0])
87-
rootSpan.setAttributes({
88-
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
89-
[SEMANTIC_ATTRIBUTE_SENTRY_OVERWRITE]: `${request.method} ${url.pathname}`,
90-
});
89+
return startSpan(
90+
{
91+
name: getSpanName(url.pathname, request.method),
92+
attributes: {
93+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.react-router',
94+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: getOpName(url.pathname, request.method),
95+
},
96+
},
97+
() => {
98+
return originalRequestHandler(request, initialContext);
99+
},
100+
);
101+
};
102+
};
91103

92-
return startSpan(
93-
{
94-
name: getSpanName(url.pathname, request.method),
95-
attributes: {
96-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.react-router',
97-
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: getOpName(url.pathname, request.method),
98-
},
99-
},
100-
() => {
101-
return originalRequestHandler(request, initialContext);
102-
},
103-
);
104-
};
105-
};
106-
}
107-
return Reflect.get(target, prop, receiver);
108-
},
109-
});
104+
return moduleExports;
110105
}
111106
}

0 commit comments

Comments
 (0)