Skip to content

Commit 55a1fc8

Browse files
authored
fix(instrumentation-http): fix http/https ESM instr for 'import defaultExport from' style (#5075)
1 parent 5e17361 commit 55a1fc8

File tree

3 files changed

+230
-155
lines changed

3 files changed

+230
-155
lines changed

experimental/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ All notable changes to experimental packages in this project will be documented
4949
* `OTEL_EXPORTER_OTLP_LOGS_INSECURE`
5050
* fix(sdk-node): use warn instead of error on unknown OTEL_NODE_RESOURCE_DETECTORS values [#5034](https://github.com/open-telemetry/opentelemetry-js/pull/5034)
5151
* fix(exporter-logs-otlp-proto): Use correct config type in Node constructor
52+
* fix(instrumentation-http): Fix instrumentation of `http.get`, `http.request`, `https.get`, and `https.request` when used from ESM code and imported via the `import defaultExport from 'http'` style. [#5024](https://github.com/open-telemetry/opentelemetry-js/issues/5024) @trentm
5253

5354
### :books: (Refine Doc)
5455

experimental/packages/opentelemetry-instrumentation-http/src/http.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,17 +235,24 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
235235
'http',
236236
['*'],
237237
(moduleExports: Http): Http => {
238+
const isESM = (moduleExports as any)[Symbol.toStringTag] === 'Module';
238239
if (!this.getConfig().disableOutgoingRequestInstrumentation) {
239240
const patchedRequest = this._wrap(
240241
moduleExports,
241242
'request',
242243
this._getPatchOutgoingRequestFunction('http')
243244
) as unknown as Func<http.ClientRequest>;
244-
this._wrap(
245+
const patchedGet = this._wrap(
245246
moduleExports,
246247
'get',
247248
this._getPatchOutgoingGetFunction(patchedRequest)
248249
);
250+
if (isESM) {
251+
// To handle `import http from 'http'`, which returns the default
252+
// export, we need to set `module.default.*`.
253+
(moduleExports as any).default.request = patchedRequest;
254+
(moduleExports as any).default.get = patchedGet;
255+
}
249256
}
250257
if (!this.getConfig().disableIncomingRequestInstrumentation) {
251258
this._wrap(
@@ -275,17 +282,24 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
275282
'https',
276283
['*'],
277284
(moduleExports: Https): Https => {
285+
const isESM = (moduleExports as any)[Symbol.toStringTag] === 'Module';
278286
if (!this.getConfig().disableOutgoingRequestInstrumentation) {
279287
const patchedRequest = this._wrap(
280288
moduleExports,
281289
'request',
282290
this._getPatchHttpsOutgoingRequestFunction('https')
283291
) as unknown as Func<http.ClientRequest>;
284-
this._wrap(
292+
const patchedGet = this._wrap(
285293
moduleExports,
286294
'get',
287295
this._getPatchHttpsOutgoingGetFunction(patchedRequest)
288296
);
297+
if (isESM) {
298+
// To handle `import https from 'https'`, which returns the default
299+
// export, we need to set `module.default.*`.
300+
(moduleExports as any).default.request = patchedRequest;
301+
(moduleExports as any).default.get = patchedGet;
302+
}
289303
}
290304
if (!this.getConfig().disableIncomingRequestInstrumentation) {
291305
this._wrap(

0 commit comments

Comments
 (0)