Skip to content

Commit 06a4c2f

Browse files
authored
feat: Added instrumentation support for Express 5 beta (#2476)
This will be experimental until [email protected] is generally available
1 parent c2b8879 commit 06a4c2f

18 files changed

+1425
-1520
lines changed

lib/instrumentation/express.js

Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
'use strict'
77

88
const { MiddlewareSpec, MiddlewareMounterSpec, RenderSpec } = require('../../lib/shim/specs')
9-
const { MIDDLEWARE_TYPE_NAMES } = require('../../lib/shim/webframework-shim/common')
109

1110
/**
1211
* Express middleware generates traces where middleware are considered siblings
@@ -25,35 +24,30 @@ module.exports = function initialize(agent, express, moduleName, shim) {
2524
return err !== 'route' && err !== 'router'
2625
})
2726

28-
if (express.Router.use) {
29-
wrapExpress4(shim, express)
30-
} else {
31-
wrapExpress3(shim, express)
32-
}
33-
}
34-
35-
function wrapExpress4(shim, express) {
36-
// Wrap `use` and `route` which are hung off `Router` directly, not on a
37-
// prototype.
3827
shim.wrapMiddlewareMounter(
39-
express.Router,
28+
express.application,
4029
'use',
4130
new MiddlewareMounterSpec({
4231
route: shim.FIRST,
4332
wrapper: wrapMiddleware
4433
})
4534
)
4635

36+
wrapExpressRouter(shim, express.Router.use ? express.Router : express.Router.prototype)
37+
wrapResponse(shim, express.response)
38+
}
39+
40+
function wrapExpressRouter(shim, router) {
4741
shim.wrapMiddlewareMounter(
48-
express.application,
42+
router,
4943
'use',
5044
new MiddlewareMounterSpec({
5145
route: shim.FIRST,
5246
wrapper: wrapMiddleware
5347
})
5448
)
5549

56-
shim.wrap(express.Router, 'route', function wrapRoute(shim, fn) {
50+
shim.wrap(router, 'route', function wrapRoute(shim, fn) {
5751
if (!shim.isFunction(fn)) {
5852
return fn
5953
}
@@ -89,7 +83,7 @@ function wrapExpress4(shim, express) {
8983
})
9084

9185
shim.wrapMiddlewareMounter(
92-
express.Router,
86+
router,
9387
'param',
9488
new MiddlewareMounterSpec({
9589
route: shim.FIRST,
@@ -105,56 +99,6 @@ function wrapExpress4(shim, express) {
10599
}
106100
})
107101
)
108-
109-
wrapResponse(shim, express.response)
110-
}
111-
112-
function wrapExpress3(shim, express) {
113-
// In Express 3 the app returned from `express()` is actually a `connect` app
114-
// which we have no access to before creation. We can not easily wrap the app
115-
// because there are a lot of methods dangling on it that act on the app itself.
116-
// Really we just care about apps being used as `request` event listeners on
117-
// `http.Server` instances so we'll wrap that instead.
118-
119-
shim.wrapMiddlewareMounter(
120-
express.Router.prototype,
121-
'param',
122-
new MiddlewareMounterSpec({
123-
route: shim.FIRST,
124-
wrapper: function wrapParamware(shim, middleware, fnName, route) {
125-
return shim.recordParamware(
126-
middleware,
127-
new MiddlewareSpec({
128-
name: route,
129-
req: shim.FIRST,
130-
next: shim.THIRD,
131-
type: MIDDLEWARE_TYPE_NAMES.PARAMWARE
132-
})
133-
)
134-
}
135-
})
136-
)
137-
shim.wrapMiddlewareMounter(
138-
express.Router.prototype,
139-
'use',
140-
new MiddlewareMounterSpec({
141-
route: shim.FIRST,
142-
wrapper: wrapMiddleware
143-
})
144-
)
145-
shim.wrapMiddlewareMounter(
146-
express.application,
147-
'use',
148-
new MiddlewareMounterSpec({
149-
route: shim.FIRST,
150-
wrapper: wrapMiddleware
151-
})
152-
)
153-
154-
// NOTE: Do not wrap application route methods in Express 3, they all just
155-
// forward their arguments to the router.
156-
wrapRouteMethods(shim, express.Router.prototype, shim.FIRST)
157-
wrapResponse(shim, express.response)
158102
}
159103

160104
function wrapRouteMethods(shim, route, path) {

test/lib/metrics_helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function assertMetrics(metrics, expected, exclusive, assertValues) {
4848
for (let i = 0, len = expected.length; i < len; i++) {
4949
const expectedMetric = expected[i]
5050
const metric = metrics.getMetric(expectedMetric[0].name, expectedMetric[0].scope)
51-
this.ok(metric)
51+
this.ok(metric, `should find ${expectedMetric[0].name}`)
5252
if (assertValues) {
5353
this.same(metric.toJSON(), expectedMetric[1])
5454
}

test/versioned/express-esm/package.json

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,22 @@
1010
"node": ">=18"
1111
},
1212
"dependencies": {
13-
"express": ">=4.6.0",
14-
"express-enrouten": "1.1",
15-
"ejs": "2.5.9"
13+
"express": {
14+
"versions": ">=4.6.0",
15+
"samples": 5
16+
}
17+
},
18+
"files": [
19+
"segments.tap.mjs",
20+
"transaction-naming.tap.mjs"
21+
]
22+
},
23+
{
24+
"engines": {
25+
"node": ">=18"
26+
},
27+
"dependencies": {
28+
"express": "5.0.0-beta.3"
1629
},
1730
"files": [
1831
"segments.tap.mjs",

0 commit comments

Comments
 (0)