From 2502e18e03ed8796679349a534100d743dc639e6 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 17 Oct 2023 03:59:54 -0700 Subject: [PATCH] fix(pg): fix instrumentation of ESM-imported pg (#1701) Co-authored-by: Marc Pichler --- .../src/instrumentation.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts index f47b1e81b0..8d7914c51b 100644 --- a/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-pg/src/instrumentation.ts @@ -66,7 +66,11 @@ export class PgInstrumentation extends InstrumentationBase { const modulePG = new InstrumentationNodeModuleDefinition( 'pg', ['8.*'], - moduleExports => { + (module: any) => { + const moduleExports: typeof pgTypes = + module[Symbol.toStringTag] === 'Module' + ? module.default // ESM + : module; // CommonJS if (isWrapped(moduleExports.Client.prototype.query)) { this._unwrap(moduleExports.Client.prototype, 'query'); } @@ -87,9 +91,13 @@ export class PgInstrumentation extends InstrumentationBase { this._getClientConnectPatch() as any ); - return moduleExports; + return module; }, - moduleExports => { + (module: any) => { + const moduleExports: typeof pgTypes = + module[Symbol.toStringTag] === 'Module' + ? module.default // ESM + : module; // CommonJS if (isWrapped(moduleExports.Client.prototype.query)) { this._unwrap(moduleExports.Client.prototype, 'query'); }