Skip to content

fix(pg): fix instrumentation of ESM-imported pg-pool #2807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

maryliag
Copy link
Contributor

The ESM imported top-level object is a Module Namespace Object per ECMA262 spec that is, apparently, incompatible with the 'PG' class object that 'pg' returns. The ".defaults" holds the equivalent of require('pg').

Fixes: #2759

Copy link

codecov bot commented Apr 29, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.55%. Comparing base (32f41ee) to head (d00650e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2807      +/-   ##
==========================================
+ Coverage   89.50%   89.55%   +0.04%     
==========================================
  Files         180      180              
  Lines        8719     8722       +3     
  Branches     1767     1767              
==========================================
+ Hits         7804     7811       +7     
+ Misses        915      911       -4     
Files with missing lines Coverage Δ
...elemetry-instrumentation-pg/src/instrumentation.ts 95.04% <100.00%> (+0.07%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pichlermarc
Copy link
Member

Thanks for fixing this 🙂
Could you also add a test akin to the below one for pg-pool to avoid regressions?

describe('pg (ESM)', () => {
it('should work with ESM usage', async () => {
await testUtils.runTestFixture({
cwd: __dirname,
argv: ['fixtures/use-pg.mjs'],
env: {
NODE_OPTIONS:
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
NODE_NO_WARNINGS: '1',
},
checkResult: (err, stdout, stderr) => {
assert.ifError(err);
},
checkCollector: (collector: testUtils.TestCollector) => {
const spans = collector.sortedSpans;
assert.strictEqual(spans.length, 3);
assert.strictEqual(spans[0].name, 'pg.connect');
assert.strictEqual(spans[0].kind, 3);
assert.strictEqual(spans[1].name, 'test-span');
assert.strictEqual(spans[1].kind, 1);
assert.strictEqual(spans[2].name, 'pg.query:SELECT otel_pg_database');
assert.strictEqual(spans[2].kind, 3);
},
});
});
});

Doing so would also check the box in #1942

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pg: cannot instrument an ESM-imported pg-pool
2 participants