Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions packages/core/src/integrations/express/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ const getExpressExport = (express: ExpressModuleExport): ExpressExport =>
* import express from 'express';
* import * as Sentry from '@sentry/deno'; // or any SDK that extends core
*
* Sentry.patchExpressModule({ express })
* Sentry.patchExpressModule(express, () => ({}));
* ```
*/
export const patchExpressModule = (options: ExpressIntegrationOptions) => {
export const patchExpressModule = (moduleExports: ExpressModuleExport, getOptions: () => ExpressIntegrationOptions) => {
// pass in the require() or import() result of express
const express = getExpressExport(options.express);
const express = getExpressExport(moduleExports);
const routerProto: ExpressRouterv4 | ExpressRouterv5 | undefined = isExpressWithRouterPrototype(express)
? express.Router.prototype // Express v5
: isExpressWithoutRouterPrototype(express)
Expand All @@ -93,7 +94,7 @@ export const patchExpressModule = (options: ExpressIntegrationOptions) => {
function routeTrace(this: ExpressRouter, ...args: Parameters<typeof originalRouteMethod>[]) {
const route = originalRouteMethod.apply(this, args);
const layer = this.stack[this.stack.length - 1] as ExpressLayer;
patchLayer(options, layer, getLayerPath(args));
patchLayer(getOptions, layer, getLayerPath(args));
return route;
},
);
Expand All @@ -113,7 +114,7 @@ export const patchExpressModule = (options: ExpressIntegrationOptions) => {
if (!layer) {
return route;
}
patchLayer(options, layer, getLayerPath(args));
patchLayer(getOptions, layer, getLayerPath(args));
return route;
},
);
Expand Down Expand Up @@ -141,7 +142,7 @@ export const patchExpressModule = (options: ExpressIntegrationOptions) => {
if (router) {
const layer = router.stack[router.stack.length - 1];
if (layer) {
patchLayer(options, layer, getLayerPath(args));
patchLayer(getOptions, layer, getLayerPath(args));
}
}
return route;
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/integrations/express/patch-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ export type ExpressPatchLayerOptions = Pick<
'onRouteResolved' | 'ignoreLayers' | 'ignoreLayersType'
>;

export function patchLayer(options: ExpressPatchLayerOptions, maybeLayer?: ExpressLayer, layerPath?: string): void {
export function patchLayer(
getOptions: () => ExpressPatchLayerOptions,
maybeLayer?: ExpressLayer,
layerPath?: string,
): void {
if (!maybeLayer?.handle) {
return;
}
Expand All @@ -86,6 +90,8 @@ export function patchLayer(options: ExpressPatchLayerOptions, maybeLayer?: Expre
//oxlint-disable-next-line no-explicit-any
...otherArgs: any[]
) {
const options = getOptions();

// Set normalizedRequest here because expressRequestHandler middleware
// (registered via setupExpressErrorHandler) is added after routes and
// therefore never runs for successful requests — route handlers typically
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/integrations/express/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ export type ExpressRouter = {
export type IgnoreMatcher = string | RegExp | ((name: string) => boolean);

export type ExpressIntegrationOptions = {
express: ExpressModuleExport; //Express
/** Ignore specific based on their name */
ignoreLayers?: IgnoreMatcher[];
/** Ignore specific layers based on their type */
Expand Down
57 changes: 28 additions & 29 deletions packages/core/test/lib/integrations/express/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ vi.mock('../../../../src/utils/debug-logger', () => ({
}));

beforeEach(() => (patchLayerCalls.length = 0));
const patchLayerCalls: [options: ExpressIntegrationOptions, layer: ExpressLayer, layerPath?: string][] = [];
const patchLayerCalls: [getOptions: () => ExpressIntegrationOptions, layer: ExpressLayer, layerPath?: string][] = [];

vi.mock('../../../../src/integrations/express/patch-layer', () => ({
patchLayer: (options: ExpressIntegrationOptions, layer?: ExpressLayer, layerPath?: string) => {
patchLayer: (getOptions: () => ExpressIntegrationOptions, layer?: ExpressLayer, layerPath?: string) => {
if (layer) {
patchLayerCalls.push([options, layer, layerPath]);
patchLayerCalls.push([getOptions, layer, layerPath]);
}
},
}));
Expand Down Expand Up @@ -131,24 +131,22 @@ function getExpress5(): ExpressExportv5 & { spies: ExpressSpies } {
describe('patchExpressModule', () => {
it('throws trying to patch/unpatch the wrong thing', () => {
expect(() => {
patchExpressModule({
express: {} as unknown as ExpressModuleExport,
} as unknown as ExpressIntegrationOptions);
patchExpressModule({} as unknown as ExpressModuleExport, () => ({}));
}).toThrowError('no valid Express route function to instrument');
});

it('can patch and restore expressv4 style module', () => {
for (const useDefault of [false, true]) {
const express = getExpress4();
const module = useDefault ? { default: express } : express;
const moduleExports = useDefault ? { default: express } : express;
const r = express.Router as ExpressRouterv4;
const a = express.application;
const options = { express: module } as unknown as ExpressIntegrationOptions;
const getOptions = () => ({});
expect((r.use as WrappedFunction).__sentry_original__).toBe(undefined);
expect((r.route as WrappedFunction).__sentry_original__).toBe(undefined);
expect((a.use as WrappedFunction).__sentry_original__).toBe(undefined);

patchExpressModule(options);
patchExpressModule(moduleExports, getOptions);

expect(typeof (r.use as WrappedFunction).__sentry_original__).toBe('function');
expect(typeof (r.route as WrappedFunction).__sentry_original__).toBe('function');
Expand All @@ -161,13 +159,13 @@ describe('patchExpressModule', () => {
const express = getExpress5();
const r = express.Router as ExpressRouterv5;
const a = express.application;
const module = useDefault ? { default: express } : express;
const options = { express: module } as unknown as ExpressIntegrationOptions;
const moduleExports = useDefault ? { default: express } : express;
const getOptions = () => ({});
expect((r.prototype.use as WrappedFunction).__sentry_original__).toBe(undefined);
expect((r.prototype.route as WrappedFunction).__sentry_original__).toBe(undefined);
expect((a.use as WrappedFunction).__sentry_original__).toBe(undefined);

patchExpressModule(options);
patchExpressModule(moduleExports, getOptions);

expect(typeof (r.prototype.use as WrappedFunction).__sentry_original__).toBe('function');
expect(typeof (r.prototype.route as WrappedFunction).__sentry_original__).toBe('function');
Expand All @@ -178,27 +176,27 @@ describe('patchExpressModule', () => {
it('calls patched and original Router.route', () => {
const expressv4 = getExpress4();
const { spies } = expressv4;
const options = { express: expressv4 };
patchExpressModule(options);
const getOptions = () => ({});
patchExpressModule(expressv4, getOptions);
expressv4.Router.route('a');
expect(spies.routerRoute).toHaveBeenCalledExactlyOnceWith('a');
});

it('calls patched and original Router.use', () => {
const expressv4 = getExpress4();
const { spies } = expressv4;
const options = { express: expressv4 };
patchExpressModule(options);
const getOptions = () => ({});
patchExpressModule(expressv4, getOptions);
expressv4.Router.use('a');
expect(patchLayerCalls).toStrictEqual([[options, { name: 'layerFinal' }, 'a']]);
expect(patchLayerCalls).toStrictEqual([[getOptions, { name: 'layerFinal' }, 'a']]);
expect(spies.routerUse).toHaveBeenCalledExactlyOnceWith('a');
});

it('skips patchLayer call in Router.use if no layer in the stack', () => {
const expressv4 = getExpress4();
const { spies } = expressv4;
const options = { express: expressv4 };
patchExpressModule(options);
const getOptions = () => ({});
patchExpressModule(expressv4, getOptions);
const { stack } = expressv4.Router;
expressv4.Router.stack = [];
expressv4.Router.use('a');
Expand All @@ -210,28 +208,28 @@ describe('patchExpressModule', () => {
it('calls patched and original application.use', () => {
const expressv4 = getExpress4();
const { spies } = expressv4;
const options = { express: expressv4 };
patchExpressModule(options);
const getOptions = () => ({});
patchExpressModule(expressv4, getOptions);
expressv4.application.use('a');
expect(patchLayerCalls).toStrictEqual([[options, { name: 'layerFinal' }, 'a']]);
expect(patchLayerCalls).toStrictEqual([[getOptions, { name: 'layerFinal' }, 'a']]);
expect(spies.appUse).toHaveBeenCalledExactlyOnceWith('a');
});

it('calls patched and original application.use on express v5', () => {
const expressv5 = getExpress5();
const { spies } = expressv5;
const options = { express: expressv5 };
patchExpressModule(options);
const getOptions = () => ({});
patchExpressModule(expressv5, getOptions);
expressv5.application.use('a');
expect(patchLayerCalls).toStrictEqual([[options, { name: 'layerFinal' }, 'a']]);
expect(patchLayerCalls).toStrictEqual([[getOptions, { name: 'layerFinal' }, 'a']]);
expect(spies.appUse).toHaveBeenCalledExactlyOnceWith('a');
});

it('skips patchLayer on application.use if no router found', () => {
const expressv4 = getExpress4();
const { spies } = expressv4;
const options = { express: expressv4 };
patchExpressModule(options);
const getOptions = () => ({});
patchExpressModule(expressv4, getOptions);
const app = expressv4.application as {
_router?: ExpressRoute;
};
Expand All @@ -246,8 +244,9 @@ describe('patchExpressModule', () => {

it('debug error when patching fails', () => {
const expressv5 = getExpress5();
patchExpressModule({ express: expressv5 });
patchExpressModule({ express: expressv5 });
const getOptions = () => ({});
patchExpressModule(expressv5, getOptions);
patchExpressModule(expressv5, getOptions);
expect(debugErrors).toStrictEqual([
['Failed to patch express route method:', new Error('Attempting to wrap method route multiple times')],
['Failed to patch express use method:', new Error('Attempting to wrap method use multiple times')],
Expand Down
30 changes: 15 additions & 15 deletions packages/core/test/lib/integrations/express/patch-layer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ describe('patchLayer', () => {
describe('no-ops', () => {
it('if layer is missing', () => {
// mostly for coverage, verifying it doesn't throw or anything
patchLayer({});
patchLayer(() => ({}));
});

it('if layer.handle is missing', () => {
// mostly for coverage, verifying it doesn't throw or anything
patchLayer({}, { handle: null } as unknown as ExpressLayer);
patchLayer(() => ({}), { handle: null } as unknown as ExpressLayer);
});

it('if layer already patched', () => {
Expand All @@ -166,7 +166,7 @@ describe('patchLayer', () => {
const layer = {
handle: wrapped,
} as unknown as ExpressLayer;
patchLayer({}, layer);
patchLayer(() => ({}), layer);
expect(layer.handle).toBe(wrapped);
});

Expand All @@ -177,7 +177,7 @@ describe('patchLayer', () => {
const layer = {
handle: original,
} as unknown as ExpressLayer;
patchLayer({}, layer);
patchLayer(() => ({}), layer);
expect(layer.handle).toBe(original);
});

Expand All @@ -188,7 +188,7 @@ describe('patchLayer', () => {
const layer = {
handle: original,
} as unknown as ExpressLayer;
patchLayer({}, layer);
patchLayer(() => ({}), layer);
expect(getOriginalFunction(layer.handle)).toBe(original);
});
});
Expand All @@ -212,7 +212,7 @@ describe('patchLayer', () => {
storeLayer(req, '/:boo');
storeLayer(req, '/:car');

patchLayer(options, layer);
patchLayer(() => options, layer);
layer.handle(req, res);
expect(layerHandleOriginal).toHaveBeenCalledOnce();

Expand Down Expand Up @@ -244,7 +244,7 @@ describe('patchLayer', () => {
storeLayer(req, '/:boo');
storeLayer(req, '/:car');

patchLayer(options, layer, '/layerPath');
patchLayer(() => options, layer, '/layerPath');
layer.handle(req, res);
expect(onRouteResolved).toHaveBeenCalledExactlyOnceWith('/a/:boo/:car/layerPath');
expect(layerHandleOriginal).toHaveBeenCalledOnce();
Expand Down Expand Up @@ -290,7 +290,7 @@ describe('patchLayer', () => {
// 'router' → router, 'bound dispatch' → request_handler, other → middleware
const layerName = type === 'router' ? 'router' : 'bound dispatch';
const layer = { name: layerName, handle: layerHandleOriginal } as unknown as ExpressLayer;
patchLayer(options, layer, '/c');
patchLayer(() => options, layer, '/c');

// storeLayer('/c') happens inside the patched handle, before being popped
// after handle returns, storedLayers should be back to ['/a', '/b']
Expand Down Expand Up @@ -327,7 +327,7 @@ describe('patchLayer', () => {
storeLayer(req, '/:boo');
storeLayer(req, '/:car');

patchLayer(options, layer, '/layerPath');
patchLayer(() => options, layer, '/layerPath');
expect(getOriginalFunction(layer.handle)).toBe(layerHandleOriginal);
expect(layer.handle.x).toBe(true);
layer.handle.x = false;
Expand Down Expand Up @@ -382,7 +382,7 @@ describe('patchLayer', () => {
storeLayer(req, '/:boo');
storeLayer(req, '/:car');

patchLayer(options, layer);
patchLayer(() => options, layer);
expect(getOriginalFunction(layer.handle)).toBe(layerHandleOriginal);
warnings.length = 0;
layer.handle(req, res);
Expand Down Expand Up @@ -441,7 +441,7 @@ describe('patchLayer', () => {
storeLayer(req, '/a');
storeLayer(req, '/b');

patchLayer(options, layer, '/c');
patchLayer(() => options, layer, '/c');
layer.handle(req, res);
expect(onRouteResolved).toHaveBeenCalledExactlyOnceWith('/a/b/c');
const span = mockSpans[0];
Expand Down Expand Up @@ -482,7 +482,7 @@ describe('patchLayer', () => {
storeLayer(req, '/a');
storeLayer(req, '/b');

patchLayer(options, layer, '/c');
patchLayer(() => options, layer, '/c');
layer.handle(req, res);
expect(onRouteResolved).toHaveBeenCalledExactlyOnceWith(undefined);
const span = mockSpans[0];
Expand Down Expand Up @@ -526,7 +526,7 @@ describe('patchLayer', () => {

storeLayer(req, '/a');
storeLayer(req, '/b');
patchLayer(options, layer, '/c');
patchLayer(() => options, layer, '/c');

expect(getStoredLayers(req)).toStrictEqual(['/a', '/b']);
const callback = vi.fn(() => {
Expand Down Expand Up @@ -576,7 +576,7 @@ describe('patchLayer', () => {

storeLayer(req, '/a');
storeLayer(req, '/b');
patchLayer(options, layer, '/c');
patchLayer(() => options, layer, '/c');

expect(getStoredLayers(req)).toStrictEqual(['/a', '/b']);
const callback = vi.fn(() => {
Expand Down Expand Up @@ -622,7 +622,7 @@ describe('patchLayer', () => {
storeLayer(req, '/a');
storeLayer(req, '/b');

patchLayer(options, layer, '/c');
patchLayer(() => options, layer, '/c');
expect(() => {
layer.handle(req, res);
}).toThrowError('yur head asplode');
Expand Down
3 changes: 2 additions & 1 deletion packages/node-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@
"dependencies": {
"@sentry/core": "10.48.0",
"@sentry/opentelemetry": "10.48.0",
"import-in-the-middle": "^3.0.0"
"import-in-the-middle": "^3.0.0",
"require-in-the-middle": "^7.5.0"
},
"devDependencies": {
"@opentelemetry/api": "^1.9.1",
Expand Down
2 changes: 2 additions & 0 deletions packages/node-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export { processSessionIntegration } from './integrations/processSession';

export type { OpenTelemetryServerRuntimeOptions } from './types';

export { registerModuleWrapper } from './module-wrapper';

export {
// This needs exporting so the NodeClient can be used without calling init
setOpenTelemetryContextAsyncContextStrategy as setNodeAsyncContextStrategy,
Expand Down
Loading
Loading