Skip to content

Commit 8136008

Browse files
committed
add test
1 parent d00650e commit 8136008

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// Use postgres from an ES module:
18+
// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs use-pg.mjs
19+
20+
import { trace } from '@opentelemetry/api';
21+
import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils';
22+
import assert from 'assert';
23+
24+
import { PgInstrumentation } from '../../build/src/index.js';
25+
26+
const CONFIG = {
27+
user: process.env.POSTGRES_USER || 'postgres',
28+
password: process.env.POSTGRES_PASSWORD || 'postgres',
29+
database: process.env.POSTGRES_DB || 'postgres',
30+
host: process.env.POSTGRES_HOST || 'localhost',
31+
port: process.env.POSTGRES_PORT
32+
? parseInt(process.env.POSTGRES_PORT, 10)
33+
: 54320,
34+
};
35+
36+
const sdk = createTestNodeSdk({
37+
serviceName: 'use-pg-pool',
38+
instrumentations: [new PgInstrumentation()],
39+
});
40+
sdk.start();
41+
42+
import { Pool as PGPool } from 'pg';
43+
const pgPool = new PGPool(CONFIG);
44+
const tracer = trace.getTracer();
45+
46+
await tracer.startActiveSpan('test-span', async span => {
47+
pgPool.connect((connectErr, _, release) => {
48+
assert.ifError(connectErr)
49+
pgPool.query('SELECT NOW()', (err, res) => {
50+
assert.ok(res);
51+
assert.ifError(err)
52+
});
53+
release();
54+
span.end();
55+
sdk.shutdown();
56+
});
57+
});

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,3 +916,32 @@ describe('pg-pool', () => {
916916
});
917917
});
918918
});
919+
920+
describe('pg-pool (ESM)', () => {
921+
it('should work with ESM usage', async () => {
922+
await testUtils.runTestFixture({
923+
cwd: __dirname,
924+
argv: ['fixtures/use-pg-pool.mjs'],
925+
env: {
926+
NODE_OPTIONS:
927+
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
928+
NODE_NO_WARNINGS: '1',
929+
},
930+
checkResult: (err, stdout, stderr) => {
931+
assert.ifError(err);
932+
},
933+
checkCollector: (collector: testUtils.TestCollector) => {
934+
const spans = collector.sortedSpans;
935+
936+
assert.strictEqual(spans.length, 3);
937+
938+
assert.strictEqual(spans[0].name, 'pgPool.connect');
939+
assert.strictEqual(spans[0].kind, 3);
940+
assert.strictEqual(spans[1].name, 'test-span');
941+
assert.strictEqual(spans[1].kind, 1);
942+
assert.strictEqual(spans[2].name, 'pgPool.query:SELECT NOW()');
943+
assert.strictEqual(spans[2].kind, 3);
944+
},
945+
});
946+
});
947+
});

0 commit comments

Comments
 (0)