-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(bun): Add orchestrion bun build plugin #21410
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
Changes from all commits
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,37 @@ | ||
| // Builds the smoke scenario with the orchestrion `bun build` plugin and writes | ||
| // the bundle to a temp dir, printing the output path for test.ts to execute. | ||
| // | ||
| // A successful build proves `bun build` runs with the plugin; running the bundle | ||
| // (see test.ts) then proves the bundled `mysql` is actually instrumented. | ||
|
|
||
| // @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 { tmpdir } from 'os'; | ||
| import { join } from 'path'; | ||
|
|
||
| void (async () => { | ||
| const outdir = join(tmpdir(), `sentry-bun-orchestrion-${process.pid}-${Date.now()}`); | ||
| const result = await Bun.build({ | ||
| entrypoints: [join(__dirname, 'scenario.ts')], | ||
| target: 'bun', | ||
| outdir, | ||
| plugins: [sentryBunPlugin()], | ||
| }); | ||
|
|
||
| if (!result.success) { | ||
| // eslint-disable-next-line no-console | ||
| console.error('BUILD_FAILED', result.logs); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const output = result.outputs[0]; | ||
| if (!output) { | ||
| // eslint-disable-next-line no-console | ||
| console.error('BUILD_FAILED no outputs'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| // eslint-disable-next-line no-console | ||
| console.log(`BUILD_OK outfile=${output.path}`); | ||
| })(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Bundled entry for the `bun build` smoke test. | ||
| // | ||
| // Once `Bun.build` (with the orchestrion plugin) has transformed `mysql`, | ||
| // calling `connection.query()` publishes to the `orchestrion:mysql:query` | ||
| // tracing channel. | ||
| // | ||
| // `start` fires synchronously on the call, so no live database is needed. | ||
| // | ||
| // We subscribe, run a query, and report which channel events fired | ||
| // (plus the detection marker the plugin's banner sets at boot). | ||
|
|
||
| import { tracingChannel } from 'node:diagnostics_channel'; | ||
|
|
||
| // @ts-ignore -- `mysql` ships no type declarations; only needed at runtime. | ||
| import mysql from 'mysql'; | ||
|
|
||
| interface QueryContext { | ||
| arguments?: unknown[]; | ||
| } | ||
| interface Connection { | ||
| query(sql: string, cb: () => void): void; | ||
| destroy(): void; | ||
| } | ||
| interface MysqlModule { | ||
| createConnection(opts: { host: string; user: string }): Connection; | ||
| } | ||
|
|
||
| const events: string[] = []; | ||
| let statement = ''; | ||
|
|
||
| tracingChannel('orchestrion:mysql:query').subscribe({ | ||
| start(message: unknown) { | ||
| events.push('start'); | ||
| const first = (message as QueryContext).arguments?.[0]; | ||
| statement = typeof first === 'string' ? first : ''; | ||
| }, | ||
| end() { | ||
| events.push('end'); | ||
| }, | ||
| asyncStart() {}, | ||
| asyncEnd() { | ||
| events.push('asyncEnd'); | ||
| }, | ||
| error() {}, | ||
| }); | ||
|
|
||
| const conn = (mysql as MysqlModule).createConnection({ host: '127.0.0.1', user: 'root' }); | ||
| try { | ||
| conn.query('SELECT 1 AS solution', () => {}); | ||
| } catch { | ||
| // No live server — `start` has already published synchronously by this point. | ||
| } | ||
| try { | ||
| conn.destroy(); | ||
| } catch { | ||
| // ignore | ||
| } | ||
|
|
||
| const marker = (globalThis as { __SENTRY_ORCHESTRION__?: { runtime?: boolean; bundler?: boolean } }) | ||
| .__SENTRY_ORCHESTRION__; | ||
|
|
||
| setTimeout(() => { | ||
| // eslint-disable-next-line no-console | ||
| console.log(`SCENARIO events=${events.join(',')} statement=${statement} marker=${JSON.stringify(marker ?? null)}`); | ||
| process.exit(0); | ||
| }, 200); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { spawnSync } from 'child_process'; | ||
| import { rmSync } from 'fs'; | ||
| import { dirname, join } from 'path'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| const dir = __dirname; | ||
|
|
||
| function runBun(args: string[]): { stdout: string; stderr: string; status: number | null } { | ||
| const res = spawnSync('bun', args, { cwd: dir, encoding: 'utf8', timeout: 60_000 }); | ||
| return { stdout: res.stdout ?? '', stderr: res.stderr ?? '', status: res.status }; | ||
| } | ||
|
|
||
| // Bun orchestrion instrumentation is BUILD-ONLY (`@sentry/bun/plugin` is a | ||
| // `Bun.build` plugin; there is no `bun run` preload). | ||
| // | ||
| // A `bun run` runtime plugin cannot instrument CommonJS dependencies like | ||
| // `mysql`: any module returned by a runtime `onLoad` plugin in Bun loses its | ||
| // CommonJS named exports | ||
| // | ||
| // When https://github.com/oven-sh/bun/pull/31770 lands, we can revisit an | ||
| // auto-load plugin for `bun run`. | ||
| describe('orchestrion mysql instrumentation (Bun)', () => { | ||
| it('bundles `mysql` with the plugin, and the built output fires the mysql channel when run', () => { | ||
| // Build the scenario with the orchestrion `bun build` plugin. | ||
| const build = runBun(['run', join(dir, 'build.ts')]); | ||
| expect(build.status, `build failed:\nstderr:\n${build.stderr}\nstdout:\n${build.stdout}`).toBe(0); | ||
|
|
||
| const outfile = build.stdout.match(/BUILD_OK outfile=(.+)/)?.[1]?.trim(); | ||
| expect(outfile, `no outfile in build output:\n${build.stdout}`).toBeTruthy(); | ||
|
|
||
| try { | ||
| // Run the built bundle. The bundled (transformed) `mysql` should publish | ||
| // to the `orchestrion:mysql:query` channel when `connection.query()` is | ||
| // called, and the plugin's banner should set the `bundler` marker at boot. | ||
| const run = runBun(['run', outfile as string]); | ||
| expect(run.status, `run failed:\nstderr:\n${run.stderr}\nstdout:\n${run.stdout}`).toBe(0); | ||
|
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. Vitest timeout below build durationMedium Severity The new orchestrion test runs two Triggered by project rule: PR Review Guidelines for Cursor Bot Reviewed by Cursor Bugbot for commit 33c961c. Configure here. |
||
|
|
||
| const line = run.stdout.split('\n').find(l => l.startsWith('SCENARIO')) ?? ''; | ||
| // channel `start` fired on `connection.query()` | ||
| expect(line).toContain('events=start'); | ||
| // with the expected SQL | ||
| expect(line).toContain('statement=SELECT 1 AS solution'); | ||
| // injected banner ran at bundle boot | ||
| expect(line).toContain('"bundler":true'); | ||
| } finally { | ||
| if (outfile) { | ||
| rmSync(dirname(outfile), { recursive: true, force: true }); | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,10 @@ | ||
| import { makeBaseNPMConfig, makeNPMConfigVariants } from '@sentry-internal/rollup-utils'; | ||
|
|
||
| export default makeNPMConfigVariants(makeBaseNPMConfig()); | ||
| export default makeNPMConfigVariants( | ||
| makeBaseNPMConfig({ | ||
| // `src/plugin.ts` backs the `@sentry/bun/plugin` subpath (the orchestrion | ||
| // `bun build` plugin). It isn't reachable from `src/index.ts`, so we list it | ||
| // as a separate entrypoint to get both ESM and CJS builds. | ||
| entrypoints: ['src/index.ts', 'src/plugin.ts'], | ||
| }), | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /** | ||
| * orchestrion build plugin for Bun. Use with `Bun.build`: | ||
| * | ||
| * Usage: | ||
| * | ||
| * ```ts | ||
| * import { sentryBunPlugin } from '@sentry/bun/plugin'; | ||
| * await Bun.build({ | ||
| * entrypoints: ['./app.ts'], | ||
| * plugins: [sentryBunPlugin()], | ||
| * }); | ||
| * ``` | ||
| * | ||
| * This is BUILD-ONLY. Runtime instrumentation (`bun run`) is intentionally not | ||
| * offered: a module returned by a runtime `onLoad` plugin in Bun loses its | ||
| * CommonJS named exports. | ||
| * | ||
| * When https://github.com/oven-sh/bun/pull/31770 lands, we can revisit. | ||
| * | ||
| * Until then, Bun apps must bundle to get orchestrion instrumentation. In dev | ||
| * (ie, `bun run`) there is simply no instrumentation, which is clearer than | ||
| * partial/inconsistent coverage. | ||
| * | ||
| * Shipped as both ESM and CJS (via the `@sentry/bun/plugin` subpath) so a user's | ||
| * `bun build` script can be authored in either module system. It's a plain | ||
| * library import here (not a `--import`/`--preload` hook), so CJS is fine; Bun | ||
| * resolves the underlying ESM-only transformer in either module system. | ||
| * | ||
| * @module | ||
| */ | ||
| export { sentryBunPlugin } from '@sentry/server-utils/orchestrion/bun'; | ||
|
Member
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. l/m: why does this implementation need to live in the server-utils package? It seems to me it would make sense to have the platform-specific implementations in the respective packages, so the bun plugin in the sentry/bun package, the node one in sentry/node, etc?
Member
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. Isn't it better to have the actual implementation all in the same place?
Member
Author
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. Hm, I had put it there because I'd moved the But maybe that actually doesn't make sense? The Deno and Node runtime imports are in their respective packages (but they're also basically one-liners). So, yeah, I think it probably makes the most sense to move this into the Bun package, rather than having a basically empty re-export, and move the |
||


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.
Unneeded sleep in scenario test
Low Severity
The bundled scenario waits 200ms in
setTimeoutbefore logging and exiting, even though the file documents that the mysql channelstartevent runs synchronously onquery(). That fixed delay adds avoidable runtime and can contribute to timing-related flakes under load.Triggered by project rule: PR Review Guidelines for Cursor Bot
Reviewed by Cursor Bugbot for commit 33c961c. Configure here.