Skip to content

Commit

Permalink
Change blocking approach
Browse files Browse the repository at this point in the history
  • Loading branch information
uurien committed Jun 28, 2024
1 parent 951898d commit 194ec41
Showing 1 changed file with 5 additions and 36 deletions.
41 changes: 5 additions & 36 deletions packages/datadog-instrumentations/src/http/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const { channel, addHook } = require('../helpers/instrument')
const shimmer = require('../../../datadog-shimmer')

const log = require('../../../dd-trace/src/log')
const { storage } = require('../../../datadog-core')

const startChannel = channel('apm:http:client:request:start')
const finishChannel = channel('apm:http:client:request:finish')
Expand All @@ -26,30 +25,6 @@ function hookFn (http) {
return http
}

let ClientRequest
function noop () {}

function createAbortedClientRequest (http, args) {
if (!ClientRequest) {
const store = storage.getStore()
storage.enterWith({ noop: true })

const clientRequest = http.get('<invalid-url>')
clientRequest.on('error', noop)
ClientRequest = Object.getPrototypeOf(clientRequest).constructor

storage.enterWith(store)
}

return new ClientRequest({
_defaultAgent: http.globalAgent, // needed to support http and https
...args.options,
agent: { // noop agent, to prevent doing a real request
addRequest: noop
}
})
}

function patch (http, methodName) {
shimmer.wrap(http, methodName, instrumentRequest)

Expand Down Expand Up @@ -94,17 +69,7 @@ function patch (http, methodName) {
}

try {
let req
if (abortController.signal.aborted) {
req = createAbortedClientRequest(http, args)

process.nextTick(() => {
req.emit('error', abortController.signal.reason || new Error('Aborted'))
})
} else {
req = request.call(this, options, callback)
}

const req = request.call(this, options, callback)
const emit = req.emit
const setTimeout = req.setTimeout

Expand Down Expand Up @@ -144,6 +109,10 @@ function patch (http, methodName) {
return emit.apply(this, arguments)
}

if (abortController.signal.aborted) {
req.destroy(abortController.signal.reason || new Error('Aborted'))
}

return req
} catch (e) {
ctx.error = e
Expand Down

0 comments on commit 194ec41

Please sign in to comment.