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

Migrate from AsyncResource to TracingChannel for Hapi.js plugin #4597

Closed
Closed
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
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(() => {})
})
})
})
})
Loading