-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(bun): enable new mysql, pg integrations in 'bun build' #21828
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
Merged
isaacs
merged 4 commits into
develop
from
isaacs/fix-bun-orchestrion-actually-instrument
Jul 2, 2026
+422
−63
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d10e2fb
feat(bun): enable new mysql, pg integrations in 'bun build'
isaacs 6300d38
fixup! feat(bun): enable new mysql, pg integrations in 'bun build'
isaacs d64a4a0
fixup! fixup! feat(bun): enable new mysql, pg integrations in 'bun bu…
isaacs cde1b6a
fixup! fixup! fixup! feat(bun): enable new mysql, pg integrations in …
isaacs 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 hidden or 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 hidden or 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 @@ | ||
| dist |
32 changes: 32 additions & 0 deletions
32
dev-packages/e2e-tests/test-applications/bun-mysql/build.ts
This file contains hidden or 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,32 @@ | ||
| // Builds `src/app.ts` with the orchestrion `bun build` plugin, emitting | ||
| // `dist/app.js` for the server to run. The plugin injects the | ||
| // `orchestrion:mysql:query` diagnostics channel into the bundled `mysql`. | ||
|
|
||
| // @ts-ignore -- subpath export resolved by Bun at runtime; the package | ||
| // tsconfig's node module resolution can't see `exports` subpaths. | ||
| import { sentryBunPlugin } from '@sentry/bun/plugin'; | ||
| import { join } from 'path'; | ||
|
|
||
| void (async () => { | ||
| const result = await Bun.build({ | ||
| entrypoints: [join(__dirname, 'src/app.ts')], | ||
| target: 'bun', | ||
| outdir: join(__dirname, 'dist'), | ||
| // `@sentry/bun` (and its deps) stay external, so we don't bundle the | ||
| // whole SDK/OTel stack. `mysql` is also listed, but the plugin strips | ||
| // instrumented packages back out of `external` so they get bundled and | ||
| // transformed (channel injection only happens on code that passes through | ||
| // the bundler). | ||
| external: ['@sentry/bun', 'mysql'], | ||
| plugins: [sentryBunPlugin()], | ||
| }); | ||
|
|
||
| if (!result.success) { | ||
| // eslint-disable-next-line no-console | ||
| console.error('BUILD_FAILED', result.logs); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| // eslint-disable-next-line no-console | ||
| console.log('BUILD_OK'); | ||
| })(); |
18 changes: 18 additions & 0 deletions
18
dev-packages/e2e-tests/test-applications/bun-mysql/docker-compose.yml
This file contains hidden or 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,18 @@ | ||
| services: | ||
| db: | ||
| image: mysql:8.0 | ||
| restart: always | ||
| container_name: e2e-tests-bun-mysql | ||
| # The `mysql` 2.x driver doesn't speak MySQL 8's default | ||
| # `caching_sha2_password` auth, so force the legacy plugin. | ||
| command: ['--default-authentication-plugin=mysql_native_password'] | ||
| ports: | ||
| - '3306:3306' | ||
| environment: | ||
| MYSQL_ROOT_PASSWORD: password | ||
| healthcheck: | ||
| test: ['CMD-SHELL', 'mysqladmin ping -h 127.0.0.1 -uroot -ppassword'] | ||
| interval: 2s | ||
| timeout: 3s | ||
| retries: 30 | ||
| start_period: 10s | ||
14 changes: 14 additions & 0 deletions
14
dev-packages/e2e-tests/test-applications/bun-mysql/global-setup.mjs
This file contains hidden or 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,14 @@ | ||
| import { execSync } from 'child_process'; | ||
| import { dirname } from 'path'; | ||
| import { fileURLToPath } from 'url'; | ||
|
|
||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| export default async function globalSetup() { | ||
| // Start MySQL via Docker Compose. `--wait` blocks until the healthcheck | ||
| // in docker-compose.yml passes, so the app can connect immediately. | ||
| execSync('docker compose up -d --wait', { | ||
| cwd: __dirname, | ||
| stdio: 'inherit', | ||
| }); | ||
| } |
12 changes: 12 additions & 0 deletions
12
dev-packages/e2e-tests/test-applications/bun-mysql/global-teardown.mjs
This file contains hidden or 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,12 @@ | ||
| import { execSync } from 'child_process'; | ||
| import { dirname } from 'path'; | ||
| import { fileURLToPath } from 'url'; | ||
|
|
||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| export default async function globalTeardown() { | ||
| execSync('docker compose down --volumes', { | ||
| cwd: __dirname, | ||
| stdio: 'inherit', | ||
| }); | ||
| } |
25 changes: 25 additions & 0 deletions
25
dev-packages/e2e-tests/test-applications/bun-mysql/package.json
This file contains hidden or 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,25 @@ | ||
| { | ||
| "name": "bun-mysql", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "start": "docker compose up -d --wait && bun run dist/app.js", | ||
| "test": "playwright test", | ||
| "clean": "npx rimraf node_modules pnpm-lock.yaml dist", | ||
| "test:build": "pnpm install && bun run build.ts", | ||
| "test:assert": "pnpm test" | ||
| }, | ||
| "dependencies": { | ||
| "@sentry/bun": "file:../../packed/sentry-bun-packed.tgz", | ||
| "mysql": "2.18.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@playwright/test": "~1.56.0", | ||
| "@sentry-internal/test-utils": "link:../../../test-utils", | ||
| "bun-types": "^1.2.9" | ||
| }, | ||
| "volta": { | ||
| "extends": "../../package.json" | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
dev-packages/e2e-tests/test-applications/bun-mysql/playwright.config.mjs
This file contains hidden or 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,12 @@ | ||
| import { getPlaywrightConfig } from '@sentry-internal/test-utils'; | ||
|
|
||
| const config = getPlaywrightConfig({ | ||
| startCommand: `pnpm start`, | ||
| port: 3030, | ||
| }); | ||
|
|
||
| export default { | ||
| ...config, | ||
| globalSetup: './global-setup.mjs', | ||
| globalTeardown: './global-teardown.mjs', | ||
| }; |
73 changes: 73 additions & 0 deletions
73
dev-packages/e2e-tests/test-applications/bun-mysql/src/app.ts
This file contains hidden or 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,73 @@ | ||
| import * as Sentry from '@sentry/bun'; | ||
|
|
||
| // @ts-ignore -- `mysql` ships no type declarations; only needed at runtime. | ||
| import mysql from 'mysql'; | ||
|
|
||
| Sentry.init({ | ||
| environment: 'qa', | ||
| dsn: process.env.E2E_TEST_DSN, | ||
| debug: !!process.env.DEBUG, | ||
| tunnel: 'http://localhost:3031/', // proxy server | ||
| tracesSampleRate: 1, | ||
| }); | ||
|
|
||
| interface Connection { | ||
| query(sql: string, cb: (err: unknown) => void): void; | ||
| connect(cb: (err: unknown) => void): void; | ||
| on(event: string, cb: (err: unknown) => void): void; | ||
| } | ||
| interface MysqlModule { | ||
| createConnection(opts: { host: string; port: number; user: string; password: string }): Connection; | ||
| } | ||
|
|
||
| // `mysql` was transformed at build time (by `@sentry/bun/plugin`) to publish | ||
| // the `orchestrion:mysql:query` channel. The Bun SDK subscribes to it, so the | ||
| // queries below produce db spans without any OTel require-hook. | ||
| const connection = (mysql as MysqlModule).createConnection({ | ||
| host: process.env.MYSQL_HOST ?? '127.0.0.1', | ||
| port: Number(process.env.MYSQL_PORT ?? 3306), | ||
| user: 'root', | ||
| password: 'password', | ||
| }); | ||
|
|
||
| // Swallow connection errors (e.g. the DB container going away at teardown) so | ||
| // they don't become an uncaught exception that crashes the process on shutdown. | ||
| connection.on('error', (err: unknown) => { | ||
| // eslint-disable-next-line no-console | ||
| console.error('mysql connection error', err); | ||
| }); | ||
|
|
||
| connection.connect((err: unknown) => { | ||
| if (err) { | ||
| // eslint-disable-next-line no-console | ||
| console.error('mysql connect error', err); | ||
| } | ||
| }); | ||
|
|
||
| Bun.serve({ | ||
| port: 3030, | ||
| hostname: '0.0.0.0', | ||
| async fetch(req: Request) { | ||
| const url = new URL(req.url); | ||
|
|
||
| // Runs two queries, the second NESTED inside the first's callback. mysql | ||
| // dispatches that callback from its socket data handler (a fresh async | ||
| // context), so the nested query's span only lands on this request's | ||
| // http.server transaction if the channel subscriber restored the parent | ||
| // across the async boundary. | ||
| if (url.pathname === '/test-mysql') { | ||
| await new Promise<void>((resolve, reject) => { | ||
| connection.query('SELECT 1 + 1 AS solution', (err: unknown) => { | ||
| if (err) return reject(err); | ||
| connection.query('SELECT NOW()', (err2: unknown) => { | ||
| if (err2) return reject(err2); | ||
| resolve(); | ||
| }); | ||
| }); | ||
| }); | ||
| return Response.json({ status: 'ok' }); | ||
| } | ||
|
|
||
| return new Response('Not found', { status: 404 }); | ||
| }, | ||
| }); |
6 changes: 6 additions & 0 deletions
6
dev-packages/e2e-tests/test-applications/bun-mysql/start-event-proxy.mjs
This file contains hidden or 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,6 @@ | ||
| import { startEventProxyServer } from '@sentry-internal/test-utils'; | ||
|
|
||
| startEventProxyServer({ | ||
| port: 3031, | ||
| proxyServerName: 'bun-mysql', | ||
| }); |
55 changes: 55 additions & 0 deletions
55
dev-packages/e2e-tests/test-applications/bun-mysql/tests/mysql.test.ts
This file contains hidden or 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,55 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { waitForTransaction } from '@sentry-internal/test-utils'; | ||
|
|
||
| test('mysql queries emit a db span with orchestrion-channel attributes', async ({ baseURL }) => { | ||
| // Each incoming request gets a Sentry http.server transaction; the mysql | ||
| // queries run inside it, so their db spans attach to that transaction. The | ||
| // channels were injected at build time by `@sentry/bun/plugin`, and the Bun | ||
| // SDK subscribes to them by default. | ||
| const transactionPromise = waitForTransaction('bun-mysql', event => { | ||
| return ( | ||
| event?.contexts?.trace?.op === 'http.server' && | ||
| (event.request?.url ?? '').includes('/test-mysql') && | ||
| (event.spans?.some(span => span.op === 'db') ?? false) | ||
| ); | ||
| }); | ||
|
|
||
| const res = await fetch(`${baseURL}/test-mysql`); | ||
| expect(res.status).toBe(200); | ||
| await res.json(); | ||
|
|
||
| const transaction = await transactionPromise; | ||
| const dbSpans = transaction.spans!.filter(span => span.op === 'db'); | ||
|
|
||
| const firstQuery = dbSpans.find(span => span.description === 'SELECT 1 + 1 AS solution'); | ||
| expect(firstQuery).toBeDefined(); | ||
| expect(firstQuery!.data?.['sentry.origin']).toBe('auto.db.orchestrion.mysql'); | ||
| expect(firstQuery!.data?.['db.system']).toBe('mysql'); | ||
| expect(firstQuery!.data?.['db.statement']).toBe('SELECT 1 + 1 AS solution'); | ||
| expect(firstQuery!.data?.['net.peer.port']).toBe(3306); | ||
| expect(firstQuery!.data?.['db.user']).toBe('root'); | ||
| }); | ||
|
|
||
| test('a nested query lands on the same transaction (async context restored)', async ({ baseURL }) => { | ||
| // The second query runs inside the first query's callback — i.e. across | ||
| // mysql's async socket-callback dispatch. Both spans appearing on the SAME | ||
| // http.server transaction proves the channel subscriber restored the parent | ||
| // span across that async boundary (otherwise the nested query would start its | ||
| // own trace and never join this transaction). | ||
| const transactionPromise = waitForTransaction('bun-mysql', event => { | ||
| return ( | ||
| event?.contexts?.trace?.op === 'http.server' && | ||
| (event.request?.url ?? '').includes('/test-mysql') && | ||
| (event.spans?.filter(span => span.op === 'db').length ?? 0) >= 2 | ||
| ); | ||
| }); | ||
|
|
||
| const res = await fetch(`${baseURL}/test-mysql`); | ||
| expect(res.status).toBe(200); | ||
| await res.json(); | ||
|
|
||
| const transaction = await transactionPromise; | ||
| const descriptions = transaction.spans!.filter(span => span.op === 'db').map(span => span.description); | ||
| expect(descriptions).toContain('SELECT 1 + 1 AS solution'); | ||
| expect(descriptions).toContain('SELECT NOW()'); | ||
| }); |
11 changes: 11 additions & 0 deletions
11
dev-packages/e2e-tests/test-applications/bun-mysql/tsconfig.json
This file contains hidden or 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,11 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "types": ["bun-types"], | ||
| "esModuleInterop": true, | ||
| "lib": ["es2020"], | ||
| "strict": true, | ||
| "outDir": "dist", | ||
| "skipLibCheck": true | ||
| }, | ||
| "include": ["src/**/*.ts", "build.ts"] | ||
| } |
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.