Skip to content

Commit

Permalink
ensure we keep refs to the channels
Browse files Browse the repository at this point in the history
  • Loading branch information
flovilmart committed Oct 19, 2021
1 parent e3cadc6 commit ae925f6
Show file tree
Hide file tree
Showing 2 changed files with 243 additions and 172 deletions.
37 changes: 22 additions & 15 deletions packages/datadog-plugin-undici/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,37 @@ function parseHeaders (headers) {
})
return object
}
const channels = {
requestChannel: undefined,
headersChannel: undefined,
errorChannel: undefined
}

function diagnostics (tracer, config) {
let diagnosticsChannel
try {
diagnosticsChannel = require('diagnostics_channel')
} catch (e) {
log.error("Unable to configure undici, cannot require 'diagnostics_channel'")
log.error(
"Unable to configure undici, cannot require 'diagnostics_channel'"
)
return () => {}
}
config = normalizeConfig(tracer, config)

const requestChannel = diagnosticsChannel.channel('undici:request:create')
const headersChannel = diagnosticsChannel.channel('undici:request:headers')
const requestErrorChannel = diagnosticsChannel.channel(
'undici:request:error'
channels.requestChannel = diagnosticsChannel.channel('undici:request:create')
channels.headersChannel = diagnosticsChannel.channel(
'undici:request:headers'
)
channels.errorChannel = diagnosticsChannel.channel('undici:request:error')

channels.requestChannel.subscribe(handleRequestCreate)
channels.errorChannel.subscribe(handleRequestError)
channels.headersChannel.subscribe(handleRequestHeaders)

// We use a weakmap here to store the request / spans
const requestSpansMap = new WeakMap()

requestChannel.subscribe(handleRequestCreate)
requestErrorChannel.subscribe(handleRequestError)
headersChannel.subscribe(handleRequestHeaders)

function handleRequestCreate ({ request }) {
const method = (request.method || 'GET').toUpperCase()

Expand Down Expand Up @@ -123,14 +130,14 @@ function diagnostics (tracer, config) {
}

return function unsubscribe () {
if (requestChannel.hasSubscribers) {
requestChannel.unsubscribe(handleRequestCreate)
if (channels.requestChannel.hasSubscribers) {
channels.requestChannel.unsubscribe(handleRequestCreate)
}
if (headersChannel.hasSubscribers) {
headersChannel.unsubscribe(handleRequestHeaders)
if (channels.headersChannel.hasSubscribers) {
channels.headersChannel.unsubscribe(handleRequestHeaders)
}
if (requestErrorChannel.hasSubscribers) {
requestErrorChannel.unsubscribe(handleRequestError)
if (channels.requestErrorChannel.hasSubscribers) {
channels.errorChannel.unsubscribe(handleRequestError)
}
}
}
Expand Down
Loading

0 comments on commit ae925f6

Please sign in to comment.