Skip to content

Commit bd8d8bb

Browse files
authored
test(node): Add pg auto instrumentation test for @sentry/node-experimental (#10347)
1 parent 256b8be commit bd8d8bb

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '3.9'
2+
3+
services:
4+
db:
5+
image: postgres:13
6+
restart: always
7+
container_name: integration-tests-postgres
8+
ports:
9+
- '5444:5432'
10+
environment:
11+
POSTGRES_USER: test
12+
POSTGRES_PASSWORD: test
13+
POSTGRES_DB: tests
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
2+
const Sentry = require('@sentry/node-experimental');
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 { Client } = require('pg');
15+
16+
const client = new Client({ port: 5444, user: 'test', password: 'test', database: 'tests' });
17+
18+
async function run() {
19+
await Sentry.startSpan(
20+
{
21+
name: 'Test Transaction',
22+
op: 'transaction',
23+
},
24+
async () => {
25+
try {
26+
await client.connect();
27+
28+
await client
29+
.query(
30+
'CREATE TABLE "User" ("id" SERIAL NOT NULL,"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,"email" TEXT NOT NULL,"name" TEXT,CONSTRAINT "User_pkey" PRIMARY KEY ("id"));',
31+
)
32+
.catch(() => {
33+
// if this is not a fresh database, the table might already exist
34+
});
35+
36+
await client.query('INSERT INTO "User" ("email", "name") VALUES ($1, $2)', ['tim', '[email protected]']);
37+
await client.query('SELECT * FROM "User"');
38+
} finally {
39+
await client.end();
40+
}
41+
},
42+
);
43+
}
44+
45+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
46+
run();
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { conditionalTest } from '../../../utils';
2+
import { createRunner } from '../../../utils/runner';
3+
4+
conditionalTest({ min: 14 })('postgres auto instrumentation', () => {
5+
test('should auto-instrument `pg` package', done => {
6+
const EXPECTED_TRANSACTION = {
7+
transaction: 'Test Transaction',
8+
spans: expect.arrayContaining([
9+
expect.objectContaining({
10+
data: expect.objectContaining({
11+
'db.system': 'postgresql',
12+
'db.name': 'tests',
13+
'sentry.origin': 'manual',
14+
'sentry.op': 'db',
15+
}),
16+
description: 'pg.connect',
17+
op: 'db',
18+
status: 'ok',
19+
}),
20+
expect.objectContaining({
21+
data: expect.objectContaining({
22+
'db.system': 'postgresql',
23+
'db.name': 'tests',
24+
'db.statement': 'INSERT INTO "User" ("email", "name") VALUES ($1, $2)',
25+
'sentry.origin': 'auto.db.otel.postgres',
26+
'sentry.op': 'db',
27+
}),
28+
description: 'INSERT INTO "User" ("email", "name") VALUES ($1, $2)',
29+
op: 'db',
30+
status: 'ok',
31+
origin: 'auto.db.otel.postgres',
32+
}),
33+
expect.objectContaining({
34+
data: expect.objectContaining({
35+
'db.system': 'postgresql',
36+
'db.name': 'tests',
37+
'db.statement': 'SELECT * FROM "User"',
38+
'sentry.origin': 'auto.db.otel.postgres',
39+
'sentry.op': 'db',
40+
}),
41+
description: 'SELECT * FROM "User"',
42+
op: 'db',
43+
status: 'ok',
44+
origin: 'auto.db.otel.postgres',
45+
}),
46+
]),
47+
};
48+
49+
createRunner(__dirname, 'scenario.js')
50+
.withDockerCompose({ workingDirectory: [__dirname], readyMatches: ['port 5432'] })
51+
.expect({ transaction: EXPECTED_TRANSACTION })
52+
.start(done);
53+
});
54+
});

0 commit comments

Comments
 (0)