From 97234e48832f41bf2834bb6dcb09e7f51255fd7e Mon Sep 17 00:00:00 2001 From: Ali Sabzevari Date: Tue, 3 Aug 2021 16:56:26 +0200 Subject: [PATCH] chore(instrumentation): fix lint warnings --- .../src/autoLoaderUtils.ts | 19 ++++++++++++++----- .../src/instrumentation.ts | 8 ++++---- .../src/platform/node/instrumentation.ts | 16 ++++++++-------- .../src/platform/node/types.ts | 2 +- .../src/types.ts | 4 +++- .../src/utils.ts | 2 +- .../test/common/autoLoader.test.ts | 1 + 7 files changed, 32 insertions(+), 20 deletions(-) diff --git a/packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts b/packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts index e070fb3d10f..7e7d152f1dd 100644 --- a/packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts +++ b/packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts @@ -18,6 +18,7 @@ import { TracerProvider } from '@opentelemetry/api'; import { MeterProvider } from '@opentelemetry/api-metrics'; import { Instrumentation } from './types'; import { AutoLoaderResult, InstrumentationOption } from './types_internal'; +import { InstrumentationBase } from './platform'; /** * Parses the options and returns instrumentations, node plugins and @@ -29,13 +30,13 @@ export function parseInstrumentationOptions( ): AutoLoaderResult { let instrumentations: Instrumentation[] = []; for (let i = 0, j = options.length; i < j; i++) { - const option = options[i] as any; + const option = options[i]; if (Array.isArray(option)) { const results = parseInstrumentationOptions(option); instrumentations = instrumentations.concat(results.instrumentations); - } else if (typeof option === 'function') { + } else if (isInstrumentationClass(option)) { instrumentations.push(new option()); - } else if ((option as Instrumentation).instrumentationName) { + } else if (isInstrumentation(option)) { instrumentations.push(option); } } @@ -43,6 +44,14 @@ export function parseInstrumentationOptions( return { instrumentations }; } +function isInstrumentationClass(option: InstrumentationOption): option is new () => T { + return typeof option === 'function'; +} + +function isInstrumentation(option: InstrumentationOption): option is Instrumentation { + return Boolean((option as Instrumentation).instrumentationName); +} + /** * Enable instrumentations * @param instrumentations @@ -53,7 +62,7 @@ export function enableInstrumentations( instrumentations: Instrumentation[], tracerProvider?: TracerProvider, meterProvider?: MeterProvider -) { +): void { for (let i = 0, j = instrumentations.length; i < j; i++) { const instrumentation = instrumentations[i]; if (tracerProvider) { @@ -70,6 +79,6 @@ export function enableInstrumentations( * Disable instrumentations * @param instrumentations */ -export function disableInstrumentations(instrumentations: Instrumentation[]) { +export function disableInstrumentations(instrumentations: Instrumentation[]): void { instrumentations.forEach(instrumentation => instrumentation.disable()); } diff --git a/packages/opentelemetry-instrumentation/src/instrumentation.ts b/packages/opentelemetry-instrumentation/src/instrumentation.ts index 56779a5e64c..687b6115019 100644 --- a/packages/opentelemetry-instrumentation/src/instrumentation.ts +++ b/packages/opentelemetry-instrumentation/src/instrumentation.ts @@ -74,7 +74,7 @@ export abstract class InstrumentationAbstract * Sets MeterProvider to this plugin * @param meterProvider */ - public setMeterProvider(meterProvider: MeterProvider) { + public setMeterProvider(meterProvider: MeterProvider): void { this._meter = meterProvider.getMeter( this.instrumentationName, this.instrumentationVersion @@ -82,7 +82,7 @@ export abstract class InstrumentationAbstract } /* Returns InstrumentationConfig */ - public getConfig() { + public getConfig(): types.InstrumentationConfig { return this._config; } @@ -90,7 +90,7 @@ export abstract class InstrumentationAbstract * Sets InstrumentationConfig to this plugin * @param InstrumentationConfig */ - public setConfig(config: types.InstrumentationConfig = {}) { + public setConfig(config: types.InstrumentationConfig = {}): void { this._config = Object.assign({}, config); } @@ -98,7 +98,7 @@ export abstract class InstrumentationAbstract * Sets TraceProvider to this plugin * @param tracerProvider */ - public setTracerProvider(tracerProvider: TracerProvider) { + public setTracerProvider(tracerProvider: TracerProvider): void { this._tracer = tracerProvider.getTracer( this.instrumentationName, this.instrumentationVersion diff --git a/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts b/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts index d03afadb934..67f25c6c47f 100644 --- a/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts +++ b/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts @@ -59,12 +59,12 @@ export abstract class InstrumentationBase } } - private _onRequire( - module: InstrumentationModuleDefinition, - exports: T, + private _onRequire( + module: InstrumentationModuleDefinition, + exports: U, name: string, baseDir?: string - ): T { + ): U { if (!baseDir) { if (typeof module.patch === 'function') { module.moduleExports = exports; @@ -92,7 +92,7 @@ export abstract class InstrumentationBase } else { // internal file const files = module.files ?? []; - const file = files.find(file => file.name === name); + const file = files.find(f => f.name === name); if (file && isSupported(file.supportedVersions, version, module.includePrerelease)) { file.moduleExports = exports; if (this._enabled) { @@ -103,7 +103,7 @@ export abstract class InstrumentationBase return exports; } - public enable() { + public enable(): void { if (this._enabled) { return; } @@ -144,7 +144,7 @@ export abstract class InstrumentationBase } } - public disable() { + public disable(): void { if (!this._enabled) { return; } @@ -162,7 +162,7 @@ export abstract class InstrumentationBase } } - public isEnabled() { + public isEnabled(): boolean { return this._enabled; } } diff --git a/packages/opentelemetry-instrumentation/src/platform/node/types.ts b/packages/opentelemetry-instrumentation/src/platform/node/types.ts index c5ea980c305..94faf3633be 100644 --- a/packages/opentelemetry-instrumentation/src/platform/node/types.ts +++ b/packages/opentelemetry-instrumentation/src/platform/node/types.ts @@ -45,7 +45,7 @@ export interface InstrumentationModuleDefinition { supportedVersions: string[]; /** Module internal files to be patched */ - files: InstrumentationModuleFile[]; + files: InstrumentationModuleFile[]; /** If set to true, the includePrerelease check will be included when calling semver.satisfies */ includePrerelease?: boolean; diff --git a/packages/opentelemetry-instrumentation/src/types.ts b/packages/opentelemetry-instrumentation/src/types.ts index 4ed6febf10a..76aad31834d 100644 --- a/packages/opentelemetry-instrumentation/src/types.ts +++ b/packages/opentelemetry-instrumentation/src/types.ts @@ -77,8 +77,10 @@ export interface InstrumentationConfig { * This interface defines the params that are be added to the wrapped function * using the "shimmer.wrap" */ -export interface ShimWrapped { +export interface ShimWrapped extends Function { __wrapped: boolean; + // eslint-disable-next-line @typescript-eslint/ban-types __unwrap: Function; + // eslint-disable-next-line @typescript-eslint/ban-types __original: Function; } diff --git a/packages/opentelemetry-instrumentation/src/utils.ts b/packages/opentelemetry-instrumentation/src/utils.ts index 8f1c98c1fa2..297a9b13434 100644 --- a/packages/opentelemetry-instrumentation/src/utils.ts +++ b/packages/opentelemetry-instrumentation/src/utils.ts @@ -73,7 +73,7 @@ export async function safeExecuteInTheMiddleAsync( * Checks if certain function has been already wrapped * @param func */ -export function isWrapped(func: any) { +export function isWrapped(func: unknown): func is ShimWrapped { return ( typeof func === 'function' && typeof (func as ShimWrapped).__original === 'function' && diff --git a/packages/opentelemetry-instrumentation/test/common/autoLoader.test.ts b/packages/opentelemetry-instrumentation/test/common/autoLoader.test.ts index c378f83fe61..b65ec690d94 100644 --- a/packages/opentelemetry-instrumentation/test/common/autoLoader.test.ts +++ b/packages/opentelemetry-instrumentation/test/common/autoLoader.test.ts @@ -34,6 +34,7 @@ class FooInstrumentation extends InstrumentationBase { } describe('autoLoader', () => { + // eslint-disable-next-line @typescript-eslint/ban-types let unload: Function | undefined; afterEach(() => {