Skip to content

Commit

Permalink
refactor(instrumentation-http): fix eslint warnings
Browse files Browse the repository at this point in the history
Fix the following eslint warnings:

```
/home/runner/work/opentelemetry-js/opentelemetry-js/experimental/packages/opentelemetry-instrumentation-http/src/http.ts
  236:41  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any
  251:31  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any
  252:31  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any
  283:41  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any
  298:31  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any
  299:31  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any
```

Extracted a utility function with more precise types for the job.

Ref open-telemetry#5365
  • Loading branch information
chancancode committed Jan 29, 2025
1 parent 3c040c4 commit 732bf01
Showing 1 changed file with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,32 @@ import {
SemconvStability,
} from './internal-types';

/**
* This is by no means general-purpose nor completely safe, but for the purpose
* of the instrumentation in this module, we want to check if a node module
* looks like a ESM with a default export aliased to itself. That is – modules
* that supports both:
*
* ```
* import { request } from 'node:http';
* ```
*
* ...and...
*
* ```
* import * as http from 'node:http';
* const { request } = http;
* ```
*/
function hasEsmDefaultExport<T extends object>(
moduleExports: T
): moduleExports is T & { default: T } {
return (
(moduleExports as { [Symbol.toStringTag]?: string })[Symbol.toStringTag] ===
'Module' && 'default' in moduleExports
);
}

/**
* `node:http` and `node:https` instrumentation for OpenTelemetry
*/
Expand Down Expand Up @@ -233,23 +259,22 @@ 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>;
) as unknown as Http['request'];
const patchedGet = this._wrap(
moduleExports,
'get',
this._getPatchOutgoingGetFunction(patchedRequest)
);
if (isESM) {
) as unknown as Http['get'];
if (hasEsmDefaultExport(moduleExports)) {
// 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;
moduleExports.default.request = patchedRequest;
moduleExports.default.get = patchedGet;
}
}
if (!this.getConfig().disableIncomingRequestInstrumentation) {
Expand Down Expand Up @@ -280,23 +305,22 @@ 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>;
) as unknown as Https['request'];
const patchedGet = this._wrap(
moduleExports,
'get',
this._getPatchHttpsOutgoingGetFunction(patchedRequest)
);
if (isESM) {
) as unknown as Https['get'];
if (hasEsmDefaultExport(moduleExports)) {
// 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;
moduleExports.default.request = patchedRequest;
moduleExports.default.get = patchedGet;
}
}
if (!this.getConfig().disableIncomingRequestInstrumentation) {
Expand Down

0 comments on commit 732bf01

Please sign in to comment.