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 context propagation for route handlers in fastify #845

Merged
merged 1 commit into from
Feb 4, 2020
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
41 changes: 40 additions & 1 deletion packages/datadog-plugin-fastify/src/fastify.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const methods = require('methods').concat('all')
const web = require('../../dd-trace/src/plugins/util/web')

function createWrapFastify (tracer, config) {
Expand All @@ -15,11 +16,19 @@ function createWrapFastify (tracer, config) {
fastify._datadog_wrapper = function () {
const app = fastify.apply(this, arguments)

if (app && typeof app.addHook === 'function') {
if (!app) return app

if (typeof app.addHook === 'function') {
app.addHook('onRequest', createOnRequest(tracer, config))
app.addHook('preHandler', preHandler)
}

methods.forEach(method => {
app[method] = wrapMethod(app[method])
})

app.route = wrapRoute(app.route)

return app
}

Expand Down Expand Up @@ -58,6 +67,36 @@ function wrapSend (send) {
}
}

function wrapRoute (route) {
if (typeof route !== 'function') return route

return function routeWithTrace (opts) {
opts.handler = wrapHandler(opts.handler)

return route.apply(this, arguments)
}
}

function wrapMethod (method) {
if (typeof method !== 'function') return method

return function methodWithTrace (url, opts, handler) {
const lastIndex = arguments.length - 1

arguments[lastIndex] = wrapHandler(arguments[lastIndex])

return method.apply(this, arguments)
}
}

function wrapHandler (handler) {
return function handlerWithTrace (request, reply) {
const req = getReq(request)

return web.reactivate(req, () => handler.apply(this, arguments))
}
}

function unwrapFastify (fastify) {
fastify._datadog_wrapper = fastify
}
Expand Down
43 changes: 43 additions & 0 deletions packages/datadog-plugin-fastify/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,49 @@ describe('Plugin', () => {
})
})

it('should run POST handlers in the request scope', done => {
if (process.env.DD_CONTEXT_PROPAGATION === 'false') return done()

app.post('/user', (request, reply) => {
expect(tracer.scope().active()).to.not.be.null
reply.send()
})

getPort().then(port => {
app.listen(port, 'localhost', () => {
axios.post(`http://localhost:${port}/user`, { foo: 'bar' })
.then(() => done())
.catch(done)
})
})
})

it('should run routes in the request scope', done => {
if (process.env.DD_CONTEXT_PROPAGATION === 'false') return done()

app.use((req, res, next) => {
expect(tracer.scope().active()).to.not.be.null
next()
})

app.route({
method: 'POST',
url: '/user',
handler: (request, reply) => {
expect(tracer.scope().active()).to.not.be.null
reply.send()
}
})

getPort().then(port => {
app.listen(port, 'localhost', () => {
axios.post(`http://localhost:${port}/user`, { foo: 'bar' })
.then(() => done())
.catch(done)
})
})
})

it('should run hooks in the request scope', done => {
if (process.env.DD_CONTEXT_PROPAGATION === 'false') return done()

Expand Down