Skip to content

Commit e679a21

Browse files
committed
test(node): Add tests for knexIntegration
Set up postgres for testing. Update span attributes to be named under 'knex'. Signed-off-by: Kaung Zin Hein <[email protected]>
1 parent b3ba484 commit e679a21

File tree

4 files changed

+155
-6
lines changed

4 files changed

+155
-6
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-knex
8+
ports:
9+
- '5445:5432'
10+
environment:
11+
POSTGRES_USER: test
12+
POSTGRES_PASSWORD: test
13+
POSTGRES_DB: tests
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
async function run() {
17+
await Sentry.startSpan(
18+
{
19+
name: 'Test Transaction',
20+
op: 'transaction',
21+
},
22+
async () => {
23+
try {
24+
const pgKnex = knex({
25+
client: 'pg',
26+
connection: {
27+
host: 'localhost',
28+
port: 5445,
29+
user: 'test',
30+
password: 'test',
31+
database: 'tests',
32+
},
33+
});
34+
35+
await pgKnex.schema.createTable('User', table => {
36+
table.increments('id').notNullable().primary({ constraintName: 'User_pkey' });
37+
table.timestamp('createdAt', { precision: 3 }).notNullable().defaultTo(pgKnex.fn.now(3));
38+
table.text('email').notNullable();
39+
table.text('name').notNullable();
40+
});
41+
42+
await pgKnex('User').insert({ name: 'bob', email: '[email protected]' });
43+
await pgKnex('User').select('*');
44+
// await client
45+
// .query(
46+
// '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"));',
47+
// )
48+
// .catch(() => {
49+
// // if this is not a fresh database, the table might already exist
50+
// });
51+
52+
// await client.query('');
53+
// await client.query('INSERT INTO "User" ("email", "name") VALUES ($1, $2)', ['tim', '[email protected]']);
54+
// await client.query('SELECT * FROM "User"');
55+
} finally {
56+
// await client.end();
57+
}
58+
},
59+
);
60+
}
61+
62+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
63+
run();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { createRunner } from '../../../utils/runner';
2+
3+
jest.setTimeout(75000);
4+
5+
describe('knex auto instrumentation', () => {
6+
test('should auto-instrument `knex` package', done => {
7+
const EXPECTED_TRANSACTION = {
8+
transaction: 'Test Transaction',
9+
spans: expect.arrayContaining([
10+
expect.objectContaining({
11+
data: expect.objectContaining({
12+
'db.system': 'knex',
13+
'db.name': 'tests',
14+
'sentry.origin': 'auto.db.otel.knex',
15+
'sentry.op': 'db',
16+
'net.peer.name': 'localhost',
17+
'net.peer.port': 5445,
18+
}),
19+
status: 'ok',
20+
description: 'pg.connect',
21+
origin: 'auto.db.otel.knex',
22+
}),
23+
expect.objectContaining({
24+
data: expect.objectContaining({
25+
'db.system': 'knex',
26+
'db.name': 'tests',
27+
'db.statement':
28+
'create table "User" ("id" serial primary key, "createdAt" timestamptz(3) not null default CURRENT_TIMESTAMP(3), "email" text not null, "name" text not null)',
29+
'sentry.origin': 'auto.db.otel.knex',
30+
'sentry.op': 'db',
31+
'net.peer.name': 'localhost',
32+
'net.peer.port': 5445,
33+
}),
34+
status: 'ok',
35+
description:
36+
'create table "User" ("id" serial primary key, "createdAt" timestamptz(3) not null default CURRENT_TIMESTAMP(3), "email" text not null, "name" text not null)',
37+
origin: 'auto.db.otel.knex',
38+
}),
39+
expect.objectContaining({
40+
data: expect.objectContaining({
41+
'db.system': 'knex',
42+
'db.name': 'tests',
43+
'db.statement': 'insert into "User" ("email", "name") values (?, ?)',
44+
'sentry.origin': 'auto.db.otel.knex',
45+
'sentry.op': 'db',
46+
'net.peer.name': 'localhost',
47+
'net.peer.port': 5445,
48+
}),
49+
status: 'ok',
50+
// In the otel spans, the placeholders (e.g., `$1`) are replaced by a `?`.
51+
description: 'insert into "User" ("email", "name") values (?, ?)',
52+
origin: 'auto.db.otel.knex',
53+
}),
54+
55+
expect.objectContaining({
56+
data: expect.objectContaining({
57+
'db.operation': 'select',
58+
'db.sql.table': 'User',
59+
'db.system': 'knex',
60+
'db.name': 'tests',
61+
'db.statement': 'select * from "User"',
62+
'sentry.origin': 'auto.db.otel.knex',
63+
'sentry.op': 'db',
64+
}),
65+
status: 'ok',
66+
description: 'select * from "User"',
67+
origin: 'auto.db.otel.knex',
68+
}),
69+
]),
70+
};
71+
72+
createRunner(__dirname, 'scenario.js')
73+
.withDockerCompose({ workingDirectory: [__dirname], readyMatches: ['port 5432'] })
74+
.expect({ transaction: EXPECTED_TRANSACTION })
75+
.start(done);
76+
});
77+
});

packages/node/src/integrations/tracing/knex.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { KnexInstrumentation } from '@opentelemetry/instrumentation-knex';
2-
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, defineIntegration, spanToJSON } from '@sentry/core';
2+
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, defineIntegration } from '@sentry/core';
33
import type { IntegrationFn } from '@sentry/types';
44
import { generateInstrumentOnce } from '../../otel/instrument';
55

@@ -16,12 +16,8 @@ const _knexIntegration = (() => {
1616

1717
setup(client) {
1818
client.on('spanStart', span => {
19-
const spanJSON = spanToJSON(span);
20-
21-
const spanDescription = spanJSON.description;
22-
console.log('[Knex]: span description: ', spanDescription);
23-
2419
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.knex');
20+
span.setAttribute('db.system', 'knex');
2521
});
2622
},
2723
};

0 commit comments

Comments
 (0)