Skip to content

Commit

Permalink
hapi: migrate from AsyncResource to TracingChannel, see #4597 (#4622)
Browse files Browse the repository at this point in the history
* fix: remove async scope in hapi dd-instrumentation
* fix linting
* replace asyncResource for tracingChannel
* move tracing channels to helpers
* add test
* remove tracingChannel map

---------

Co-authored-by: Cris Naranjo <cris@yulife.com>
  • Loading branch information
2 people authored and bengl committed Aug 30, 2024
1 parent f807d31 commit a500276
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
11 changes: 4 additions & 7 deletions packages/datadog-instrumentations/src/hapi.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict'

const tracingChannel = require('dc-polyfill').tracingChannel
const shimmer = require('../../datadog-shimmer')
const { addHook, channel, AsyncResource } = require('./helpers/instrument')
const { addHook, channel } = require('./helpers/instrument')

const handleChannel = channel('apm:hapi:request:handle')
const routeChannel = channel('apm:hapi:request:route')
const errorChannel = channel('apm:hapi:request:error')
const enterChannel = channel('apm:hapi:extension:enter')
const hapiTracingChannel = tracingChannel('apm:hapi:extension')

function wrapServer (server) {
return function (options) {
Expand Down Expand Up @@ -96,11 +97,7 @@ function wrapHandler (handler) {

if (!req) return handler.apply(this, arguments)

const asyncResource = new AsyncResource('bound-anonymous-fn')

return asyncResource.runInAsyncScope(() => {
enterChannel.publish({ req })

return hapiTracingChannel.traceSync(() => {
return handler.apply(this, arguments)
})
}
Expand Down
4 changes: 2 additions & 2 deletions packages/datadog-plugin-hapi/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class HapiPlugin extends RouterPlugin {
}
})

this.addSub('apm:hapi:extension:enter', ({ req }) => {
this.enter(this._requestSpans.get(req))
this.addBind('apm:hapi:extension:start', ({ req }) => {
return this._requestSpans.get(req)
})
}
}
Expand Down
25 changes: 25 additions & 0 deletions packages/datadog-plugin-hapi/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const axios = require('axios')
const semver = require('semver')
const agent = require('../../dd-trace/test/plugins/agent')
const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants')
const { AsyncLocalStorage } = require('async_hooks')

const versionRange = parseInt(process.versions.node.split('.')[0]) > 14
? '<17 || >18'
Expand Down Expand Up @@ -348,6 +349,30 @@ describe('Plugin', () => {
.get(`http://localhost:${port}/user/123`)
.catch(() => {})
})

it('should persist AsyncLocalStorage context', (done) => {
const als = new AsyncLocalStorage()
const path = '/path'

server.ext('onRequest', (request, h) => {
als.enterWith({ path: request.path })
return reply(request, h)
})

server.route({
method: 'GET',
path,
handler: async (request, h) => {
expect(als.getStore()).to.deep.equal({ path })
done()
return h.response ? h.response() : h()
}
})

axios
.get(`http://localhost:${port}${path}`)
.catch(() => {})
})
})
})
})

0 comments on commit a500276

Please sign in to comment.