Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf

protected init() {
const SUPPORTED_PG_VERSIONS = ['>=8.0.3 <9'];
const SUPPORTED_PG_POOL_VERSIONS = ['>=2.0.0 <4'];

const modulePgNativeClient = new InstrumentationNodeModuleFile(
'pg/lib/native/client.js',
Expand Down Expand Up @@ -168,8 +169,9 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf

const modulePGPool = new InstrumentationNodeModuleDefinition(
'pg-pool',
['>=2.0.0 <4'],
(moduleExports: typeof pgPoolTypes) => {
SUPPORTED_PG_POOL_VERSIONS,
(module: any) => {
const moduleExports = extractModuleExports(module);
if (isWrapped(moduleExports.prototype.connect)) {
this._unwrap(moduleExports.prototype, 'connect');
}
Expand All @@ -180,7 +182,8 @@ export class PgInstrumentation extends InstrumentationBase<PgInstrumentationConf
);
return moduleExports;
},
(moduleExports: typeof pgPoolTypes) => {
(module: any) => {
const moduleExports = extractModuleExports(module);
if (isWrapped(moduleExports.prototype.connect)) {
this._unwrap(moduleExports.prototype, 'connect');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Use postgres from an ES module:
// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs use-pg.mjs

import { trace } from '@opentelemetry/api';
import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils';
import assert from 'assert';

import { PgInstrumentation } from '../../build/src/index.js';

const CONFIG = {
user: process.env.POSTGRES_USER || 'postgres',
password: process.env.POSTGRES_PASSWORD || 'postgres',
database: process.env.POSTGRES_DB || 'postgres',
host: process.env.POSTGRES_HOST || 'localhost',
port: process.env.POSTGRES_PORT
? parseInt(process.env.POSTGRES_PORT, 10)
: 54320,
};

const sdk = createTestNodeSdk({
serviceName: 'use-pg-pool',
instrumentations: [new PgInstrumentation()],
});
sdk.start();

import { Pool as PGPool } from 'pg';
const pgPool = new PGPool(CONFIG);
const tracer = trace.getTracer();

await tracer.startActiveSpan('test-span', async span => {
pgPool.connect((connectErr, _, release) => {
assert.ifError(connectErr)
pgPool.query('SELECT NOW()', (err, res) => {
assert.ok(res);
assert.ifError(err)
});
release();
span.end();
sdk.shutdown();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -916,3 +916,32 @@ describe('pg-pool', () => {
});
});
});

describe('pg-pool (ESM)', () => {
it('should work with ESM usage', async () => {
await testUtils.runTestFixture({
cwd: __dirname,
argv: ['fixtures/use-pg-pool.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, 'pgPool.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, 'pgPool.query:SELECT NOW()');
assert.strictEqual(spans[2].kind, 3);
},
});
});
});
Loading