Skip to content

Commit

Permalink
fix(ioredis): apply ESM check fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc committed Jan 23, 2024
1 parent 3a07390 commit cae8ee0
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ const DEFAULT_CONFIG: IORedisInstrumentationConfig = {
requireParentSpan: true,
};

function getModuleExports(module: any): any {
// The Symbol.toStringTag module check is unreliable starting from import-in-the-middle@1.7.2
// see https://github.com/DataDog/import-in-the-middle/issues/57 - once fixed this may start working again.
if (
module[Symbol.toStringTag] === 'Module' ||
(module.prototype == null && module.default.prototype != null)
) {
return module.default;

Check warning on line 45 in plugins/node/opentelemetry-instrumentation-ioredis/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-ioredis/src/instrumentation.ts#L45

Added line #L45 was not covered by tests
} else {
return module;
}
}

export class IORedisInstrumentation extends InstrumentationBase<any> {
constructor(_config: IORedisInstrumentationConfig = {}) {
super(
Expand All @@ -50,10 +63,7 @@ export class IORedisInstrumentation extends InstrumentationBase<any> {
'ioredis',
['>1', '<6'],
(module, moduleVersion?: string) => {
const moduleExports =
module[Symbol.toStringTag] === 'Module'
? module.default // ESM
: module; // CommonJS
const moduleExports = getModuleExports(module);
diag.debug('Applying patch for ioredis');
if (isWrapped(moduleExports.prototype.sendCommand)) {
this._unwrap(moduleExports.prototype, 'sendCommand');
Expand All @@ -75,10 +85,7 @@ export class IORedisInstrumentation extends InstrumentationBase<any> {
},
module => {
if (module === undefined) return;
const moduleExports =
module[Symbol.toStringTag] === 'Module'
? module.default // ESM
: module; // CommonJS
const moduleExports = getModuleExports(module);
diag.debug('Removing patch for ioredis');
this._unwrap(moduleExports.prototype, 'sendCommand');
this._unwrap(moduleExports.prototype, 'connect');
Expand Down

0 comments on commit cae8ee0

Please sign in to comment.