Skip to content

Commit e01a428

Browse files
test(node): Add pg-native integration tests (#15206)
Co-authored-by: Abhijeet Prasad <[email protected]>
1 parent 9789f32 commit e01a428

File tree

5 files changed

+208
-42
lines changed

5 files changed

+208
-42
lines changed

.github/workflows/build.yml

+4
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,10 @@ jobs:
713713
with:
714714
dependency_cache_key: ${{ needs.job_build.outputs.dependency_cache_key }}
715715

716+
- name: Build `libpq`
717+
run: yarn libpq:build
718+
working-directory: dev-packages/node-integration-tests
719+
716720
- name: Overwrite typescript version
717721
if: matrix.typescript == '3.8'
718722
run: node ./scripts/use-ts-3_8.js

dev-packages/node-integration-tests/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"build:types": "tsc -p tsconfig.types.json",
1717
"clean": "rimraf -g **/node_modules && run-p clean:script",
1818
"clean:script": "node scripts/clean.js",
19+
"libpq:build": "npm rebuild libpq",
1920
"express-v5-install": "cd suites/express-v5 && yarn --no-lockfile",
2021
"lint": "eslint . --format stylish",
2122
"fix": "eslint . --format stylish --fix",
@@ -62,7 +63,8 @@
6263
"nock": "^13.5.5",
6364
"node-cron": "^3.0.3",
6465
"node-schedule": "^2.1.1",
65-
"pg": "^8.7.3",
66+
"pg": "^8.13.1",
67+
"pg-native": "3.2.0",
6668
"proxy": "^2.1.1",
6769
"redis-4": "npm:redis@^4.6.14",
6870
"reflect-metadata": "0.2.1",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
2+
const Sentry = require('@sentry/node');
3+
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
release: '1.0',
7+
tracesSampleRate: 1.0,
8+
transport: loggingTransport,
9+
});
10+
11+
// Stop the process from exiting before the transaction is sent
12+
setInterval(() => {}, 1000);
13+
14+
const { native } = require('pg');
15+
const { Client } = native;
16+
17+
const client = new Client({ port: 5444, user: 'test', password: 'test', database: 'tests' });
18+
19+
async function run() {
20+
await Sentry.startSpan(
21+
{
22+
name: 'Test Transaction',
23+
op: 'transaction',
24+
},
25+
async () => {
26+
try {
27+
await client.connect();
28+
29+
await client
30+
.query(
31+
'CREATE TABLE "NativeUser" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id"));',
32+
)
33+
.catch(() => {
34+
// if this is not a fresh database, the table might already exist
35+
});
36+
37+
await client.query('INSERT INTO "NativeUser" ("email", "name") VALUES ($1, $2)', ['tim', '[email protected]']);
38+
await client.query('SELECT * FROM "NativeUser"');
39+
} finally {
40+
await client.end();
41+
}
42+
},
43+
);
44+
}
45+
46+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
47+
run();

dev-packages/node-integration-tests/suites/tracing/postgres/test.ts

+50
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,54 @@ describe('postgres auto instrumentation', () => {
5353
.expect({ transaction: EXPECTED_TRANSACTION })
5454
.start(done);
5555
});
56+
57+
test('should auto-instrument `pg-native` package', done => {
58+
const EXPECTED_TRANSACTION = {
59+
transaction: 'Test Transaction',
60+
spans: expect.arrayContaining([
61+
expect.objectContaining({
62+
data: expect.objectContaining({
63+
'db.system': 'postgresql',
64+
'db.name': 'tests',
65+
'sentry.origin': 'manual',
66+
'sentry.op': 'db',
67+
}),
68+
description: 'pg.connect',
69+
op: 'db',
70+
status: 'ok',
71+
}),
72+
expect.objectContaining({
73+
data: expect.objectContaining({
74+
'db.system': 'postgresql',
75+
'db.name': 'tests',
76+
'db.statement': 'INSERT INTO "NativeUser" ("email", "name") VALUES ($1, $2)',
77+
'sentry.origin': 'auto.db.otel.postgres',
78+
'sentry.op': 'db',
79+
}),
80+
description: 'INSERT INTO "NativeUser" ("email", "name") VALUES ($1, $2)',
81+
op: 'db',
82+
status: 'ok',
83+
origin: 'auto.db.otel.postgres',
84+
}),
85+
expect.objectContaining({
86+
data: expect.objectContaining({
87+
'db.system': 'postgresql',
88+
'db.name': 'tests',
89+
'db.statement': 'SELECT * FROM "NativeUser"',
90+
'sentry.origin': 'auto.db.otel.postgres',
91+
'sentry.op': 'db',
92+
}),
93+
description: 'SELECT * FROM "NativeUser"',
94+
op: 'db',
95+
status: 'ok',
96+
origin: 'auto.db.otel.postgres',
97+
}),
98+
]),
99+
};
100+
101+
createRunner(__dirname, 'scenario-native.js')
102+
.withDockerCompose({ workingDirectory: [__dirname], readyMatches: ['port 5432'] })
103+
.expect({ transaction: EXPECTED_TRANSACTION })
104+
.start(done);
105+
});
56106
});

0 commit comments

Comments
 (0)