Skip to content

Commit 99431df

Browse files
blumamirpichlermarcJamieDanielson
authored
feat!(instrumentation): remove moduleExports generic type from instrumentation registration (#4598)
* feat!(instrumentation): remove moudleExports generic type from instrumentation registration * fix: lint * chore: add changelog * fix: core instrumentations * docs: update README with the change * Update experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts Co-authored-by: Marc Pichler <[email protected]> * Update experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts Co-authored-by: Marc Pichler <[email protected]> * Update CHANGELOG.md Co-authored-by: Marc Pichler <[email protected]> * chore: lint * revert: sdk-logs in tsconfig * chore: lint markdown * Apply suggestions from code review Co-authored-by: Jamie Danielson <[email protected]> * Update experimental/packages/opentelemetry-instrumentation/src/instrumentationNodeModuleFile.ts Co-authored-by: Jamie Danielson <[email protected]> * Update experimental/packages/opentelemetry-instrumentation/src/instrumentationNodeModuleDefinition.ts Co-authored-by: Jamie Danielson <[email protected]> * fix: remove unrelevant eslint ignore --------- Co-authored-by: Marc Pichler <[email protected]> Co-authored-by: Jamie Danielson <[email protected]>
1 parent 73fddf9 commit 99431df

File tree

13 files changed

+74
-75
lines changed

13 files changed

+74
-75
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/
99

1010
### :boom: Breaking Change
1111

12+
* feat!(instrumentation): remove moduleExports generic type from instrumentation registration [#4598](https://github.com/open-telemetry/opentelemetry-js/pull/4598) @blumamir
13+
* breaking for instrumentation authors that depend on
14+
* `InstrumentationBase`
15+
* `InstrumentationNodeModuleDefinition`
16+
* `InstrumentationNodeModuleFile`
17+
1218
### :rocket: (Enhancement)
1319

1420
* feat(sdk-trace-base): log resource attributes in ConsoleSpanExporter [#4605](https://github.com/open-telemetry/opentelemetry-js/pull/4605) @pichlermarc

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ export interface FetchInstrumentationConfig extends InstrumentationConfig {
7272
/**
7373
* This class represents a fetch plugin for auto instrumentation
7474
*/
75-
export class FetchInstrumentation extends InstrumentationBase<
76-
Promise<Response>
77-
> {
75+
export class FetchInstrumentation extends InstrumentationBase {
7876
readonly component: string = 'fetch';
7977
readonly version: string = VERSION;
8078
moduleName = this.component;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export class GrpcInstrumentation extends InstrumentationBase {
9898

9999
init() {
100100
return [
101-
new InstrumentationNodeModuleDefinition<any>(
101+
new InstrumentationNodeModuleDefinition(
102102
'@grpc/grpc-js',
103103
['1.*'],
104104
(moduleExports, version) => {

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
6363
/**
6464
* Http instrumentation instrumentation for Opentelemetry
6565
*/
66-
export class HttpInstrumentation extends InstrumentationBase<Http> {
66+
export class HttpInstrumentation extends InstrumentationBase {
6767
/** keep track on spans not ended */
6868
private readonly _spanNotEnded: WeakSet<Span> = new WeakSet<Span>();
6969
private _headerCapture;
@@ -104,18 +104,18 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
104104
}
105105

106106
init(): [
107-
InstrumentationNodeModuleDefinition<Https>,
108-
InstrumentationNodeModuleDefinition<Http>,
107+
InstrumentationNodeModuleDefinition,
108+
InstrumentationNodeModuleDefinition,
109109
] {
110110
return [this._getHttpsInstrumentation(), this._getHttpInstrumentation()];
111111
}
112112

113113
private _getHttpInstrumentation() {
114114
const version = process.versions.node;
115-
return new InstrumentationNodeModuleDefinition<Http>(
115+
return new InstrumentationNodeModuleDefinition(
116116
'http',
117117
['*'],
118-
moduleExports => {
118+
(moduleExports: Http): Http => {
119119
this._diag.debug(`Applying patch for http@${version}`);
120120
if (isWrapped(moduleExports.request)) {
121121
this._unwrap(moduleExports, 'request');
@@ -143,7 +143,7 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
143143
);
144144
return moduleExports;
145145
},
146-
moduleExports => {
146+
(moduleExports: Http) => {
147147
if (moduleExports === undefined) return;
148148
this._diag.debug(`Removing patch for http@${version}`);
149149

@@ -156,10 +156,10 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
156156

157157
private _getHttpsInstrumentation() {
158158
const version = process.versions.node;
159-
return new InstrumentationNodeModuleDefinition<Https>(
159+
return new InstrumentationNodeModuleDefinition(
160160
'https',
161161
['*'],
162-
moduleExports => {
162+
(moduleExports: Https): Https => {
163163
this._diag.debug(`Applying patch for https@${version}`);
164164
if (isWrapped(moduleExports.request)) {
165165
this._unwrap(moduleExports, 'request');
@@ -187,7 +187,7 @@ export class HttpInstrumentation extends InstrumentationBase<Http> {
187187
);
188188
return moduleExports;
189189
},
190-
moduleExports => {
190+
(moduleExports: Https) => {
191191
if (moduleExports === undefined) return;
192192
this._diag.debug(`Removing patch for https@${version}`);
193193

experimental/packages/opentelemetry-instrumentation-xml-http-request/src/xhr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export interface XMLHttpRequestInstrumentationConfig
8181
/**
8282
* This class represents a XMLHttpRequest plugin for auto instrumentation
8383
*/
84-
export class XMLHttpRequestInstrumentation extends InstrumentationBase<XMLHttpRequest> {
84+
export class XMLHttpRequestInstrumentation extends InstrumentationBase {
8585
readonly component: string = 'xml-http-request';
8686
readonly version: string = VERSION;
8787
moduleName = this.component;

experimental/packages/opentelemetry-instrumentation/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class MyInstrumentation extends InstrumentationBase {
3636
* the plugin should patch multiple modules or versions.
3737
*/
3838
protected init() {
39-
const module = new InstrumentationNodeModuleDefinition<typeof module_name_to_be_patched>(
39+
const module = new InstrumentationNodeModuleDefinition(
4040
'module_name_to_be_patched',
4141
['1.*'],
4242
this._onPatchMain,
@@ -63,8 +63,8 @@ export class MyInstrumentation extends InstrumentationBase {
6363
this._unwrap(moduleExports, 'mainMethodName');
6464
}
6565

66-
private _addPatchingMethod(): InstrumentationNodeModuleFile<typeof module_name_to_be_patched> {
67-
const file = new InstrumentationNodeModuleFile<typeof module_name_to_be_patched>(
66+
private _addPatchingMethod(): InstrumentationNodeModuleFile {
67+
const file = new InstrumentationNodeModuleFile(
6868
'module_name_to_be_patched/src/some_file.js',
6969
this._onPatchMethodName,
7070
this._onUnPatchMethodName,

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ import {
3535
/**
3636
* Base abstract internal class for instrumenting node and web plugins
3737
*/
38-
export abstract class InstrumentationAbstract<T = any>
39-
implements Instrumentation
40-
{
38+
export abstract class InstrumentationAbstract implements Instrumentation {
4139
protected _config: InstrumentationConfig;
4240

4341
private _tracer: Tracer;
@@ -116,7 +114,7 @@ export abstract class InstrumentationAbstract<T = any>
116114
*
117115
* @returns an array of {@link InstrumentationModuleDefinition}
118116
*/
119-
public getModuleDefinitions(): InstrumentationModuleDefinition<T>[] {
117+
public getModuleDefinitions(): InstrumentationModuleDefinition[] {
120118
const initResult = this.init() ?? [];
121119
if (!Array.isArray(initResult)) {
122120
return [initResult];
@@ -172,7 +170,7 @@ export abstract class InstrumentationAbstract<T = any>
172170
* methods.
173171
*/
174172
protected abstract init():
175-
| InstrumentationModuleDefinition<T>
176-
| InstrumentationModuleDefinition<T>[]
173+
| InstrumentationModuleDefinition
174+
| InstrumentationModuleDefinition[]
177175
| void;
178176
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@ import {
1919
InstrumentationModuleFile,
2020
} from './types';
2121

22-
export class InstrumentationNodeModuleDefinition<T>
23-
implements InstrumentationModuleDefinition<T>
22+
export class InstrumentationNodeModuleDefinition
23+
implements InstrumentationModuleDefinition
2424
{
25-
files: InstrumentationModuleFile<T>[];
25+
files: InstrumentationModuleFile[];
2626
constructor(
2727
public name: string,
2828
public supportedVersions: string[],
29-
public patch?: (exports: T, moduleVersion?: string) => T,
30-
public unpatch?: (exports: T, moduleVersion?: string) => void,
31-
files?: InstrumentationModuleFile<any>[]
29+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
30+
public patch?: (exports: any, moduleVersion?: string) => any,
31+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
32+
public unpatch?: (exports: any, moduleVersion?: string) => void,
33+
files?: InstrumentationModuleFile[]
3234
) {
3335
this.files = files || [];
3436
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@
1717
import { InstrumentationModuleFile } from './types';
1818
import { normalize } from './platform/index';
1919

20-
export class InstrumentationNodeModuleFile<T>
21-
implements InstrumentationModuleFile<T>
20+
export class InstrumentationNodeModuleFile
21+
implements InstrumentationModuleFile
2222
{
2323
public name: string;
2424
constructor(
2525
name: string,
2626
public supportedVersions: string[],
27-
public patch: (moduleExports: T, moduleVersion?: string) => T,
28-
public unpatch: (moduleExports?: T, moduleVersion?: string) => void
27+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
28+
public patch: (moduleExports: any, moduleVersion?: string) => any,
29+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
30+
public unpatch: (moduleExports?: any, moduleVersion?: string) => void
2931
) {
3032
this.name = normalize(name);
3133
}

experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ import { readFileSync } from 'fs';
3535
/**
3636
* Base abstract class for instrumenting node plugins
3737
*/
38-
export abstract class InstrumentationBase<T = any>
38+
export abstract class InstrumentationBase
3939
extends InstrumentationAbstract
4040
implements types.Instrumentation
4141
{
42-
private _modules: InstrumentationModuleDefinition<T>[];
42+
private _modules: InstrumentationModuleDefinition[];
4343
private _hooks: (Hooked | Hook)[] = [];
4444
private _requireInTheMiddleSingleton: RequireInTheMiddleSingleton =
4545
RequireInTheMiddleSingleton.getInstance();
@@ -58,7 +58,7 @@ export abstract class InstrumentationBase<T = any>
5858
modules = [modules];
5959
}
6060

61-
this._modules = (modules as InstrumentationModuleDefinition<T>[]) || [];
61+
this._modules = (modules as InstrumentationModuleDefinition[]) || [];
6262

6363
if (this._modules.length === 0) {
6464
diag.debug(
@@ -143,7 +143,7 @@ export abstract class InstrumentationBase<T = any>
143143
};
144144

145145
private _warnOnPreloadedModules(): void {
146-
this._modules.forEach((module: InstrumentationModuleDefinition<T>) => {
146+
this._modules.forEach((module: InstrumentationModuleDefinition) => {
147147
const { name } = module;
148148
try {
149149
const resolvedModule = require.resolve(name);
@@ -174,7 +174,7 @@ export abstract class InstrumentationBase<T = any>
174174
}
175175

176176
private _onRequire<T>(
177-
module: InstrumentationModuleDefinition<T>,
177+
module: InstrumentationModuleDefinition,
178178
exports: T,
179179
name: string,
180180
baseDir?: string | void
@@ -216,7 +216,8 @@ export abstract class InstrumentationBase<T = any>
216216
return supportedFileInstrumentations.reduce<T>((patchedExports, file) => {
217217
file.moduleExports = patchedExports;
218218
if (this._enabled) {
219-
return file.patch(patchedExports, module.moduleVersion);
219+
// patch signature is not typed, so we cast it assuming it's correct
220+
return file.patch(patchedExports, module.moduleVersion) as T;
220221
}
221222
return patchedExports;
222223
}, exports);
@@ -246,20 +247,10 @@ export abstract class InstrumentationBase<T = any>
246247
this._warnOnPreloadedModules();
247248
for (const module of this._modules) {
248249
const hookFn: HookFn = (exports, name, baseDir) => {
249-
return this._onRequire<typeof exports>(
250-
module as unknown as InstrumentationModuleDefinition<typeof exports>,
251-
exports,
252-
name,
253-
baseDir
254-
);
250+
return this._onRequire<typeof exports>(module, exports, name, baseDir);
255251
};
256252
const onRequire: OnRequireFn = (exports, name, baseDir) => {
257-
return this._onRequire<typeof exports>(
258-
module as unknown as InstrumentationModuleDefinition<typeof exports>,
259-
exports,
260-
name,
261-
baseDir
262-
);
253+
return this._onRequire<typeof exports>(module, exports, name, baseDir);
263254
};
264255

265256
// `RequireInTheMiddleSingleton` does not support absolute paths.

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,29 +82,30 @@ export interface ShimWrapped extends Function {
8282
__original: Function;
8383
}
8484

85-
export interface InstrumentationModuleFile<T> {
85+
export interface InstrumentationModuleFile {
8686
/** Name of file to be patched with relative path */
8787
name: string;
8888

89-
moduleExports?: T;
89+
moduleExports?: unknown;
9090

9191
/** Supported version this file */
9292
supportedVersions: string[];
9393

9494
/** Method to patch the instrumentation */
95-
patch(moduleExports: T, moduleVersion?: string): T;
95+
patch(moduleExports: unknown, moduleVersion?: string): unknown;
9696

9797
/** Method to patch the instrumentation */
9898

9999
/** Method to unpatch the instrumentation */
100-
unpatch(moduleExports?: T, moduleVersion?: string): void;
100+
unpatch(moduleExports?: unknown, moduleVersion?: string): void;
101101
}
102102

103-
export interface InstrumentationModuleDefinition<T> {
103+
export interface InstrumentationModuleDefinition {
104104
/** Module name or path */
105105
name: string;
106106

107-
moduleExports?: T;
107+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
108+
moduleExports?: any;
108109

109110
/** Instrumented module version */
110111
moduleVersion?: string;
@@ -113,15 +114,16 @@ export interface InstrumentationModuleDefinition<T> {
113114
supportedVersions: string[];
114115

115116
/** Module internal files to be patched */
116-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
117-
files: InstrumentationModuleFile<any>[];
117+
files: InstrumentationModuleFile[];
118118

119119
/** If set to true, the includePrerelease check will be included when calling semver.satisfies */
120120
includePrerelease?: boolean;
121121

122122
/** Method to patch the instrumentation */
123-
patch?: (moduleExports: T, moduleVersion?: string) => T;
123+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
124+
patch?: (moduleExports: any, moduleVersion?: string) => any;
124125

125126
/** Method to unpatch the instrumentation */
126-
unpatch?: (moduleExports: T, moduleVersion?: string) => void;
127+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
128+
unpatch?: (moduleExports: any, moduleVersion?: string) => void;
127129
}

experimental/packages/opentelemetry-instrumentation/test/common/Instrumentation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ describe('BaseInstrumentation', () => {
135135
});
136136

137137
describe('getModuleDefinitions', () => {
138-
const moduleDefinition: InstrumentationModuleDefinition<unknown> = {
138+
const moduleDefinition: InstrumentationModuleDefinition = {
139139
name: 'foo',
140140
patch: moduleExports => {},
141141
unpatch: moduleExports => {},

0 commit comments

Comments
 (0)