Skip to content

Commit fff626d

Browse files
committed
maybe fix instr
1 parent d76f57e commit fff626d

File tree

1 file changed

+49
-47
lines changed

1 file changed

+49
-47
lines changed

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

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class ReactRouterInstrumentation extends InstrumentationBase<Instrumentat
4040
if (isWrapped(moduleExports['createRequestHandler'])) {
4141
this._unwrap(moduleExports, 'createRequestHandler');
4242
}
43-
this._wrap(moduleExports, 'createRequestHandler', this._patchCreateRequestHandler());
43+
this._wrap(moduleExports, 'createRequestHandler', _patchCreateRequestHandler);
4444
return moduleExports;
4545
},
4646
(moduleExports: ReactRouterModuleExports) => {
@@ -50,58 +50,60 @@ export class ReactRouterInstrumentation extends InstrumentationBase<Instrumentat
5050

5151
return reactRouterServerModule;
5252
}
53+
}
5354

54-
/**
55-
* Returns a patched version of the createRequestHandler function that adds Sentry performance monitoring.
56-
* This wraps the request handler to create spans for data loader and action requests.
57-
*/
58-
private _patchCreateRequestHandler(): (original: typeof reactRouter.createRequestHandler) => any {
59-
return function sentryWrappedCreateRequestHandler(this: unknown, ...args: unknown[]) {
60-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
61-
// @ts-ignore not sure why original isn't found here?
62-
const originalRequestHandler = (original as typeof reactRouter.createRequestHandler).apply(this, args);
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-
}
55+
/**
56+
* Returns a patched version of the createRequestHandler function that adds Sentry performance monitoring.
57+
* This wraps the request handler to create spans for data loader and action requests.
58+
*/
59+
function _patchCreateRequestHandler(
60+
original: typeof reactRouter.createRequestHandler,
61+
): typeof reactRouter.createRequestHandler {
62+
return function sentryWrappedCreateRequestHandler(this: unknown, ...args: unknown[]) {
63+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
64+
// @ts-ignore not sure why original isn't found here?
65+
const originalRequestHandler = (original as typeof reactRouter.createRequestHandler).apply(this, args);
66+
return async function sentryWrappedRequestHandler(request: Request, initialContext?: unknown) {
67+
let url: URL;
68+
try {
69+
url = new URL(request.url);
70+
} catch (error) {
71+
return originalRequestHandler(request, initialContext);
72+
}
7073

71-
// We currently just want to trace loaders and actions
72-
if (!isDataRequest(url.pathname)) {
73-
return originalRequestHandler(request, initialContext);
74-
}
74+
// We currently just want to trace loaders and actions
75+
if (!isDataRequest(url.pathname)) {
76+
return originalRequestHandler(request, initialContext);
77+
}
7578

76-
const activeSpan = getActiveSpan();
77-
const rootSpan = activeSpan && getRootSpan(activeSpan);
79+
const activeSpan = getActiveSpan();
80+
const rootSpan = activeSpan && getRootSpan(activeSpan);
7881

79-
if (!rootSpan) {
80-
DEBUG_BUILD && logger.debug('No active root span found, skipping tracing for data request');
81-
return originalRequestHandler(request, initialContext);
82-
}
82+
if (!rootSpan) {
83+
DEBUG_BUILD && logger.debug('No active root span found, skipping tracing for data request');
84+
return originalRequestHandler(request, initialContext);
85+
}
8386

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-
});
87+
// Set the source and overwrite attributes on the root span to ensure the transaction name
88+
// is derived from the raw URL pathname rather than any parameterized route that may be set later
89+
// TODO: try to set derived parameterized route from build here (args[0])
90+
rootSpan.setAttributes({
91+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
92+
[SEMANTIC_ATTRIBUTE_SENTRY_OVERWRITE]: `${request.method} ${url.pathname}`,
93+
});
9194

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-
},
95+
return startSpan(
96+
{
97+
name: getSpanName(url.pathname, request.method),
98+
attributes: {
99+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.react-router',
100+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: getOpName(url.pathname, request.method),
99101
},
100-
() => {
101-
return originalRequestHandler(request, initialContext);
102-
},
103-
);
104-
};
102+
},
103+
() => {
104+
return originalRequestHandler(request, initialContext);
105+
},
106+
);
105107
};
106-
}
108+
};
107109
}

0 commit comments

Comments
 (0)