-
-
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
Merged
AbhiPrasad
merged 10 commits into
getsentry:develop
from
Zen-cronic:feat/knexIntegration-node
Nov 11, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b3ba484
feat(node): Add knexIntegration to Node
Zen-cronic e679a21
test(node): Add tests for knexIntegration
Zen-cronic 36d425f
fix(node): Update integration to identify knex spans
Zen-cronic b40cdad
fix(e2e): Update exports for node-exports-test-app
Zen-cronic 08a6d6d
test(node): Add more db client tests
Zen-cronic de2277a
Merge branch 'develop' into feat/knexIntegration-node
AbhiPrasad 60b6132
Merge branch 'develop' into feat/knexIntegration-node
AbhiPrasad 8c04ae8
clean up integration
AbhiPrasad eb3902d
fix merge conflict in yarn.lock
AbhiPrasad a943bbe
test(node): Fix tests after latest change
Zen-cronic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
dev-packages/node-integration-tests/suites/tracing/knex/docker-compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
53 changes: 53 additions & 0 deletions
53
dev-packages/node-integration-tests/suites/tracing/knex/scenario-withMysql2.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
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, | ||
integrations: [Sentry.knexIntegration()], | ||
}); | ||
|
||
// 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(); |
53 changes: 53 additions & 0 deletions
53
dev-packages/node-integration-tests/suites/tracing/knex/scenario-withPostgres.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
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, | ||
integrations: [Sentry.knexIntegration()], | ||
}); | ||
|
||
// 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(); |
129 changes: 129 additions & 0 deletions
129
dev-packages/node-integration-tests/suites/tracing/knex/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
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', () => { | ||
// Update this if another knex version is installed | ||
const KNEX_VERSION = '2.5.1'; | ||
|
||
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({ | ||
'knex.version': KNEX_VERSION, | ||
'db.system': 'postgresql', | ||
'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({ | ||
'knex.version': KNEX_VERSION, | ||
'db.system': 'postgresql', | ||
'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({ | ||
'knex.version': KNEX_VERSION, | ||
'db.operation': 'select', | ||
'db.sql.table': 'User', | ||
'db.system': 'postgresql', | ||
'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({ | ||
'knex.version': KNEX_VERSION, | ||
'db.system': 'mysql2', | ||
'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({ | ||
'knex.version': KNEX_VERSION, | ||
'db.system': 'mysql2', | ||
'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({ | ||
'knex.version': KNEX_VERSION, | ||
'db.operation': 'select', | ||
'db.sql.table': 'User', | ||
'db.system': 'mysql2', | ||
'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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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 { data } = spanToJSON(span); | ||
// knex.version is always set in the span data | ||
// /~https://github.com/open-telemetry/opentelemetry-js-contrib/blob/0309caeafc44ac9cb13a3345b790b01b76d0497d/plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts#L138 | ||
if (data && 'knex.version' in data) { | ||
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.knex'); | ||
} | ||
}); | ||
}, | ||
}; | ||
}) satisfies IntegrationFn; | ||
|
||
/** | ||
* Knex integration | ||
* | ||
* Capture tracing data for [Knex](https://knexjs.org/). | ||
* | ||
* @example | ||
* ```javascript | ||
* import * as Sentry from '@sentry/node'; | ||
* | ||
* Sentry.init({ | ||
* integrations: [Sentry.knexIntegration()], | ||
* }); | ||
* ``` | ||
*/ | ||
export const knexIntegration = defineIntegration(_knexIntegration); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.