diff --git a/packages/opentelemetry-instrumentation-fetch/src/fetch.ts b/packages/opentelemetry-instrumentation-fetch/src/fetch.ts index 5d107027d7..eefcdf5d08 100644 --- a/packages/opentelemetry-instrumentation-fetch/src/fetch.ts +++ b/packages/opentelemetry-instrumentation-fetch/src/fetch.ts @@ -67,9 +67,7 @@ export interface FetchInstrumentationConfig extends InstrumentationConfig { /** * This class represents a fetch plugin for auto instrumentation */ -export class FetchInstrumentation extends InstrumentationBase< - Promise -> { +export class FetchInstrumentation extends InstrumentationBase { readonly component: string = 'fetch'; readonly version: string = VERSION; moduleName = this.component; @@ -84,7 +82,7 @@ export class FetchInstrumentation extends InstrumentationBase< ); } - init() {} + protected init() {} private _getConfig(): FetchInstrumentationConfig { return this._config; diff --git a/packages/opentelemetry-instrumentation-grpc/src/grpc-js/index.ts b/packages/opentelemetry-instrumentation-grpc/src/grpc-js/index.ts index 0cf2114891..d55ba11d22 100644 --- a/packages/opentelemetry-instrumentation-grpc/src/grpc-js/index.ts +++ b/packages/opentelemetry-instrumentation-grpc/src/grpc-js/index.ts @@ -69,7 +69,8 @@ export class GrpcJsInstrumentation extends InstrumentationBase { this._config = Object.assign({}, config); } - init() { + // use InstrumentationNodeModuleDefinition[] to avoid leaking grpc types + protected init(): InstrumentationNodeModuleDefinition[] { return [ new InstrumentationNodeModuleDefinition( '@grpc/grpc-js', diff --git a/packages/opentelemetry-instrumentation-grpc/src/grpc/index.ts b/packages/opentelemetry-instrumentation-grpc/src/grpc/index.ts index 87486ef157..3be586b8bb 100644 --- a/packages/opentelemetry-instrumentation-grpc/src/grpc/index.ts +++ b/packages/opentelemetry-instrumentation-grpc/src/grpc/index.ts @@ -51,9 +51,7 @@ import { AttributeNames } from '../enums/AttributeNames'; */ let grpcClient: typeof grpcTypes; -export class GrpcNativeInstrumentation extends InstrumentationBase< - typeof grpcTypes -> { +export class GrpcNativeInstrumentation extends InstrumentationBase { constructor( protected override _config: GrpcInstrumentationConfig & InstrumentationConfig = {}, name: string, @@ -68,7 +66,8 @@ export class GrpcNativeInstrumentation extends InstrumentationBase< this._config = Object.assign({}, config); } - init() { + // use InstrumentationNodeModuleDefinition[] to avoid leaking grpc types + protected init(): InstrumentationNodeModuleDefinition[] { return [ new InstrumentationNodeModuleDefinition( 'grpc', diff --git a/packages/opentelemetry-instrumentation-http/src/http.ts b/packages/opentelemetry-instrumentation-http/src/http.ts index 4b52635eef..c6737dc6d8 100644 --- a/packages/opentelemetry-instrumentation-http/src/http.ts +++ b/packages/opentelemetry-instrumentation-http/src/http.ts @@ -28,7 +28,7 @@ import { import { suppressTracing } from '@opentelemetry/core'; import type * as http from 'http'; import type * as https from 'https'; -import { Socket } from 'net'; +import type { Socket } from 'net'; import * as semver from 'semver'; import * as url from 'url'; import { @@ -55,7 +55,7 @@ import { RPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core'; /** * Http instrumentation instrumentation for Opentelemetry */ -export class HttpInstrumentation extends InstrumentationBase { +export class HttpInstrumentation extends InstrumentationBase { /** keep track on spans not ended */ private readonly _spanNotEnded: WeakSet = new WeakSet(); private readonly _version = process.versions.node; @@ -76,7 +76,8 @@ export class HttpInstrumentation extends InstrumentationBase { this._config = Object.assign({}, config); } - init() { + // use InstrumentationNodeModuleDefinition[] here to avoid leaking http types + protected init(): InstrumentationNodeModuleDefinition[] { return [this._getHttpsInstrumentation(), this._getHttpInstrumentation()]; } @@ -169,7 +170,7 @@ export class HttpInstrumentation extends InstrumentationBase { /** * Creates spans for incoming requests, restoring spans' context if applied. */ - protected _getPatchIncomingRequestFunction(component: 'http' | 'https') { + private _getPatchIncomingRequestFunction(component: 'http' | 'https') { return (original: (event: string, ...args: unknown[]) => boolean) => { return this._incomingRequestFunction(component, original); }; @@ -179,13 +180,13 @@ export class HttpInstrumentation extends InstrumentationBase { * Creates spans for outgoing requests, sending spans' context for distributed * tracing. */ - protected _getPatchOutgoingRequestFunction(component: 'http' | 'https') { + private _getPatchOutgoingRequestFunction(component: 'http' | 'https') { return (original: Func): Func => { return this._outgoingRequestFunction(component, original); }; } - protected _getPatchOutgoingGetFunction( + private _getPatchOutgoingGetFunction( clientRequest: ( options: http.RequestOptions | string | url.URL, ...args: HttpRequestArgs diff --git a/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts b/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts index eabab83d1c..98458b0e80 100644 --- a/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts +++ b/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts @@ -78,7 +78,7 @@ export interface XMLHttpRequestInstrumentationConfig /** * This class represents a XMLHttpRequest plugin for auto instrumentation */ -export class XMLHttpRequestInstrumentation extends InstrumentationBase { +export class XMLHttpRequestInstrumentation extends InstrumentationBase { readonly component: string = 'xml-http-request'; readonly version: string = VERSION; moduleName = this.component; @@ -97,7 +97,7 @@ export class XMLHttpRequestInstrumentation extends InstrumentationBase - implements types.Instrumentation { +export abstract class InstrumentationAbstract implements types.Instrumentation { protected _config: types.InstrumentationConfig; private _tracer: Tracer; @@ -121,7 +120,7 @@ export abstract class InstrumentationAbstract * methods */ protected abstract init(): - | InstrumentationModuleDefinition - | InstrumentationModuleDefinition[] + | InstrumentationModuleDefinition + | InstrumentationModuleDefinition[] | void; } diff --git a/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts b/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts index f50cff4252..fe039c3de4 100644 --- a/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts +++ b/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts @@ -25,10 +25,9 @@ import { diag } from '@opentelemetry/api'; /** * Base abstract class for instrumenting node plugins */ -export abstract class InstrumentationBase - extends InstrumentationAbstract +export abstract class InstrumentationBase extends InstrumentationAbstract implements types.Instrumentation { - private _modules: InstrumentationModuleDefinition[]; + private _modules: InstrumentationModuleDefinition[]; private _hooks: RequireInTheMiddle.Hooked[] = []; private _enabled = false; @@ -45,7 +44,7 @@ export abstract class InstrumentationBase modules = [modules]; } - this._modules = (modules as InstrumentationModuleDefinition[]) || []; + this._modules = (modules as InstrumentationModuleDefinition[]) || []; if (this._modules.length === 0) { diag.warn( @@ -131,9 +130,7 @@ export abstract class InstrumentationBase { internals: true }, (exports, name, baseDir) => { return this._onRequire( - (module as unknown) as InstrumentationModuleDefinition< - typeof exports - >, + module as InstrumentationModuleDefinition, exports, name, baseDir