Skip to content

Commit 08a6d6d

Browse files
committed
test(node): Add more db client tests
Add mysql2 client tests using knex. Add `knexIntegration` exports in several packages. Signed-off-by: Kaung Zin Hein <[email protected]>
1 parent b40cdad commit 08a6d6d

File tree

7 files changed

+130
-9
lines changed

7 files changed

+130
-9
lines changed
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
version: '3.9'
22

33
services:
4-
db:
4+
db_postgres:
55
image: postgres:13
66
restart: always
7-
container_name: integration-tests-knex
7+
container_name: integration-tests-knex-postgres
88
ports:
99
- '5445:5432'
1010
environment:
1111
POSTGRES_USER: test
1212
POSTGRES_PASSWORD: test
1313
POSTGRES_DB: tests
14+
15+
db_mysql2:
16+
image: mysql:8
17+
restart: always
18+
container_name: integration-tests-knex-mysql2
19+
ports:
20+
- '3307:3306'
21+
environment:
22+
MYSQL_ROOT_PASSWORD: docker
23+
MYSQL_DATABASE: tests
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 knex = require('knex').default;
15+
16+
const mysql2Client = knex({
17+
client: 'mysql2',
18+
connection: {
19+
host: 'localhost',
20+
port: 3307,
21+
user: 'root',
22+
password: 'docker',
23+
database: 'tests',
24+
},
25+
});
26+
27+
async function run() {
28+
await Sentry.startSpan(
29+
{
30+
name: 'Test Transaction',
31+
op: 'transaction',
32+
},
33+
async () => {
34+
try {
35+
await mysql2Client.schema.createTable('User', table => {
36+
table.increments('id').notNullable().primary({ constraintName: 'User_pkey' });
37+
table.timestamp('createdAt', { precision: 3 }).notNullable().defaultTo(mysql2Client.fn.now(3));
38+
table.text('email').notNullable();
39+
table.text('name').notNullable();
40+
});
41+
42+
await mysql2Client('User').insert({ name: 'jane', email: '[email protected]' });
43+
await mysql2Client('User').select('*');
44+
} finally {
45+
await mysql2Client.destroy();
46+
}
47+
},
48+
);
49+
}
50+
51+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
52+
run();

dev-packages/node-integration-tests/suites/tracing/knex/scenario.js renamed to dev-packages/node-integration-tests/suites/tracing/knex/scenario-withPostgres.js

File renamed without changes.

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

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import { createRunner } from '../../../utils/runner';
22

3-
jest.setTimeout(75000);
3+
// When running docker compose, we need a larger timeout, as this takes some time...
4+
jest.setTimeout(90000);
45

56
describe('knex auto instrumentation', () => {
6-
test('should auto-instrument `knex` package', done => {
7+
test('should auto-instrument `knex` package when using `pg` client', done => {
78
const EXPECTED_TRANSACTION = {
89
transaction: 'Test Transaction',
910
spans: expect.arrayContaining([
1011
expect.objectContaining({
1112
data: expect.objectContaining({
1213
'db.system': 'knex',
1314
'db.name': 'tests',
14-
'db.statement':
15-
'create table "User" ("id" serial primary key, "createdAt" timestamptz(3) not null default CURRENT_TIMESTAMP(3), "email" text not null, "name" text not null)',
1615
'sentry.origin': 'auto.db.otel.knex',
1716
'sentry.op': 'db',
1817
'net.peer.name': 'localhost',
@@ -27,14 +26,13 @@ describe('knex auto instrumentation', () => {
2726
data: expect.objectContaining({
2827
'db.system': 'knex',
2928
'db.name': 'tests',
30-
'db.statement': 'insert into "User" ("email", "name") values (?, ?)',
3129
'sentry.origin': 'auto.db.otel.knex',
3230
'sentry.op': 'db',
3331
'net.peer.name': 'localhost',
3432
'net.peer.port': 5445,
3533
}),
3634
status: 'ok',
37-
// In the otel spans, the placeholders (e.g., `$1`) are replaced by a `?`.
35+
// In the knex-otel spans, the placeholders (e.g., `$1`) are replaced by a `?`.
3836
description: 'insert into "User" ("email", "name") values (?, ?)',
3937
origin: 'auto.db.otel.knex',
4038
}),
@@ -56,9 +54,67 @@ describe('knex auto instrumentation', () => {
5654
]),
5755
};
5856

59-
createRunner(__dirname, 'scenario.js')
57+
createRunner(__dirname, 'scenario-withPostgres.js')
6058
.withDockerCompose({ workingDirectory: [__dirname], readyMatches: ['port 5432'] })
6159
.expect({ transaction: EXPECTED_TRANSACTION })
6260
.start(done);
6361
});
62+
63+
test('should auto-instrument `knex` package when using `mysql2` client', done => {
64+
const EXPECTED_TRANSACTION = {
65+
transaction: 'Test Transaction',
66+
spans: expect.arrayContaining([
67+
expect.objectContaining({
68+
data: expect.objectContaining({
69+
'db.system': 'knex',
70+
'db.name': 'tests',
71+
'db.user': 'root',
72+
'sentry.origin': 'auto.db.otel.knex',
73+
'sentry.op': 'db',
74+
'net.peer.name': 'localhost',
75+
'net.peer.port': 3307,
76+
}),
77+
status: 'ok',
78+
description:
79+
'create table `User` (`id` int unsigned not null auto_increment primary key, `createdAt` timestamp(3) not null default CURRENT_TIMESTAMP(3), `email` text not null, `name` text not null)',
80+
origin: 'auto.db.otel.knex',
81+
}),
82+
expect.objectContaining({
83+
data: expect.objectContaining({
84+
'db.system': 'knex',
85+
'db.name': 'tests',
86+
'db.user': 'root',
87+
'sentry.origin': 'auto.db.otel.knex',
88+
'sentry.op': 'db',
89+
'net.peer.name': 'localhost',
90+
'net.peer.port': 3307,
91+
}),
92+
status: 'ok',
93+
description: 'insert into `User` (`email`, `name`) values (?, ?)',
94+
origin: 'auto.db.otel.knex',
95+
}),
96+
97+
expect.objectContaining({
98+
data: expect.objectContaining({
99+
'db.operation': 'select',
100+
'db.sql.table': 'User',
101+
'db.system': 'knex',
102+
'db.name': 'tests',
103+
'db.statement': 'select * from `User`',
104+
'db.user': 'root',
105+
'sentry.origin': 'auto.db.otel.knex',
106+
'sentry.op': 'db',
107+
}),
108+
status: 'ok',
109+
description: 'select * from `User`',
110+
origin: 'auto.db.otel.knex',
111+
}),
112+
]),
113+
};
114+
115+
createRunner(__dirname, 'scenario-withMysql2.js')
116+
.withDockerCompose({ workingDirectory: [__dirname], readyMatches: ['port: 3306'] })
117+
.expect({ transaction: EXPECTED_TRANSACTION })
118+
.start(done);
119+
});
64120
});

packages/remix/src/index.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export {
6666
inboundFiltersIntegration,
6767
initOpenTelemetry,
6868
isInitialized,
69+
knexIntegration,
6970
koaIntegration,
7071
lastEventId,
7172
linkedErrorsIntegration,

packages/solidstart/src/server/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export {
5757
inboundFiltersIntegration,
5858
initOpenTelemetry,
5959
isInitialized,
60+
knexIntegration,
6061
koaIntegration,
6162
lastEventId,
6263
linkedErrorsIntegration,

packages/sveltekit/src/server/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export {
5959
inboundFiltersIntegration,
6060
initOpenTelemetry,
6161
isInitialized,
62+
knexIntegration,
6263
koaIntegration,
6364
lastEventId,
6465
linkedErrorsIntegration,

0 commit comments

Comments
 (0)