Skip to content

Commit

Permalink
fix(ioredis): fix instrumentation of ESM-imported ioredis (#1694)
Browse files Browse the repository at this point in the history
Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
  • Loading branch information
trentm and pichlermarc committed Oct 17, 2023
1 parent 659d7ba commit 7b457cd
Showing 1 changed file with 18 additions and 10 deletions.
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
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;
}
};
};
}
}

0 comments on commit 7b457cd

Please sign in to comment.