Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix express path being lost when active scope is closed #212

Merged
merged 1 commit into from
Aug 3, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 4 additions & 9 deletions src/plugins/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ function createWrapProcessParams (tracer, config) {
return function wrapProcessParams (processParams) {
return function processParamsWithTrace (layer, called, req, res, done) {
const matchers = layer._datadog_matchers
const scope = tracer.scopeManager().active()

if (matchers && scope) {
if (matchers) {
const paths = req._datadog_paths || []

// Try to guess which path actually matched
Expand Down Expand Up @@ -126,14 +125,10 @@ function wrapNext (tracer, layer, req, next) {
const originalNext = next

return function () {
const scope = tracer.scopeManager().active()

if (scope) {
const paths = req._datadog_paths
const paths = req._datadog_paths

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

originalNext.apply(null, arguments)
Expand Down
34 changes: 34 additions & 0 deletions test/plugins/express.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,40 @@ describe('Plugin', () => {
})
})

it('should not lose the current path without a scope', done => {
const app = express()
const router = express.Router()

router.use((req, res, next) => {
const scope = tracer.scopeManager().active()

scope.close()

next()
})

router.get('/user/:id', (req, res) => {
res.status(200).send()
})

app.use('/app', router)

getPort().then(port => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('resource', 'GET /app/user/:id')
})
.then(done)
.catch(done)

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

it('should fallback to the the verb if a path pattern could not be found', done => {
const app = express()

Expand Down