Skip to content

Commit

Permalink
fix hapi plugin losing the scope when a payload is sent (#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
rochdev authored Sep 4, 2019
1 parent af98d71 commit d882349
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
59 changes: 59 additions & 0 deletions packages/datadog-plugin-hapi/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@ function createWrapDispatch (tracer, config) {
}
}

function createWrapRebuild () {
return function wrapRebuild (rebuild) {
return function rebuildWithTrace (event) {
rebuild.apply(this, arguments)

this._cycle = this._cycle.map(wrapMiddleware)
}
}
}

function createWrapLifecycle () {
return function wrapLifecycle (lifecycle) {
return function lifecycleWithTrace () {
return lifecycle.apply(this, arguments).map(wrapMiddleware)
}
}
}

function wrapMiddleware (middleware) {
if (typeof middleware !== 'function') return middleware

return function (request, next) {
return web.reactivate(request.raw.req, () => middleware.apply(this, arguments))
}
}

module.exports = [
{
name: '@hapi/hapi',
Expand All @@ -60,6 +86,17 @@ module.exports = [
this.unwrap(Request, 'generate')
}
},
{
name: '@hapi/hapi',
versions: ['>=17.9'],
file: 'lib/route.js',
patch (Route, tracer, config) {
this.wrap(Route.prototype, 'rebuild', createWrapRebuild(tracer, config))
},
unpatch (Route) {
this.unwrap(Route.prototype, 'rebuild')
}
},
{
name: 'hapi',
versions: ['>=17.1'],
Expand Down Expand Up @@ -93,6 +130,28 @@ module.exports = [
this.unwrap(Request.prototype, '_execute')
}
},
{
name: 'hapi',
versions: ['>=10.4'],
file: 'lib/route.js',
patch (Route, tracer, config) {
this.wrap(Route.prototype, 'rebuild', createWrapRebuild(tracer, config))
},
unpatch (Route) {
this.unwrap(Route.prototype, 'rebuild')
}
},
{
name: 'hapi',
versions: ['2 - 10.3'],
file: 'lib/route.js',
patch (Route, tracer, config) {
this.wrap(Route.prototype, 'lifecycle', createWrapLifecycle(tracer, config))
},
unpatch (Route) {
this.unwrap(Route.prototype, 'lifecycle')
}
},
{
name: '@hapi/hapi',
versions: ['>=17.9'],
Expand Down
21 changes: 21 additions & 0 deletions packages/datadog-plugin-hapi/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@ describe('Plugin', () => {
.catch(done)
})

it('should run the request handler in the request scope with a payload', done => {
server.route({
method: 'POST',
path: '/user/{id}',
handler: (request, h) => {
try {
expect(tracer.scope().active()).to.not.be.null
done()
} catch (e) {
done(e)
}

return handler(request, h)
}
})

axios
.post(`http://localhost:${port}/user/123`, {})
.catch(done)
})

it('should run pre-handlers in the request scope', done => {
server.route({
method: 'GET',
Expand Down

0 comments on commit d882349

Please sign in to comment.