Skip to content

Commit 6c7cbc3

Browse files
authored
[CO] Add support for Express .use() routes (#6164)
1 parent 624e155 commit 6c7cbc3

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

packages/datadog-instrumentations/src/router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ function createWrapRouterMethod (name) {
146146
}
147147

148148
if (routeAddedChannel.hasSubscribers) {
149-
routeAddedChannel.publish({ topOfStackFunc: methodWithTrace, layer: this.stack[0] })
149+
routeAddedChannel.publish({ topOfStackFunc: methodWithTrace, layer: this.stack.at(-1) })
150150
}
151151

152152
if (this.stack.length > offset) {

packages/datadog-plugin-express/test/code_origin.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ describe('Plugin', () => {
8787
await assertCodeOrigin('/v1/user', { line, method: 'testCase', type: 'Context' })
8888
})
8989

90+
it('should support .use() routes', async function testCase () {
91+
app.get('/route_before', (req, res) => res.end())
92+
const line = getNextLineNumber()
93+
app.use('/foo', (req, res) => {
94+
res.end()
95+
})
96+
app.get('/route_after', (req, res) => res.end())
97+
98+
await assertCodeOrigin('/foo/bar', { line, method: 'testCase', type: 'Context' })
99+
})
100+
90101
it('should point to route handler even if passed through a middleware', async function testCase () {
91102
app.use((req, res, next) => {
92103
next()

0 commit comments

Comments
 (0)