Skip to content

Commit 36d425f

Browse files
committed
fix(node): Update integration to identify knex spans
Differentiate knex spans from other database spans. Signed-off-by: Kaung Zin Hein <[email protected]>
1 parent e679a21 commit 36d425f

File tree

3 files changed

+28
-44
lines changed

3 files changed

+28
-44
lines changed

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

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ setInterval(() => {}, 1000);
1313

1414
const knex = require('knex').default;
1515

16+
const pgClient = knex({
17+
client: 'pg',
18+
connection: {
19+
host: 'localhost',
20+
port: 5445,
21+
user: 'test',
22+
password: 'test',
23+
database: 'tests',
24+
},
25+
});
26+
1627
async function run() {
1728
await Sentry.startSpan(
1829
{
@@ -21,39 +32,17 @@ async function run() {
2132
},
2233
async () => {
2334
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 => {
35+
await pgClient.schema.createTable('User', table => {
3636
table.increments('id').notNullable().primary({ constraintName: 'User_pkey' });
37-
table.timestamp('createdAt', { precision: 3 }).notNullable().defaultTo(pgKnex.fn.now(3));
37+
table.timestamp('createdAt', { precision: 3 }).notNullable().defaultTo(pgClient.fn.now(3));
3838
table.text('email').notNullable();
3939
table.text('name').notNullable();
4040
});
4141

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"');
42+
await pgClient('User').insert({ name: 'bob', email: '[email protected]' });
43+
await pgClient('User').select('*');
5544
} finally {
56-
// await client.end();
45+
await pgClient.destroy();
5746
}
5847
},
5948
);

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,6 @@ describe('knex auto instrumentation', () => {
77
const EXPECTED_TRANSACTION = {
88
transaction: 'Test Transaction',
99
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-
}),
2310
expect.objectContaining({
2411
data: expect.objectContaining({
2512
'db.system': 'knex',

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

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

66
const INTEGRATION_NAME = 'Knex';
77

8-
export const instrumentKnex = generateInstrumentOnce(INTEGRATION_NAME, () => new KnexInstrumentation({}));
8+
export const instrumentKnex = generateInstrumentOnce(
9+
INTEGRATION_NAME,
10+
() => new KnexInstrumentation({ requireParentSpan: true }),
11+
);
912

1013
const _knexIntegration = (() => {
1114
return {
@@ -16,8 +19,13 @@ const _knexIntegration = (() => {
1619

1720
setup(client) {
1821
client.on('spanStart', span => {
19-
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.knex');
20-
span.setAttribute('db.system', 'knex');
22+
const spanJSON = spanToJSON(span);
23+
const spanData = spanJSON.data;
24+
25+
if (spanData && 'knex.version' in spanData) {
26+
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.knex');
27+
span.setAttribute('db.system', 'knex');
28+
}
2129
});
2230
},
2331
};

0 commit comments

Comments
 (0)