Skip to content

Commit e383c4e

Browse files
authored
fix: Updated express instrumentation to properly instrument an array of middleware defined on a route (#3473)
1 parent 3b3ab72 commit e383c4e

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

lib/subscribers/express/route.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ class ExpressRouteSubscriber extends MiddlewareSubscriber {
3333
// express could have multiple routers, wrap them all
3434
for (let i = 0; i < routeArgs.length; i++) {
3535
const routeHandler = routeArgs[i]
36+
// express supports an array of middlewares as well when defining route
37+
// wrap each handler and reassign to args
38+
if (Array.isArray(routeHandler)) {
39+
for (let j = 0; j < routeHandler.length; j++) {
40+
const handler = routeHandler[j]
41+
routeArgs[i][j] = self.wrapper.wrap({ handler })
42+
}
43+
continue
44+
}
45+
3646
let segmentName = null
3747
let route = null
3848
if (routeHandler.stack) {

test/versioned/express/segments.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,59 @@ test('when using a regular expression in path', function (t, end) {
784784
})
785785
})
786786

787+
test('route defines middlewares as a series of arrays', function (t, end) {
788+
const { app } = t.nr
789+
790+
function mw1(req, res, next) {
791+
next()
792+
}
793+
794+
function mw2(req, res, next) {
795+
next()
796+
}
797+
798+
function mw3(req, res, next) {
799+
next()
800+
}
801+
802+
function mw4(req, res, next) {
803+
next()
804+
}
805+
806+
function handler(req, res) {
807+
res.end()
808+
}
809+
810+
app.get('/test', [mw1, mw2], [mw3, mw4], handler)
811+
812+
runTest(t, '/test', function (root, transaction) {
813+
assertSegments(
814+
transaction.trace,
815+
root,
816+
['Expressjs/Route Path: /test',
817+
[
818+
`${NAMES.EXPRESS.MIDDLEWARE}mw1`,
819+
`${NAMES.EXPRESS.MIDDLEWARE}mw2`,
820+
`${NAMES.EXPRESS.MIDDLEWARE}mw3`,
821+
`${NAMES.EXPRESS.MIDDLEWARE}mw4`,
822+
`${NAMES.EXPRESS.MIDDLEWARE}handler`
823+
]
824+
],
825+
assertSegmentsOptions
826+
)
827+
828+
checkMetrics(transaction.metrics, [
829+
`${NAMES.EXPRESS.MIDDLEWARE}mw1//test`,
830+
`${NAMES.EXPRESS.MIDDLEWARE}mw2//test`,
831+
`${NAMES.EXPRESS.MIDDLEWARE}mw3//test`,
832+
`${NAMES.EXPRESS.MIDDLEWARE}mw4//test`,
833+
`${NAMES.EXPRESS.MIDDLEWARE}handler//test`
834+
], '/test')
835+
836+
end()
837+
})
838+
})
839+
787840
const codeLevelMetrics = [true, false]
788841
for (const enabled of codeLevelMetrics) {
789842
test(`Code Level Metrics ${enabled}`, function (t, end) {

0 commit comments

Comments
 (0)