Skip to content
Merged
Changes from 1 commit
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
Next Next commit
fix(ioredis): fix instrumentation of ESM-imported ioredis
- With an ESM import the top-level object is a Module Namespace Object
  (https://tc39.es/ecma262/#sec-module-namespace-objects) that has no
  prototype. For compat, Node.js assigns the usual CommonJS
  module.exports to `<module>.default`, so use that when this is
  an ESM module.
- Also, TypeScript translates class properties to assignments in the
  constructor after the super() call. Because super() can call init()
  and enable() synchronously, it calls back into
  'IORedisInstrumentation' before 'traceSendCommand' was defined.
  Defining it as a method fixes that issue.

Fixes: #1692
  • Loading branch information
trentm committed Sep 21, 2023
commit 62a14569380ed7e734ce155e840ec2152d78c273
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ 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
diag.debug('Applying patch for ioredis');
if (isWrapped(moduleExports.prototype.sendCommand)) {
this._unwrap(moduleExports.prototype, 'sendCommand');
Expand All @@ -67,10 +70,13 @@ 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 +90,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 +184,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 +219,5 @@ export class IORedisInstrumentation extends InstrumentationBase<any> {
throw error;
}
};
};
}
}