Skip to content

Commit 1e99187

Browse files
committed
fixup pg-pool ESM test; align POSTGRES_DB value used for local and CI 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.
1 parent f8470f1 commit 1e99187

File tree

4 files changed

+50
-25
lines changed

4 files changed

+50
-25
lines changed

packages/opentelemetry-test-utils/src/test-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const dockerRunCmds = {
4343
mysql:
4444
'docker run --rm -d --name otel-mysql -p 33306:3306 -e MYSQL_ROOT_PASSWORD=rootpw -e MYSQL_DATABASE=test_db -e MYSQL_USER=otel -e MYSQL_PASSWORD=secret mysql:5.7 --log_output=TABLE --general_log=ON',
4545
postgres:
46-
'docker run --rm -d --name otel-postgres -p 54320:5432 -e POSTGRES_PASSWORD=postgres postgres:16-alpine',
46+
'docker run --rm -d --name otel-postgres -p 54320:5432 -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=otel_pg_database postgres:16-alpine',
4747
redis: 'docker run --rm -d --name otel-redis -p 63790:6379 redis:alpine',
4848
};
4949

plugins/node/opentelemetry-instrumentation-pg/test/fixtures/use-pg-pool.mjs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { PgInstrumentation } from '../../build/src/index.js';
2626
const CONFIG = {
2727
user: process.env.POSTGRES_USER || 'postgres',
2828
password: process.env.POSTGRES_PASSWORD || 'postgres',
29-
database: process.env.POSTGRES_DB || 'postgres',
29+
database: process.env.POSTGRES_DB || 'otel_pg_database',
3030
host: process.env.POSTGRES_HOST || 'localhost',
3131
port: process.env.POSTGRES_PORT
3232
? parseInt(process.env.POSTGRES_PORT, 10)
@@ -39,20 +39,21 @@ const sdk = createTestNodeSdk({
3939
});
4040
sdk.start();
4141

42-
import pool from 'pg-pool';
43-
const pgPool = new pool(CONFIG);
42+
import Pool from 'pg-pool';
43+
const pgPool = new Pool(CONFIG);
4444

4545
const tracer = trace.getTracer();
4646

4747
await tracer.startActiveSpan('test-span', async span => {
48-
pgPool.connect((connectErr, _, release) => {
49-
assert.ifError(connectErr)
50-
pgPool.query('SELECT NOW()', (err, res) => {
51-
assert.ok(res);
52-
assert.ifError(err)
53-
});
54-
release();
48+
const client = await pgPool.connect();
49+
try {
50+
const res = await pgPool.query('SELECT NOW()');
51+
assert.ok(res);
52+
console.log('rows:', res.rows);
53+
} finally {
54+
client.release();
55+
pgPool.end();
5556
span.end();
5657
sdk.shutdown();
57-
});
58+
}
5859
});

plugins/node/opentelemetry-instrumentation-pg/test/fixtures/use-pg.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { PgInstrumentation } from '../../build/src/index.js';
2626
const CONFIG = {
2727
user: process.env.POSTGRES_USER || 'postgres',
2828
password: process.env.POSTGRES_PASSWORD || 'postgres',
29-
database: process.env.POSTGRES_DB || 'postgres',
29+
database: process.env.POSTGRES_DB || 'otel_pg_database',
3030
host: process.env.POSTGRES_HOST || 'localhost',
3131
port: process.env.POSTGRES_PORT
3232
? parseInt(process.env.POSTGRES_PORT, 10)

plugins/node/opentelemetry-instrumentation-pg/test/pg-pool.test.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,12 @@ describe('pg-pool', () => {
650650

651651
it('should not add duplicate event listeners to PgPool events', done => {
652652
const poolAux: pgPool<pg.Client> = new pgPool(CONFIG);
653+
654+
const finish = () => {
655+
poolAux.end();
656+
done();
657+
}
658+
653659
let completed = 0;
654660
poolAux.connect((err, client, release) => {
655661
if (err) {
@@ -687,7 +693,7 @@ describe('pg-pool', () => {
687693

688694
completed++;
689695
if (completed >= 2) {
690-
done();
696+
finish();
691697
}
692698
});
693699

@@ -727,7 +733,7 @@ describe('pg-pool', () => {
727733

728734
completed++;
729735
if (completed >= 2) {
730-
done();
736+
finish();
731737
}
732738
});
733739
});
@@ -808,6 +814,8 @@ describe('pg-pool', () => {
808814
1,
809815
'expected to have 1 used connection'
810816
);
817+
818+
poolAux.end();
811819
done();
812820
});
813821
});
@@ -817,6 +825,12 @@ describe('pg-pool', () => {
817825
const pool1: pgPool<pg.Client> = new pgPool(CONFIG);
818826
const pool2: pgPool<pg.Client> = new pgPool(CONFIG);
819827

828+
const finish = () => {
829+
pool1.end();
830+
pool2.end();
831+
done();
832+
}
833+
820834
let completed = 0;
821835
pool1.connect((err, client, release) => {
822836
if (err) {
@@ -862,7 +876,7 @@ describe('pg-pool', () => {
862876

863877
completed++;
864878
if (completed >= 2) {
865-
done();
879+
finish();
866880
}
867881
});
868882

@@ -910,7 +924,7 @@ describe('pg-pool', () => {
910924

911925
completed++;
912926
if (completed >= 2) {
913-
done();
927+
finish();
914928
}
915929
});
916930
});
@@ -937,14 +951,24 @@ describe('pg-pool (ESM)', () => {
937951
checkCollector: (collector: testUtils.TestCollector) => {
938952
const spans = collector.sortedSpans;
939953

940-
assert.strictEqual(spans.length, 3);
941-
942-
assert.strictEqual(spans[0].name, 'pgPool.connect');
943-
assert.strictEqual(spans[0].kind, 3);
944-
assert.strictEqual(spans[1].name, 'test-span');
945-
assert.strictEqual(spans[1].kind, 1);
946-
assert.strictEqual(spans[2].name, 'pgPool.query:SELECT NOW()');
947-
assert.strictEqual(spans[2].kind, 3);
954+
assert.strictEqual(spans.length, 6);
955+
956+
let span = spans.shift()!;
957+
assert.strictEqual(span.name, 'test-span');
958+
assert.strictEqual(span.kind, 1 /* OtlpSpanKind.INTERNAL */);
959+
const expectedRemainingSpanNames = [
960+
// I believe two sets of `*.connect` spans because pg-pool opens
961+
// two connections to start.
962+
'pg-pool.connect',
963+
'pg.connect',
964+
'pg-pool.connect',
965+
'pg.connect',
966+
'pg.query:SELECT otel_pg_database'
967+
];
968+
for (let expectedName of expectedRemainingSpanNames) {
969+
span = spans.shift()!;
970+
assert.strictEqual(span.name, expectedName);
971+
}
948972
},
949973
});
950974
});

0 commit comments

Comments
 (0)