-
Notifications
You must be signed in to change notification settings - Fork 615
feat(instrumentation-express): Add support for Express v5 #2437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
7bf9b3a
35fd061
e0bee03
f9c4b72
b84ad64
39c4dcc
28a9fd0
4488637
c209205
3aa1c9e
3524745
94f564c
9587d3e
4a71e9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,9 +59,12 @@ export class ExpressInstrumentation extends InstrumentationBase<ExpressInstrumen | |
| return [ | ||
| new InstrumentationNodeModuleDefinition( | ||
| 'express', | ||
| ['>=4.0.0 <5'], | ||
| ['>=4.0.0 <6'], | ||
| moduleExports => { | ||
| const routerProto = moduleExports.Router as unknown as express.Router; | ||
| const isExpressV5 = 'Route' in moduleExports.Router; | ||
|
||
| const routerProto = isExpressV5 | ||
| ? moduleExports.Router.prototype | ||
| : moduleExports.Router; | ||
| // patch express.Router.route | ||
| if (isWrapped(routerProto.route)) { | ||
| this._unwrap(routerProto, 'route'); | ||
|
|
@@ -81,13 +84,16 @@ export class ExpressInstrumentation extends InstrumentationBase<ExpressInstrumen | |
| moduleExports.application, | ||
| 'use', | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| this._getAppUsePatch() as any | ||
| this._getAppUsePatch(isExpressV5) as any | ||
| ); | ||
| return moduleExports; | ||
| }, | ||
| moduleExports => { | ||
| if (moduleExports === undefined) return; | ||
| const routerProto = moduleExports.Router as unknown as express.Router; | ||
| const isExpressV5 = 'Route' in moduleExports.Router; | ||
| const routerProto = isExpressV5 | ||
| ? moduleExports.Router.prototype | ||
| : moduleExports.Router; | ||
| this._unwrap(routerProto, 'route'); | ||
| this._unwrap(routerProto, 'use'); | ||
| this._unwrap(moduleExports.application, 'use'); | ||
|
|
@@ -135,16 +141,22 @@ export class ExpressInstrumentation extends InstrumentationBase<ExpressInstrumen | |
| /** | ||
| * Get the patch for Application.use function | ||
| */ | ||
| private _getAppUsePatch() { | ||
| private _getAppUsePatch(isExpressV5: boolean) { | ||
| const instrumentation = this; | ||
| return function (original: express.Application['use']) { | ||
| return function use( | ||
| this: { _router: ExpressRouter }, | ||
| // In express 5.x the router is stored in `router` whereas in 4.x it's stored in `_router` | ||
| this: { _router?: ExpressRouter; router?: ExpressRouter }, | ||
| ...args: Parameters<typeof original> | ||
| ) { | ||
| // if we access app.router in express 4.x we trigger an assertion error | ||
| // This property existed in v3, was removed in v4 and then re-added in v5 | ||
| const router = isExpressV5 ? this.router : this._router; | ||
| const route = original.apply(this, args); | ||
| const layer = this._router.stack[this._router.stack.length - 1]; | ||
| instrumentation._applyPatch(layer, getLayerPath(args)); | ||
| if (router) { | ||
| const layer = router.stack[router.stack.length - 1]; | ||
| instrumentation._applyPatch(layer, getLayerPath(args)); | ||
| } | ||
| return route; | ||
| }; | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"@types/express": "4.17.21"
There is now a v5 for the typing too
https://www.npmjs.com/package/@types/express