Skip to content

Commit

Permalink
chore: remove patch and unpatch diag from instrumentations (#2107)
Browse files Browse the repository at this point in the history
* chore: remove patch and unpatch diag from instrumentations

* fix: compile

* fix: compile error connect instrumentation

* fix: compile

* fix: remove more patch and unpatch diag prints

* fix: remove unused moduleVersion argument

* fix: unused import from merge
  • Loading branch information
blumamir committed Apr 25, 2024
1 parent 73e01f2 commit f3406ea
Show file tree
Hide file tree
Showing 34 changed files with 99 additions and 330 deletions.
20 changes: 4 additions & 16 deletions plugins/node/instrumentation-cucumber/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ export class CucumberInstrumentation extends InstrumentationBase {
new InstrumentationNodeModuleDefinition(
'@cucumber/cucumber',
['^8.0.0', '^9.0.0', '^10.0.0'],
(moduleExports: Cucumber, moduleVersion) => {
this._diag.debug(
`Applying patch for @cucumber/cucumber@${moduleVersion}`
);
(moduleExports: Cucumber) => {
this.module = moduleExports;
steps.forEach(step => {
if (isWrapped(moduleExports[step])) {
Expand All @@ -72,11 +69,8 @@ export class CucumberInstrumentation extends InstrumentationBase {
});
return moduleExports;
},
(moduleExports: Cucumber, moduleVersion) => {
(moduleExports: Cucumber) => {
if (moduleExports === undefined) return;
this._diag.debug(
`Removing patch for @cucumber/cucumber@${moduleVersion}`
);
[...hooks, ...steps].forEach(method => {
this._unwrap(moduleExports, method);
});
Expand All @@ -85,10 +79,7 @@ export class CucumberInstrumentation extends InstrumentationBase {
new InstrumentationNodeModuleFile(
'@cucumber/cucumber/lib/runtime/test_case_runner.js',
['^8.0.0', '^9.0.0', '^10.0.0'],
(moduleExports, moduleVersion) => {
this._diag.debug(
`Applying patch for @cucumber/cucumber/lib/runtime/test_case_runner.js@${moduleVersion}`
);
moduleExports => {
if (isWrapped(moduleExports.default.prototype.run)) {
this._unwrap(moduleExports.default.prototype, 'run');
this._unwrap(moduleExports.default.prototype, 'runStep');
Expand All @@ -115,11 +106,8 @@ export class CucumberInstrumentation extends InstrumentationBase {
}
return moduleExports;
},
(moduleExports, moduleVersion) => {
moduleExports => {
if (moduleExports === undefined) return;
this._diag.debug(
`Removing patch for @cucumber/cucumber/lib/runtime/test_case_runner.js@${moduleVersion}`
);
this._unwrap(moduleExports.default.prototype, 'run');
this._unwrap(moduleExports.default.prototype, 'runStep');
if ('runAttempt' in moduleExports.default.prototype) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
isWrapped,
} from '@opentelemetry/instrumentation';
import {
diag,
trace,
context,
Link,
Expand Down Expand Up @@ -54,17 +53,13 @@ export class DataloaderInstrumentation extends InstrumentationBase {
new InstrumentationNodeModuleDefinition(
MODULE_NAME,
['^2.0.0'],
(dataloader, moduleVersion) => {
diag.debug(`Applying patch for ${MODULE_NAME}@${moduleVersion}`);

dataloader => {
this._patchLoad(dataloader.prototype);
this._patchLoadMany(dataloader.prototype);

return this._getPatchedConstructor(dataloader);
},
(dataloader, moduleVersion) => {
diag.debug(`Removing patch for ${MODULE_NAME}@${moduleVersion}`);

dataloader => {
if (isWrapped(dataloader.prototype.load)) {
this._unwrap(dataloader.prototype, 'load');
}
Expand Down
4 changes: 0 additions & 4 deletions plugins/node/instrumentation-fs/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export default class FsInstrumentation extends InstrumentationBase {
'fs',
['*'],
(fs: FS) => {
this._diag.debug('Applying patch for fs');
for (const fName of SYNC_FUNCTIONS) {
const { objectToPatch, functionNameToPatch } = indexFs(fs, fName);

Expand Down Expand Up @@ -113,7 +112,6 @@ export default class FsInstrumentation extends InstrumentationBase {
},
(fs: FS) => {
if (fs === undefined) return;
this._diag.debug('Removing patch for fs');
for (const fName of SYNC_FUNCTIONS) {
const { objectToPatch, functionNameToPatch } = indexFs(fs, fName);
if (isWrapped(objectToPatch[functionNameToPatch])) {
Expand All @@ -137,7 +135,6 @@ export default class FsInstrumentation extends InstrumentationBase {
'fs/promises',
['*'],
(fsPromises: FSPromises) => {
this._diag.debug('Applying patch for fs/promises');
for (const fName of PROMISE_FUNCTIONS) {
if (isWrapped(fsPromises[fName])) {
this._unwrap(fsPromises, fName);
Expand All @@ -152,7 +149,6 @@ export default class FsInstrumentation extends InstrumentationBase {
},
(fsPromises: FSPromises) => {
if (fsPromises === undefined) return;
this._diag.debug('Removing patch for fs/promises');
for (const fName of PROMISE_FUNCTIONS) {
if (isWrapped(fsPromises[fName])) {
this._unwrap(fsPromises, fName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ export default class LruMemoizerInstrumentation extends InstrumentationBase {
'lru-memoizer',
['>=1.3 <3'],
moduleExports => {
this._diag.debug('applying patch for lru-memoizer');

// moduleExports is a function which receives an options object,
// and returns a "memoizer" function upon invocation.
// We want to patch this "memoizer's" internal function
Expand Down
1 change: 0 additions & 1 deletion plugins/node/instrumentation-mongoose/src/mongoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ export class MongooseInstrumentation extends InstrumentationBase {
}

private unpatch(moduleExports: typeof mongoose): void {
this._diag.debug('mongoose instrumentation: unpatch mongoose');
this._unwrap(moduleExports.Model.prototype, 'save');
// revert the patch for $save which we applied by aliasing it to patched `save`
moduleExports.Model.prototype.$save = moduleExports.Model.prototype.save;
Expand Down
19 changes: 1 addition & 18 deletions plugins/node/instrumentation-socket.io/src/socket.io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export class SocketIoInstrumentation extends InstrumentationBase {
if (moduleVersion === undefined) {
return moduleExports;
}
this._diag.debug(`applying patch to socket.io@${moduleVersion} Socket`);
if (isWrapped(moduleExports?.Socket?.prototype?.on)) {
this._unwrap(moduleExports.Socket.prototype, 'on');
}
Expand Down Expand Up @@ -112,9 +111,6 @@ export class SocketIoInstrumentation extends InstrumentationBase {
if (moduleVersion === undefined) {
return moduleExports;
}
this._diag.debug(
`applying patch to socket.io@${moduleVersion} StrictEventEmitter`
);
if (isWrapped(moduleExports?.BroadcastOperator?.prototype?.emit)) {
this._unwrap(moduleExports.BroadcastOperator.prototype, 'emit');
}
Expand Down Expand Up @@ -142,9 +138,6 @@ export class SocketIoInstrumentation extends InstrumentationBase {
if (moduleVersion === undefined) {
return moduleExports;
}
this._diag.debug(
`applying patch to socket.io@${moduleVersion} Namespace`
);
if (isWrapped(moduleExports?.Namespace?.prototype?.emit)) {
this._unwrap(moduleExports.Namespace.prototype, 'emit');
}
Expand All @@ -171,7 +164,6 @@ export class SocketIoInstrumentation extends InstrumentationBase {
if (moduleVersion === undefined) {
return moduleExports;
}
this._diag.debug(`applying patch to socket.io@${moduleVersion} Socket`);
if (isWrapped(moduleExports.prototype?.on)) {
this._unwrap(moduleExports.prototype, 'on');
}
Expand Down Expand Up @@ -206,9 +198,6 @@ export class SocketIoInstrumentation extends InstrumentationBase {
if (moduleVersion === undefined) {
return moduleExports;
}
this._diag.debug(
`applying patch to socket.io@${moduleVersion} Namespace`
);
if (isWrapped(moduleExports?.prototype?.emit)) {
this._unwrap(moduleExports.prototype, 'emit');
}
Expand Down Expand Up @@ -237,9 +226,6 @@ export class SocketIoInstrumentation extends InstrumentationBase {
if (moduleVersion === undefined) {
return moduleExports;
}
this._diag.debug(
`applying patch to socket.io@${moduleVersion} Server`
);
if (isWrapped(moduleExports?.Server?.prototype?.on)) {
this._unwrap(moduleExports.Server.prototype, 'on');
}
Expand All @@ -250,7 +236,7 @@ export class SocketIoInstrumentation extends InstrumentationBase {
);
return moduleExports;
},
(moduleExports, moduleVersion) => {
moduleExports => {
if (isWrapped(moduleExports?.Server?.prototype?.on)) {
this._unwrap(moduleExports.Server.prototype, 'on');
}
Expand All @@ -272,9 +258,6 @@ export class SocketIoInstrumentation extends InstrumentationBase {
if (moduleVersion === undefined) {
return moduleExports;
}
this._diag.debug(
`applying patch to socket.io@${moduleVersion} Server`
);
if (isWrapped(moduleExports?.prototype?.on)) {
this._unwrap(moduleExports.prototype, 'on');
}
Expand Down
4 changes: 1 addition & 3 deletions plugins/node/instrumentation-tedious/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ export class TediousInstrumentation extends InstrumentationBase {
new InstrumentationNodeModuleDefinition(
TediousInstrumentation.COMPONENT,
['>=1.11.0 <=15'],
(moduleExports: typeof tedious, moduleVersion) => {
this._diag.debug(`Patching tedious@${moduleVersion}`);

(moduleExports: typeof tedious) => {
const ConnectionPrototype: any = moduleExports.Connection.prototype;
for (const method of PATCHED_METHODS) {
if (isWrapped(ConnectionPrototype[method])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ export class AwsLambdaInstrumentation extends InstrumentationBase {
module,
['*'],
(moduleExports: LambdaModule) => {
diag.debug('Applying patch for lambda handler');
if (isWrapped(moduleExports[functionName])) {
this._unwrap(moduleExports, functionName);
}
Expand All @@ -160,7 +159,6 @@ export class AwsLambdaInstrumentation extends InstrumentationBase {
},
(moduleExports?: LambdaModule) => {
if (moduleExports == null) return;
diag.debug('Removing patch for lambda handler');
this._unwrap(moduleExports, functionName);
}
),
Expand Down
15 changes: 0 additions & 15 deletions plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,6 @@ export class AwsInstrumentation extends InstrumentationBase {
}

protected patchV3ConstructStack(moduleExports: any, moduleVersion?: string) {
diag.debug(
'aws-sdk instrumentation: applying patch to aws-sdk v3 constructStack'
);
this._wrap(
moduleExports,
'constructStack',
Expand All @@ -186,17 +183,11 @@ export class AwsInstrumentation extends InstrumentationBase {
}

protected unpatchV3ConstructStack(moduleExports: any) {
diag.debug(
'aws-sdk instrumentation: applying unpatch to aws-sdk v3 constructStack'
);
this._unwrap(moduleExports, 'constructStack');
return moduleExports;
}

protected patchV3SmithyClient(moduleExports: any) {
diag.debug(
'aws-sdk instrumentation: applying patch to aws-sdk v3 client send'
);
this._wrap(
moduleExports.Client.prototype,
'send',
Expand All @@ -206,17 +197,11 @@ export class AwsInstrumentation extends InstrumentationBase {
}

protected unpatchV3SmithyClient(moduleExports: any) {
diag.debug(
'aws-sdk instrumentation: applying patch to aws-sdk v3 constructStack'
);
this._unwrap(moduleExports.Client.prototype, 'send');
return moduleExports;
}

protected patchV2(moduleExports: any, moduleVersion?: string) {
diag.debug(
`aws-sdk instrumentation: applying patch to ${AwsInstrumentation.component}`
);
this.unpatchV2(moduleExports);
this._wrap(
moduleExports?.Request.prototype,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ export class BunyanInstrumentation extends InstrumentationBase {
new InstrumentationNodeModuleDefinition(
'bunyan',
['<2.0'],
(module: any, moduleVersion) => {
this._diag.debug(`Applying patch for bunyan@${moduleVersion}`);
(module: any) => {
const instrumentation = this;
const Logger =
module[Symbol.toStringTag] === 'Module'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ export class CassandraDriverInstrumentation extends InstrumentationBase {
return new InstrumentationNodeModuleDefinition(
'cassandra-driver',
supportedVersions,
(driverModule, moduleVersion) => {
this._diag.debug(
`Applying patch for cassandra-driver@${moduleVersion}`
);
driverModule => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Client = driverModule.Client.prototype as any;

Expand All @@ -76,10 +73,7 @@ export class CassandraDriverInstrumentation extends InstrumentationBase {

return driverModule;
},
(driverModule, moduleVersion) => {
this._diag.debug(
`Removing patch for cassandra-driver@${moduleVersion}`
);
driverModule => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Client = driverModule.Client.prototype as any;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { context, diag, Span, SpanOptions } from '@opentelemetry/api';
import { context, Span, SpanOptions } from '@opentelemetry/api';
import { getRPCMetadata, RPCType } from '@opentelemetry/core';
import type { HandleFunction, NextFunction, Server } from 'connect';
import type { ServerResponse } from 'http';
Expand Down Expand Up @@ -55,12 +55,8 @@ export class ConnectInstrumentation extends InstrumentationBase {
new InstrumentationNodeModuleDefinition(
'connect',
['^3.0.0'],
(moduleExports, moduleVersion) => {
diag.debug(`Applying patch for connect@${moduleVersion}`);
moduleExports => {
return this._patchConstructor(moduleExports);
},
(moduleExports, moduleVersion) => {
diag.debug(`Removing patch for connect@${moduleVersion}`);
}
),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class DnsInstrumentation extends InstrumentationBase {
'dns',
['*'],
(moduleExports: typeof dns) => {
diag.debug('Applying patch for dns');
if (isWrapped(moduleExports.lookup)) {
this._unwrap(moduleExports, 'lookup');
}
Expand All @@ -66,7 +65,6 @@ export class DnsInstrumentation extends InstrumentationBase {
},
moduleExports => {
if (moduleExports === undefined) return;
diag.debug('Removing patch for dns');
this._unwrap(moduleExports, 'lookup');
this._unwrap(moduleExports.promises, 'lookup');
}
Expand All @@ -75,7 +73,6 @@ export class DnsInstrumentation extends InstrumentationBase {
'dns/promises',
['*'],
(moduleExports: typeof dnsPromises) => {
diag.debug('Applying patch for dns/promises');
if (isWrapped(moduleExports.lookup)) {
this._unwrap(moduleExports, 'lookup');
}
Expand All @@ -85,7 +82,6 @@ export class DnsInstrumentation extends InstrumentationBase {
},
moduleExports => {
if (moduleExports === undefined) return;
diag.debug('Removing patch for dns/promises');
this._unwrap(moduleExports, 'lookup');
}
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ export class ExpressInstrumentation extends InstrumentationBase {
new InstrumentationNodeModuleDefinition(
'express',
['^4.0.0'],
(moduleExports, moduleVersion) => {
diag.debug(`Applying patch for express@${moduleVersion}`);
moduleExports => {
const routerProto = moduleExports.Router as unknown as express.Router;
// patch express.Router.route
if (isWrapped(routerProto.route)) {
Expand All @@ -97,9 +96,8 @@ export class ExpressInstrumentation extends InstrumentationBase {
);
return moduleExports;
},
(moduleExports, moduleVersion) => {
moduleExports => {
if (moduleExports === undefined) return;
diag.debug(`Removing patch for express@${moduleVersion}`);
const routerProto = moduleExports.Router as unknown as express.Router;
this._unwrap(routerProto, 'route');
this._unwrap(routerProto, 'use');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ export class FastifyInstrumentation extends InstrumentationBase {
new InstrumentationNodeModuleDefinition(
'fastify',
['^3.0.0', '^4.0.0'],
(moduleExports, moduleVersion) => {
this._diag.debug(`Applying patch for fastify@${moduleVersion}`);
moduleExports => {
return this._patchConstructor(moduleExports);
}
),
Expand Down
Loading

0 comments on commit f3406ea

Please sign in to comment.