diff --git a/dev-packages/deno-integration-tests/suites/orchestrion-mysql2/test.ts b/dev-packages/deno-integration-tests/suites/orchestrion-mysql2/test.ts new file mode 100644 index 000000000000..b0aef546de46 --- /dev/null +++ b/dev-packages/deno-integration-tests/suites/orchestrion-mysql2/test.ts @@ -0,0 +1,106 @@ +// + +import { tracingChannel } from 'node:diagnostics_channel'; +import type { TransactionEvent } from '@sentry/core'; +import type { DenoClient } from '@sentry/deno'; +import { getCurrentScope, getGlobalScope, getIsolationScope, init, startSpan } from '@sentry/deno'; +import { assert } from 'https://deno.land/std@0.212.0/assert/assert.ts'; +import { assertEquals } from 'https://deno.land/std@0.212.0/assert/assert_equals.ts'; +import { assertExists } from 'https://deno.land/std@0.212.0/assert/assert_exists.ts'; + +function resetGlobals(): void { + getCurrentScope().clear(); + getCurrentScope().setClient(undefined); + getIsolationScope().clear(); + getGlobalScope().clear(); +} + +/** See deno-redis.test.ts — same sink shape, deduped for clarity. */ +function transactionSink(): { + beforeSendTransaction: (event: TransactionEvent) => null; + waitFor: (predicate: (event: TransactionEvent) => boolean) => Promise; +} { + const transactions: TransactionEvent[] = []; + const waiters: { predicate: (e: TransactionEvent) => boolean; resolve: (e: TransactionEvent) => void }[] = []; + return { + beforeSendTransaction(event) { + transactions.push(event); + for (let i = waiters.length - 1; i >= 0; i--) { + const w = waiters[i]!; + if (w.predicate(event)) { + waiters.splice(i, 1); + w.resolve(event); + } + } + return null; + }, + waitFor(predicate) { + const already = transactions.find(predicate); + if (already) return Promise.resolve(already); + return new Promise(resolve => { + waiters.push({ predicate, resolve }); + }); + }, + }; +} + +function withTimeout(p: Promise, ms: number, what: string): Promise { + let timer: ReturnType | undefined; + const timeout = new Promise((_, reject) => { + timer = setTimeout(() => reject(new Error(`Timed out waiting for ${what} after ${ms}ms`)), ms); + }); + return Promise.race([p, timeout]).finally(() => { + if (timer !== undefined) clearTimeout(timer); + }); +} + +Deno.test('mysql2 instrumentation: included in default integrations (Deno 2.8.0+)', () => { + resetGlobals(); + const client = init({ dsn: 'https://username@domain/123' }) as DenoClient; + const names = client.getOptions().integrations.map(i => i.name); + assert(names.includes('Mysql2'), `Mysql2 should be in defaults, got ${names.join(', ')}`); +}); + +Deno.test('mysql2 instrumentation: orchestrion:mysql2:query channel produces a nested db span', async () => { + resetGlobals(); + const sink = transactionSink(); + init({ + dsn: 'https://username@domain/123', + tracesSampleRate: 1, + beforeSendTransaction: sink.beforeSendTransaction, + }); + + const channel = tracingChannel('orchestrion:mysql2:query'); + + // `arguments[0]` is the SQL; `self.config` is the mysql2 connection config. + const ctx = { + arguments: ['SELECT 1 AS solution'], + self: { config: { host: '127.0.0.1', port: 3306, database: 'mydb', user: 'root' } }, + }; + + startSpan({ name: 'parent', op: 'test' }, () => { + channel.start.runStores(ctx, () => { + channel.end.publish(ctx); + }); + channel.asyncStart.runStores(ctx, () => { + channel.asyncEnd.publish(ctx); + }); + }); + + const parent = await withTimeout( + sink.waitFor(t => t.transaction === 'parent'), + 5000, + "'parent' transaction", + ); + + const mysqlSpan = parent.spans?.find(s => s.op === 'db'); + assertExists(mysqlSpan, `expected a db child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`); + assertEquals(mysqlSpan!.description, 'SELECT 1 AS solution'); + assertEquals(mysqlSpan!.data?.['db.system'], 'mysql'); + assertEquals(mysqlSpan!.data?.['db.statement'], 'SELECT 1 AS solution'); + assertEquals(mysqlSpan!.data?.['db.name'], 'mydb'); + assertEquals(mysqlSpan!.data?.['db.user'], 'root'); + assertEquals(mysqlSpan!.data?.['net.peer.name'], '127.0.0.1'); + assertEquals(mysqlSpan!.data?.['net.peer.port'], 3306); + assertEquals(mysqlSpan!.data?.['sentry.origin'], 'auto.db.orchestrion.mysql2'); +}); diff --git a/packages/deno/src/index.ts b/packages/deno/src/index.ts index 2be88c84c751..3d3ef7fa10f9 100644 --- a/packages/deno/src/index.ts +++ b/packages/deno/src/index.ts @@ -123,6 +123,7 @@ export { mongodbChannelIntegration, mongooseChannelIntegration, mysqlChannelIntegration, + mysql2ChannelIntegration, postgresChannelIntegration, postgresJsChannelIntegration, } from '@sentry/server-utils/orchestrion'; diff --git a/packages/deno/src/sdk.ts b/packages/deno/src/sdk.ts index 77a241a57a89..e3ec76137b43 100644 --- a/packages/deno/src/sdk.ts +++ b/packages/deno/src/sdk.ts @@ -17,6 +17,7 @@ import { mongodbChannelIntegration, mongooseChannelIntegration, mysqlChannelIntegration, + mysql2ChannelIntegration, postgresChannelIntegration, postgresJsChannelIntegration, } from '@sentry/server-utils/orchestrion'; @@ -77,6 +78,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] { mongodbChannelIntegration(), mongooseChannelIntegration(), mysqlChannelIntegration(), + mysql2ChannelIntegration(), postgresChannelIntegration(), postgresJsChannelIntegration(), ] diff --git a/packages/deno/test/__snapshots__/mod.test.ts.snap b/packages/deno/test/__snapshots__/mod.test.ts.snap index 8fb952298f27..e110dcb09e6c 100644 --- a/packages/deno/test/__snapshots__/mod.test.ts.snap +++ b/packages/deno/test/__snapshots__/mod.test.ts.snap @@ -120,6 +120,7 @@ snapshot[`captureException 1`] = ` "Mongo", "Mongoose", "Mysql", + "Mysql2", "Postgres", "PostgresJs", "ContextLines", @@ -202,6 +203,7 @@ snapshot[`captureMessage 1`] = ` "Mongo", "Mongoose", "Mysql", + "Mysql2", "Postgres", "PostgresJs", "ContextLines", @@ -291,6 +293,7 @@ snapshot[`captureMessage twice 1`] = ` "Mongo", "Mongoose", "Mysql", + "Mysql2", "Postgres", "PostgresJs", "ContextLines", @@ -387,6 +390,7 @@ snapshot[`captureMessage twice 2`] = ` "Mongo", "Mongoose", "Mysql", + "Mysql2", "Postgres", "PostgresJs", "ContextLines",