-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(node): Add knex
integration
#13526
Changes from 5 commits
b3ba484
e679a21
36d425f
b40cdad
08a6d6d
de2277a
60b6132
8c04ae8
eb3902d
a943bbe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
version: '3.9' | ||
|
||
services: | ||
db_postgres: | ||
image: postgres:13 | ||
restart: always | ||
container_name: integration-tests-knex-postgres | ||
ports: | ||
- '5445:5432' | ||
environment: | ||
POSTGRES_USER: test | ||
POSTGRES_PASSWORD: test | ||
POSTGRES_DB: tests | ||
|
||
db_mysql2: | ||
image: mysql:8 | ||
restart: always | ||
container_name: integration-tests-knex-mysql2 | ||
ports: | ||
- '3307:3306' | ||
environment: | ||
MYSQL_ROOT_PASSWORD: docker | ||
MYSQL_DATABASE: tests |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
release: '1.0', | ||
tracesSampleRate: 1.0, | ||
transport: loggingTransport, | ||
}); | ||
|
||
// Stop the process from exiting before the transaction is sent | ||
setInterval(() => {}, 1000); | ||
|
||
const knex = require('knex').default; | ||
|
||
const mysql2Client = knex({ | ||
client: 'mysql2', | ||
connection: { | ||
host: 'localhost', | ||
port: 3307, | ||
user: 'root', | ||
password: 'docker', | ||
database: 'tests', | ||
}, | ||
}); | ||
|
||
async function run() { | ||
await Sentry.startSpan( | ||
{ | ||
name: 'Test Transaction', | ||
op: 'transaction', | ||
}, | ||
async () => { | ||
try { | ||
await mysql2Client.schema.createTable('User', table => { | ||
table.increments('id').notNullable().primary({ constraintName: 'User_pkey' }); | ||
table.timestamp('createdAt', { precision: 3 }).notNullable().defaultTo(mysql2Client.fn.now(3)); | ||
table.text('email').notNullable(); | ||
table.text('name').notNullable(); | ||
}); | ||
|
||
await mysql2Client('User').insert({ name: 'jane', email: 'jane@domain.com' }); | ||
await mysql2Client('User').select('*'); | ||
} finally { | ||
await mysql2Client.destroy(); | ||
} | ||
}, | ||
); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
run(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
release: '1.0', | ||
tracesSampleRate: 1.0, | ||
transport: loggingTransport, | ||
}); | ||
|
||
// Stop the process from exiting before the transaction is sent | ||
setInterval(() => {}, 1000); | ||
|
||
const knex = require('knex').default; | ||
|
||
const pgClient = knex({ | ||
client: 'pg', | ||
connection: { | ||
host: 'localhost', | ||
port: 5445, | ||
user: 'test', | ||
password: 'test', | ||
database: 'tests', | ||
}, | ||
}); | ||
|
||
async function run() { | ||
await Sentry.startSpan( | ||
{ | ||
name: 'Test Transaction', | ||
op: 'transaction', | ||
}, | ||
async () => { | ||
try { | ||
await pgClient.schema.createTable('User', table => { | ||
table.increments('id').notNullable().primary({ constraintName: 'User_pkey' }); | ||
table.timestamp('createdAt', { precision: 3 }).notNullable().defaultTo(pgClient.fn.now(3)); | ||
table.text('email').notNullable(); | ||
table.text('name').notNullable(); | ||
}); | ||
|
||
await pgClient('User').insert({ name: 'bob', email: 'bob@domain.com' }); | ||
await pgClient('User').select('*'); | ||
} finally { | ||
await pgClient.destroy(); | ||
} | ||
}, | ||
); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
run(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { createRunner } from '../../../utils/runner'; | ||
|
||
// When running docker compose, we need a larger timeout, as this takes some time... | ||
jest.setTimeout(90000); | ||
|
||
describe('knex auto instrumentation', () => { | ||
test('should auto-instrument `knex` package when using `pg` client', done => { | ||
const EXPECTED_TRANSACTION = { | ||
transaction: 'Test Transaction', | ||
spans: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'db.system': 'knex', | ||
'db.name': 'tests', | ||
'sentry.origin': 'auto.db.otel.knex', | ||
'sentry.op': 'db', | ||
'net.peer.name': 'localhost', | ||
'net.peer.port': 5445, | ||
}), | ||
status: 'ok', | ||
description: | ||
'create table "User" ("id" serial primary key, "createdAt" timestamptz(3) not null default CURRENT_TIMESTAMP(3), "email" text not null, "name" text not null)', | ||
origin: 'auto.db.otel.knex', | ||
}), | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'db.system': 'knex', | ||
'db.name': 'tests', | ||
'sentry.origin': 'auto.db.otel.knex', | ||
'sentry.op': 'db', | ||
'net.peer.name': 'localhost', | ||
'net.peer.port': 5445, | ||
}), | ||
status: 'ok', | ||
// In the knex-otel spans, the placeholders (e.g., `$1`) are replaced by a `?`. | ||
description: 'insert into "User" ("email", "name") values (?, ?)', | ||
origin: 'auto.db.otel.knex', | ||
}), | ||
|
||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'db.operation': 'select', | ||
'db.sql.table': 'User', | ||
'db.system': 'knex', | ||
'db.name': 'tests', | ||
'db.statement': 'select * from "User"', | ||
'sentry.origin': 'auto.db.otel.knex', | ||
'sentry.op': 'db', | ||
}), | ||
status: 'ok', | ||
description: 'select * from "User"', | ||
origin: 'auto.db.otel.knex', | ||
}), | ||
]), | ||
}; | ||
|
||
createRunner(__dirname, 'scenario-withPostgres.js') | ||
.withDockerCompose({ workingDirectory: [__dirname], readyMatches: ['port 5432'] }) | ||
.expect({ transaction: EXPECTED_TRANSACTION }) | ||
.start(done); | ||
}); | ||
|
||
test('should auto-instrument `knex` package when using `mysql2` client', done => { | ||
const EXPECTED_TRANSACTION = { | ||
transaction: 'Test Transaction', | ||
spans: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'db.system': 'knex', | ||
'db.name': 'tests', | ||
'db.user': 'root', | ||
'sentry.origin': 'auto.db.otel.knex', | ||
'sentry.op': 'db', | ||
'net.peer.name': 'localhost', | ||
'net.peer.port': 3307, | ||
}), | ||
status: 'ok', | ||
description: | ||
'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)', | ||
origin: 'auto.db.otel.knex', | ||
}), | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'db.system': 'knex', | ||
'db.name': 'tests', | ||
'db.user': 'root', | ||
'sentry.origin': 'auto.db.otel.knex', | ||
'sentry.op': 'db', | ||
'net.peer.name': 'localhost', | ||
'net.peer.port': 3307, | ||
}), | ||
status: 'ok', | ||
description: 'insert into `User` (`email`, `name`) values (?, ?)', | ||
origin: 'auto.db.otel.knex', | ||
}), | ||
|
||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'db.operation': 'select', | ||
'db.sql.table': 'User', | ||
'db.system': 'knex', | ||
'db.name': 'tests', | ||
'db.statement': 'select * from `User`', | ||
'db.user': 'root', | ||
'sentry.origin': 'auto.db.otel.knex', | ||
'sentry.op': 'db', | ||
}), | ||
status: 'ok', | ||
description: 'select * from `User`', | ||
origin: 'auto.db.otel.knex', | ||
}), | ||
]), | ||
}; | ||
|
||
createRunner(__dirname, 'scenario-withMysql2.js') | ||
.withDockerCompose({ workingDirectory: [__dirname], readyMatches: ['port: 3306'] }) | ||
.expect({ transaction: EXPECTED_TRANSACTION }) | ||
.start(done); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { KnexInstrumentation } from '@opentelemetry/instrumentation-knex'; | ||
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, defineIntegration, spanToJSON } from '@sentry/core'; | ||
import type { IntegrationFn } from '@sentry/types'; | ||
import { generateInstrumentOnce } from '../../otel/instrument'; | ||
|
||
const INTEGRATION_NAME = 'Knex'; | ||
|
||
export const instrumentKnex = generateInstrumentOnce( | ||
INTEGRATION_NAME, | ||
() => new KnexInstrumentation({ requireParentSpan: true }), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, |
||
); | ||
|
||
const _knexIntegration = (() => { | ||
return { | ||
name: INTEGRATION_NAME, | ||
setupOnce() { | ||
instrumentKnex(); | ||
}, | ||
|
||
setup(client) { | ||
client.on('spanStart', span => { | ||
const spanJSON = spanToJSON(span); | ||
const spanData = spanJSON.data; | ||
|
||
if (spanData && 'knex.version' in spanData) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In knex instr, the prop |
||
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.knex'); | ||
span.setAttribute('db.system', 'knex'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't override There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. noted |
||
} | ||
}); | ||
}, | ||
}; | ||
}) satisfies IntegrationFn; | ||
|
||
/** | ||
* Knex integration | ||
* | ||
* Capture tracing data for Knex. | ||
*/ | ||
export const knexIntegration = defineIntegration(_knexIntegration); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Breaking change in major version 3 drops support for node < 16. See here.