Skip to content
Merged
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
19 changes: 16 additions & 3 deletions src/plugins/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ function createWrapRouterMethod (tracer) {
const handleError = layer.handle_error

layer.handle_request = (req, res, next) => {
return handleRequest.call(layer, req, res, wrapNext(tracer, req, next))
return handleRequest.call(layer, req, res, wrapNext(tracer, layer, req, next))
}

layer.handle_error = (error, req, res, next) => {
return handleError.call(layer, error, req, res, wrapNext(tracer, req, next))
return handleError.call(layer, error, req, res, wrapNext(tracer, layer, req, next))
}

layer._datadog_matchers = matchers
Expand All @@ -112,8 +112,21 @@ function createWrapRouterMethod (tracer) {
}
}

function wrapNext (tracer, req, next) {
function wrapNext (tracer, layer, req, next) {
if (req._datadog_trace_patched) {
const context = tracer._context
const originalNext = next

next = context.bind(function () {
const paths = context.get('express.paths')

if (paths && layer.path && !layer.regexp.fast_star) {
paths.pop()
}

originalNext.apply(null, arguments)
})

return tracer._context.bind(next)
}

Expand Down
32 changes: 32 additions & 0 deletions test/plugins/express.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,38 @@ describe('Plugin', () => {
})
})

it('should only keep the last matching path of a middleware stack', done => {
const app = express()
const router = express.Router()

router.use('/', (req, res, next) => next())
router.use('*', (req, res, next) => next())
router.use('/bar', (req, res, next) => next())
router.use('/bar', (req, res, next) => {
res.status(200).send()
})

app.use('/', (req, res, next) => next())
app.use('*', (req, res, next) => next())
app.use('/foo/bar', (req, res, next) => next())
app.use('/foo', router)

getPort().then(port => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('resource', '/foo/bar')
})
.then(done)
.catch(done)

appListener = app.listen(port, 'localhost', () => {
axios
.get(`http://localhost:${port}/foo/bar`)
.catch(done)
})
})
})

it('should support asynchronous routers', done => {
const app = express()
const router = express.Router()
Expand Down