Skip to content

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

@codecov
Copy link

codecov bot commented Apr 29, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.67%. Comparing base (e8e3cbd) to head (8edfd00).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2807   +/-   ##
=======================================
  Coverage   89.66%   89.67%           
=======================================
  Files         184      184           
  Lines        8943     8946    +3     
  Branches     1834     1834           
=======================================
+ Hits         8019     8022    +3     
  Misses        924      924           
Files with missing lines Coverage Δ
...elemetry-instrumentation-pg/src/instrumentation.ts 95.04% <100.00%> (+0.07%) ⬆️
🚀 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

Copy link
Contributor

@trentm trentm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

If you are super-bored sometime ;) a test for this (similar to the runTestFixture usage in test/pg.test.ts) would be great. Then we could check off instrumentation-pg in #1942 (comment)

@trentm
Copy link
Contributor

trentm commented May 2, 2025

Oh, duh, Marc said the exact same thing already.

@maryliag
Copy link
Contributor Author

maryliag commented May 2, 2025

Yes, I will be adding the test shortly :D

maryliag and others added 5 commits May 6, 2025 11:30
… testing of instrumentation-pg

This also fixes a couple missing 'pool.end()' calls in pg-pool.test.ts that would
result in a 10s hang at the end of testing.
Copy link
Member

@pichlermarc pichlermarc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for fixing this 🙂

…e-pg-pool.mjs

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
@trentm trentm merged commit f6bc4cc into open-telemetry:main May 7, 2025
9 of 10 checks passed
@dyladan dyladan mentioned this pull request May 7, 2025
@maryliag maryliag deleted the fix-pg-pool branch May 9, 2025 19:25
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

6 participants