From 7b457cdbcf322a31bbf9dba1412ba49b2da587eb Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 17 Oct 2023 01:05:44 -0700 Subject: [PATCH] fix(ioredis): fix instrumentation of ESM-imported ioredis (#1694) Co-authored-by: Marc Pichler --- .../src/instrumentation.ts | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-ioredis/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-ioredis/src/instrumentation.ts index 94f8b850ed..0753518de3 100644 --- a/plugins/node/opentelemetry-instrumentation-ioredis/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-ioredis/src/instrumentation.ts @@ -49,7 +49,11 @@ export class IORedisInstrumentation extends InstrumentationBase { new InstrumentationNodeModuleDefinition( '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'); @@ -67,10 +71,14 @@ export class IORedisInstrumentation extends InstrumentationBase { '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'); @@ -84,17 +92,17 @@ export class IORedisInstrumentation extends InstrumentationBase { */ 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') { @@ -178,9 +186,9 @@ export class IORedisInstrumentation extends InstrumentationBase { throw error; } }; - }; + } - private traceConnection = (original: Function) => { + private _traceConnection(original: Function) { const instrumentation = this; return function (this: RedisInterface) { const config = @@ -213,5 +221,5 @@ export class IORedisInstrumentation extends InstrumentationBase { throw error; } }; - }; + } }