Skip to content

Commit

Permalink
fix(instrumentation-http): fix http/https ESM instr for 'import defau…
Browse files Browse the repository at this point in the history
…ltExport from' style (#5075)
  • Loading branch information
trentm authored Oct 22, 2024
1 parent 5e17361 commit 55a1fc8
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 155 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ All notable changes to experimental packages in this project will be documented
* `OTEL_EXPORTER_OTLP_LOGS_INSECURE`
* 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)
* fix(exporter-logs-otlp-proto): Use correct config type in Node constructor
* 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

### :books: (Refine Doc)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,24 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
'http',
['*'],
(moduleExports: Http): Http => {
const isESM = (moduleExports as any)[Symbol.toStringTag] === 'Module';
if (!this.getConfig().disableOutgoingRequestInstrumentation) {
const patchedRequest = this._wrap(
moduleExports,
'request',
this._getPatchOutgoingRequestFunction('http')
) as unknown as Func<http.ClientRequest>;
this._wrap(
const patchedGet = this._wrap(
moduleExports,
'get',
this._getPatchOutgoingGetFunction(patchedRequest)
);
if (isESM) {
// To handle `import http from 'http'`, which returns the default
// export, we need to set `module.default.*`.
(moduleExports as any).default.request = patchedRequest;
(moduleExports as any).default.get = patchedGet;
}
}
if (!this.getConfig().disableIncomingRequestInstrumentation) {
this._wrap(
Expand Down Expand Up @@ -275,17 +282,24 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
'https',
['*'],
(moduleExports: Https): Https => {
const isESM = (moduleExports as any)[Symbol.toStringTag] === 'Module';
if (!this.getConfig().disableOutgoingRequestInstrumentation) {
const patchedRequest = this._wrap(
moduleExports,
'request',
this._getPatchHttpsOutgoingRequestFunction('https')
) as unknown as Func<http.ClientRequest>;
this._wrap(
const patchedGet = this._wrap(
moduleExports,
'get',
this._getPatchHttpsOutgoingGetFunction(patchedRequest)
);
if (isESM) {
// To handle `import https from 'https'`, which returns the default
// export, we need to set `module.default.*`.
(moduleExports as any).default.request = patchedRequest;
(moduleExports as any).default.get = patchedGet;
}
}
if (!this.getConfig().disableIncomingRequestInstrumentation) {
this._wrap(
Expand Down
Loading

0 comments on commit 55a1fc8

Please sign in to comment.