Skip to content

Commit

Permalink
chore(instrumentation): fix lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
alisabzevari committed Aug 8, 2021
1 parent 3eb5386 commit 97234e4
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 20 deletions.
19 changes: 14 additions & 5 deletions packages/opentelemetry-instrumentation/src/autoLoaderUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,20 +30,28 @@ 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);
}
}

return { instrumentations };
}

function isInstrumentationClass<T extends InstrumentationBase>(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
Expand All @@ -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) {
Expand All @@ -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());
}
8 changes: 4 additions & 4 deletions packages/opentelemetry-instrumentation/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,31 @@ export abstract class InstrumentationAbstract<T = any>
* Sets MeterProvider to this plugin
* @param meterProvider
*/
public setMeterProvider(meterProvider: MeterProvider) {
public setMeterProvider(meterProvider: MeterProvider): void {
this._meter = meterProvider.getMeter(
this.instrumentationName,
this.instrumentationVersion
);
}

/* Returns InstrumentationConfig */
public getConfig() {
public getConfig(): types.InstrumentationConfig {
return this._config;
}

/**
* Sets InstrumentationConfig to this plugin
* @param InstrumentationConfig
*/
public setConfig(config: types.InstrumentationConfig = {}) {
public setConfig(config: types.InstrumentationConfig = {}): void {
this._config = Object.assign({}, config);
}

/**
* Sets TraceProvider to this plugin
* @param tracerProvider
*/
public setTracerProvider(tracerProvider: TracerProvider) {
public setTracerProvider(tracerProvider: TracerProvider): void {
this._tracer = tracerProvider.getTracer(
this.instrumentationName,
this.instrumentationVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ export abstract class InstrumentationBase<T = any>
}
}

private _onRequire<T>(
module: InstrumentationModuleDefinition<T>,
exports: T,
private _onRequire<U>(
module: InstrumentationModuleDefinition<U>,
exports: U,
name: string,
baseDir?: string
): T {
): U {
if (!baseDir) {
if (typeof module.patch === 'function') {
module.moduleExports = exports;
Expand Down Expand Up @@ -92,7 +92,7 @@ export abstract class InstrumentationBase<T = any>
} 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) {
Expand All @@ -103,7 +103,7 @@ export abstract class InstrumentationBase<T = any>
return exports;
}

public enable() {
public enable(): void {
if (this._enabled) {
return;
}
Expand Down Expand Up @@ -144,7 +144,7 @@ export abstract class InstrumentationBase<T = any>
}
}

public disable() {
public disable(): void {
if (!this._enabled) {
return;
}
Expand All @@ -162,7 +162,7 @@ export abstract class InstrumentationBase<T = any>
}
}

public isEnabled() {
public isEnabled(): boolean {
return this._enabled;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface InstrumentationModuleDefinition<T> {
supportedVersions: string[];

/** Module internal files to be patched */
files: InstrumentationModuleFile<any>[];
files: InstrumentationModuleFile<T>[];

/** If set to true, the includePrerelease check will be included when calling semver.satisfies */
includePrerelease?: boolean;
Expand Down
4 changes: 3 additions & 1 deletion packages/opentelemetry-instrumentation/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion packages/opentelemetry-instrumentation/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export async function safeExecuteInTheMiddleAsync<T>(
* 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' &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class FooInstrumentation extends InstrumentationBase {
}

describe('autoLoader', () => {
// eslint-disable-next-line @typescript-eslint/ban-types
let unload: Function | undefined;

afterEach(() => {
Expand Down

0 comments on commit 97234e4

Please sign in to comment.