diff --git a/packages/datadog-instrumentations/src/http/client.js b/packages/datadog-instrumentations/src/http/client.js index 856c1c0f8e2..bfd657756b9 100644 --- a/packages/datadog-instrumentations/src/http/client.js +++ b/packages/datadog-instrumentations/src/http/client.js @@ -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') @@ -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('') - 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) @@ -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 @@ -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