Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ioredis): fix instrumentation of ESM-imported ioredis #1694

Merged
merged 6 commits into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ export class IORedisInstrumentation extends InstrumentationBase<any> {
new InstrumentationNodeModuleDefinition<any>(
'ioredis',
['>1', '<6'],
(moduleExports, moduleVersion?: string) => {
(module, moduleVersion?: string) => {
const moduleExports =
module[Symbol.toStringTag] === 'Module'
? module.default // ESM
: module; // CommonJS
blumamir marked this conversation as resolved.
Show resolved Hide resolved
diag.debug('Applying patch for ioredis');
if (isWrapped(moduleExports.prototype.sendCommand)) {
this._unwrap(moduleExports.prototype, 'sendCommand');
Expand All @@ -67,10 +71,14 @@ export class IORedisInstrumentation extends InstrumentationBase<any> {
'connect',
this._patchConnection()
);
return moduleExports;
return module;
},
moduleExports => {
if (moduleExports === undefined) return;
module => {
if (module === undefined) return;
const moduleExports =
module[Symbol.toStringTag] === 'Module'
? module.default // ESM
: module; // CommonJS
diag.debug('Removing patch for ioredis');
this._unwrap(moduleExports.prototype, 'sendCommand');
this._unwrap(moduleExports.prototype, 'connect');
Expand All @@ -84,17 +92,17 @@ export class IORedisInstrumentation extends InstrumentationBase<any> {
*/
private _patchSendCommand(moduleVersion?: string) {
return (original: Function) => {
return this.traceSendCommand(original, moduleVersion);
return this._traceSendCommand(original, moduleVersion);
};
}

private _patchConnection() {
return (original: Function) => {
return this.traceConnection(original);
return this._traceConnection(original);
};
}

private traceSendCommand = (original: Function, moduleVersion?: string) => {
private _traceSendCommand(original: Function, moduleVersion?: string) {
const instrumentation = this;
return function (this: RedisInterface, cmd?: IORedisCommand) {
if (arguments.length < 1 || typeof cmd !== 'object') {
Expand Down Expand Up @@ -178,9 +186,9 @@ export class IORedisInstrumentation extends InstrumentationBase<any> {
throw error;
}
};
};
}

private traceConnection = (original: Function) => {
private _traceConnection(original: Function) {
const instrumentation = this;
return function (this: RedisInterface) {
const config =
Expand Down Expand Up @@ -213,5 +221,5 @@ export class IORedisInstrumentation extends InstrumentationBase<any> {
throw error;
}
};
};
}
}
Loading