Skip to content

Commit

Permalink
fix(ioredis): fix instrumentation of ESM-imported ioredis
Browse files Browse the repository at this point in the history
- 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: open-telemetry#1692
  • Loading branch information
trentm committed Sep 21, 2023
1 parent 5675c49 commit 62a1456
Showing 1 changed file with 16 additions and 10 deletions.
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;
}
};
};
}
}

0 comments on commit 62a1456

Please sign in to comment.