From addb1bee0b586086f0d8fd221abb310f22d5812a Mon Sep 17 00:00:00 2001 From: rochdev Date: Fri, 25 May 2018 11:07:25 -0400 Subject: [PATCH 1/2] fix express resource with multiple middlewares matching the route --- src/plugins/express.js | 12 +++++++++++- test/plugins/express.spec.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/plugins/express.js b/src/plugins/express.js index b7a51c3b609..5d33429e178 100644 --- a/src/plugins/express.js +++ b/src/plugins/express.js @@ -99,7 +99,17 @@ function createWrapRouterMethod (tracer) { layer.handle_request = (req, res, next) => { if (req._datadog_trace_patched) { - next = context.bind(next) + const originalNext = next + + next = context.bind(function () { + const paths = context.get('express.paths') + + if (paths && layer.path) { + paths.pop() + } + + originalNext.apply(null, arguments) + }) } return handle.call(layer, req, res, next) diff --git a/test/plugins/express.spec.js b/test/plugins/express.spec.js index 7ab186f6df0..47fd6b98a79 100644 --- a/test/plugins/express.spec.js +++ b/test/plugins/express.spec.js @@ -140,6 +140,37 @@ describe('Plugin', () => { }) }) + it('should only keep the last matching path of a middleware stack', done => { + const app = express() + const router = express.Router() + + router.use('/bar', (req, res, next) => next()) + + app.use('/', (req, res, next) => next()) + app.use('*', (req, res, next) => next()) + app.use('/*', (req, res, next) => next()) + app.use('/foo/bar', (req, res, next) => next()) + app.use('/foo', router) + app.use('/foo/bar', (req, res, next) => { + res.status(200).send() + }) + + 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() From b91779ae38b71e2f7e74b7f24cc618b5e9e75d93 Mon Sep 17 00:00:00 2001 From: rochdev Date: Fri, 25 May 2018 11:58:56 -0400 Subject: [PATCH 2/2] fix star wildcard not tested properly --- src/plugins/express.js | 2 +- test/plugins/express.spec.js | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/plugins/express.js b/src/plugins/express.js index 5d33429e178..142ae64750e 100644 --- a/src/plugins/express.js +++ b/src/plugins/express.js @@ -104,7 +104,7 @@ function createWrapRouterMethod (tracer) { next = context.bind(function () { const paths = context.get('express.paths') - if (paths && layer.path) { + if (paths && layer.path && !layer.regexp.fast_star) { paths.pop() } diff --git a/test/plugins/express.spec.js b/test/plugins/express.spec.js index 47fd6b98a79..1f9cecc699d 100644 --- a/test/plugins/express.spec.js +++ b/test/plugins/express.spec.js @@ -144,16 +144,17 @@ describe('Plugin', () => { 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('/*', (req, res, next) => next()) app.use('/foo/bar', (req, res, next) => next()) app.use('/foo', router) - app.use('/foo/bar', (req, res, next) => { - res.status(200).send() - }) getPort().then(port => { agent