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
21 changes: 10 additions & 11 deletions src/plugins/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ function createWrapMethod (tracer, config) {
}, span => {
const originalEnd = res.end

res.end = function () {
res.end = originalEnd
const returned = res.end.apply(this, arguments)
const paths = tracer._context.get('express.paths')
res.end = tracer.bind(function () {
const returned = originalEnd.apply(this, arguments)
const paths = tracer.currentSpan().context()._express_paths

if (paths) {
span.setTag('resource.name', paths.join(''))
Expand All @@ -40,11 +39,11 @@ function createWrapMethod (tracer, config) {
span.finish()

return returned
}
})

req._datadog_trace_patched = true

return next()
next()
})
}

Expand All @@ -65,15 +64,15 @@ function createWrapProcessParams (tracer, config) {
return function wrapProcessParams (processParams) {
return function processParamsWithTrace (layer, called, req, res, done) {
const matchers = layer._datadog_matchers
let paths = context.get('express.paths') || []
const span = context.get('current')

if (matchers && span) {
const paths = span.context()._express_paths || []

if (matchers) {
// Try to guess which path actually matched
for (let i = 0; i < matchers.length; i++) {
if (matchers[i].test(layer.path)) {
paths = paths.concat(matchers[i].path)

context.set('express.paths', paths)
span.context()._express_paths = paths.concat(matchers[i].path)

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

it('should bind the response to the current context', done => {
const app = express()

context.run(() => {
const send = context.bind(res => res.status(200).send())

app.get('/user', (req, res) => {
send(res)
})
})

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

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

it('should bind the next callback to the current context', done => {
const app = express()

Expand Down