Skip to content

Commit 732bf01

Browse files
committed
refactor(instrumentation-http): fix eslint warnings
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
1 parent 3c040c4 commit 732bf01

File tree

1 file changed

+36
-12
lines changed
  • experimental/packages/opentelemetry-instrumentation-http/src

1 file changed

+36
-12
lines changed

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

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,32 @@ import {
9090
SemconvStability,
9191
} from './internal-types';
9292

93+
/**
94+
* This is by no means general-purpose nor completely safe, but for the purpose
95+
* of the instrumentation in this module, we want to check if a node module
96+
* looks like a ESM with a default export aliased to itself. That is – modules
97+
* that supports both:
98+
*
99+
* ```
100+
* import { request } from 'node:http';
101+
* ```
102+
*
103+
* ...and...
104+
*
105+
* ```
106+
* import * as http from 'node:http';
107+
* const { request } = http;
108+
* ```
109+
*/
110+
function hasEsmDefaultExport<T extends object>(
111+
moduleExports: T
112+
): moduleExports is T & { default: T } {
113+
return (
114+
(moduleExports as { [Symbol.toStringTag]?: string })[Symbol.toStringTag] ===
115+
'Module' && 'default' in moduleExports
116+
);
117+
}
118+
93119
/**
94120
* `node:http` and `node:https` instrumentation for OpenTelemetry
95121
*/
@@ -233,23 +259,22 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
233259
'http',
234260
['*'],
235261
(moduleExports: Http): Http => {
236-
const isESM = (moduleExports as any)[Symbol.toStringTag] === 'Module';
237262
if (!this.getConfig().disableOutgoingRequestInstrumentation) {
238263
const patchedRequest = this._wrap(
239264
moduleExports,
240265
'request',
241266
this._getPatchOutgoingRequestFunction('http')
242-
) as unknown as Func<http.ClientRequest>;
267+
) as unknown as Http['request'];
243268
const patchedGet = this._wrap(
244269
moduleExports,
245270
'get',
246271
this._getPatchOutgoingGetFunction(patchedRequest)
247-
);
248-
if (isESM) {
272+
) as unknown as Http['get'];
273+
if (hasEsmDefaultExport(moduleExports)) {
249274
// To handle `import http from 'http'`, which returns the default
250275
// export, we need to set `module.default.*`.
251-
(moduleExports as any).default.request = patchedRequest;
252-
(moduleExports as any).default.get = patchedGet;
276+
moduleExports.default.request = patchedRequest;
277+
moduleExports.default.get = patchedGet;
253278
}
254279
}
255280
if (!this.getConfig().disableIncomingRequestInstrumentation) {
@@ -280,23 +305,22 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
280305
'https',
281306
['*'],
282307
(moduleExports: Https): Https => {
283-
const isESM = (moduleExports as any)[Symbol.toStringTag] === 'Module';
284308
if (!this.getConfig().disableOutgoingRequestInstrumentation) {
285309
const patchedRequest = this._wrap(
286310
moduleExports,
287311
'request',
288312
this._getPatchHttpsOutgoingRequestFunction('https')
289-
) as unknown as Func<http.ClientRequest>;
313+
) as unknown as Https['request'];
290314
const patchedGet = this._wrap(
291315
moduleExports,
292316
'get',
293317
this._getPatchHttpsOutgoingGetFunction(patchedRequest)
294-
);
295-
if (isESM) {
318+
) as unknown as Https['get'];
319+
if (hasEsmDefaultExport(moduleExports)) {
296320
// To handle `import https from 'https'`, which returns the default
297321
// export, we need to set `module.default.*`.
298-
(moduleExports as any).default.request = patchedRequest;
299-
(moduleExports as any).default.get = patchedGet;
322+
moduleExports.default.request = patchedRequest;
323+
moduleExports.default.get = patchedGet;
300324
}
301325
}
302326
if (!this.getConfig().disableIncomingRequestInstrumentation) {

0 commit comments

Comments
 (0)