Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,9 @@ jobs:
strategy:
fail-fast: false
matrix:
# 1.3.14 is the oldest supported Bun version
bun: ['1.3.14', '1.4']
# 1.3.14 is the oldest supported Bun version. Canary runs the `bun run` instrumentation
# suites (`node-suites-bun-run`), which need Bun 1.4.5 or later.
bun: ['1.3.14', '1.4', 'canary']
shard: [1, 2, 3, 4]
steps:
- name: Check out current commit (${{ needs.job_get_metadata.outputs.commit_label }})
Expand Down
5 changes: 5 additions & 0 deletions dev-packages/bun-integration-tests/node-suites/bun-run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Preloaded by the `node-suites-bun-run` project: maps `@sentry/node` to `@sentry/bun`, and
// registers the `bun run` instrumentation before the scenario loads, as users do with
// `bun --preload @sentry/bun/import`.
import './alias-sentry-bun';
import '@sentry/bun/import';
31 changes: 29 additions & 2 deletions dev-packages/bun-integration-tests/node-suites/excludes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ const NO_FETCH_INSTRUMENTATION = [
'suites/tracing/requests/traceparent/test.ts',
];

// `bun run` cannot inject the diagnostics channels into libraries, so framework, database and AI
// instrumentation creates no spans. Apps must be built with `@sentry/bun/plugin`.
// Before Bun 1.4.5, `bun run` cannot inject the diagnostics channels into libraries, so framework,
// database and AI instrumentation creates no spans. Apps must be built with `@sentry/bun/plugin`.
// The `node-suites-bun-build` and `node-suites-bun-run` projects run these suites.
// See https://github.com/getsentry/sentry-javascript/issues/23882
export const NO_AUTO_INSTRUMENTATION = [
'suites/cron/cron-integration/test.ts',
Expand Down Expand Up @@ -275,3 +276,29 @@ export const BUN_BUILD_EXCLUDE = [
...BUN_BUILD_UNBUNDLEABLE_DEPENDENCY,
...BUN_BUILD_NOT_TRIAGED,
];

// The `bun run` project (`node-suites-bun-run`) runs the suites of `NO_AUTO_INSTRUMENTATION` with
// `@sentry/bun/import` preloaded, on Bun 1.4.5 or later. These do not run there.

// The expected spans have `sentry.javascript.node` as the SDK name.
const BUN_RUN_EXPECTS_SENTRY_NODE = ['suites/tracing/mongodb/test.ts', 'suites/tracing/mysql/test.ts'];

// Some or all tests fail under `bun run`. The cause is not investigated yet. They also fail with the
// bundled scenarios.
const BUN_RUN_NOT_TRIAGED = [
'suites/express/tracing/request-data/test.ts',
'suites/express/with-http/maxIncomingRequestBodySize/test.ts',
'suites/hono-sdk/test.ts',
'suites/pino/test.ts',
];

export const BUN_RUN_EXCLUDE = [
'**/node_modules/**',
// SQL Server runs in the dedicated Node/x86 CI job.
'suites/tracing/tedious/test.ts',
// These fail for the same reasons as with the bundled scenarios.
...BUN_BUILD_EAGER_SUBSCRIPTION,
...BUN_BUILD_NO_HTTP_SERVER_INTEGRATION,
...BUN_RUN_EXPECTS_SENTRY_NODE,
...BUN_RUN_NOT_TRIAGED,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as Sentry from '@sentry/bun';

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
tracesSampleRate: 1.0,
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ import * as Sentry from '@sentry/bun';
Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
tracesSampleRate: 1.0,
// Bun 1.4.5 and later inject the channels in `bun run`, unless this is `false`.
enableRuntimeChannelInjection: false,
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { spawnSync } from 'child_process';
import { join } from 'path';
import { describe, expect, it } from 'vitest';
import { supports } from '../../../node-integration-tests/utils';

function runScenario(file: string): string {
const res = spawnSync('bun', ['run', join(__dirname, file)], { cwd: __dirname, encoding: 'utf8' });
Expand All @@ -9,13 +10,17 @@ function runScenario(file: string): string {
}

describe('no channel injection (Bun)', () => {
it('warns without `debug` when the app runs unbuilt with `bun run`', () => {
it('warns without `debug` when the app runs unbuilt with `bun run` and no channels are injected', () => {
const output = runScenario('scenario.ts');

expect(output).toContain('No diagnostics-channel injection detected');
expect(output).toContain('@sentry/bun/plugin');
});

it.runIf(supports({ bunMin: '1.4.5' }))('does not warn when `init()` injects the channels in `bun run`', () => {
expect(runScenario('scenario-runtime-injection.ts')).not.toContain('No diagnostics-channel injection detected');
});

it('does not warn when no channel integration is installed', () => {
expect(runScenario('scenario-no-integrations.ts')).not.toContain('No diagnostics-channel injection detected');
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Bundled entry for the `bun build` smoke test.
// Entry for the `bun build` and `bun run` smoke tests.
//
// Once `Bun.build` (with the orchestrion plugin) has transformed `mysql`,
// calling `connection.query()` publishes to the `orchestrion:mysql:query`
// tracing channel.
// Once the orchestrion plugin has transformed `mysql` (in `Bun.build`, or in
// `bun run` with `Bun.plugin()`), 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).
// (plus the detection marker).

import { tracingChannel } from 'node:diagnostics_channel';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { spawnSync } from 'child_process';
import { rmSync } from 'fs';
import { dirname, join } from 'path';
import { describe, expect, it } from 'vitest';
import { supports } from '../../../node-integration-tests/utils';

const dir = __dirname;

Expand All @@ -16,16 +17,23 @@ function runBun(args: string[]): { stdout: string; stderr: string; status: numbe
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)', () => {
// Before Bun 1.4.5, a CommonJS module like `mysql` loses its named exports
// when a runtime `onLoad` plugin returns it, so `bun run` cannot load it.
it.runIf(supports({ bunMin: '1.4.5' }))(
'fires the mysql channel in `bun run` with the `@sentry/bun/import` preload',
() => {
const run = runBun(['--preload', '@sentry/bun/import', join(dir, 'scenario.ts')]);
expect(run.status, `run failed:\nstderr:\n${run.stderr}\nstdout:\n${run.stdout}`).toBe(0);

const line = run.stdout.split('\n').find(l => l.startsWith('SCENARIO')) ?? '';
expect(line).toContain('events=start');
expect(line).toContain('statement=SELECT 1 AS solution');
expect(line).toContain('"runtime":["mysql"]');
},
SUBPROCESS_TIMEOUT_MS,
);

it(
'bundles `mysql` with the plugin, and the built output fires the mysql channel when run',
() => {
Expand Down
34 changes: 33 additions & 1 deletion dev-packages/bun-integration-tests/vite.config.mts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { execFileSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { defineConfig } from 'vitest/config';
import baseConfig from '../../vite/vite.config';
import {
BUN_BUILD_EXCLUDE,
BUN_RUN_EXCLUDE,
NO_AUTO_INSTRUMENTATION,
NODE_SUITES_EXCLUDE,
SENTRY_BUN_EXCLUDE,
Expand All @@ -14,6 +16,17 @@ const NODE_SUITES_ROOT = fileURLToPath(new URL('../node-integration-tests', impo
// All Node suites also run on Bun. The scenarios stay in `node-integration-tests`.
const NODE_SUITES = ['suites/**/test.ts'];

// Before Bun 1.4.5, `bun run` cannot load the modules that the instrumentation transforms.
const [bunMajor = 0, bunMinor = 0, bunPatch = 0] = execFileSync('bun', ['--version'], { encoding: 'utf8' })
.trim()
.split(/[.-]/)
.map(Number);
const BUN_RUN_INSTRUMENTS = bunMajor > 1 || (bunMajor === 1 && (bunMinor > 4 || (bunMinor === 4 && bunPatch >= 5)));

const AUTO_INSTRUMENTATION_SUITES = NO_AUTO_INSTRUMENTATION.map(glob =>
glob.endsWith('/**') ? `${glob}/test.ts` : glob,
);

const nodeSuitesTest = {
root: NODE_SUITES_ROOT,
include: NODE_SUITES,
Expand Down Expand Up @@ -91,7 +104,7 @@ export default defineConfig({
// it starts, as Bun apps must be built to get these spans.
// See https://github.com/getsentry/sentry-javascript/issues/23882
name: 'node-suites-bun-build',
include: NO_AUTO_INSTRUMENTATION.map(glob => (glob.endsWith('/**') ? `${glob}/test.ts` : glob)),
include: AUTO_INSTRUMENTATION_SUITES,
exclude: BUN_BUILD_EXCLUDE,
env: {
RUNTIME: 'bun',
Expand All @@ -101,6 +114,25 @@ export default defineConfig({
},
},
},
...(BUN_RUN_INSTRUMENTS
? [
{
extends: true,
test: {
...nodeSuitesTest,
// The auto-instrumentation suites, run unbuilt with `@sentry/bun/import` preloaded.
name: 'node-suites-bun-run',
include: AUTO_INSTRUMENTATION_SUITES,
exclude: BUN_RUN_EXCLUDE,
env: {
RUNTIME: 'bun',
RUNTIME_PRELOAD: fileURLToPath(new URL('./node-suites/bun-run.ts', import.meta.url)),
EXPECTED_SDK_NAME: 'sentry.javascript.bun',
},
},
},
]
: []),
],
},
});
1 change: 1 addition & 0 deletions dev-packages/bundler-tests/tests/runtime-entries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const CASES: Case[] = [
{ specifier: '@sentry/node', conditions: NODE, formats: BOTH },
{ specifier: '@sentry/node/import', conditions: NODE, formats: ESM },
{ specifier: '@sentry/bun', conditions: ['bun', 'node'], formats: BOTH },
{ specifier: '@sentry/bun/import', conditions: ['bun', 'node'], formats: ESM },
{ specifier: '@sentry/deno', conditions: ['deno', 'node'], formats: ESM },
{ specifier: '@sentry/vercel-edge', conditions: ['edge-light'], formats: BOTH },
{ specifier: '@sentry/cloudflare', conditions: WORKERD, formats: BOTH },
Expand Down
4 changes: 2 additions & 2 deletions dev-packages/e2e-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ is the reference setup.
itself does not get these files, so run a variant with `pnpm run test:run <app> --variant <runtime>`.
- **Bun and Deno versions**: a variant can pin the Bun or Deno version, for example `"bun-version": "1.4.4"` or
`"deno-version": "v2.9.0"`.
- **Bun**: under `bun run` the SDK cannot inject diagnostics channels into packages that stay outside the build (for
example Express behind `react-router-serve`), so those produce no spans on Bun. Where a test depends on them, branch
- **Bun**: under `bun run` before Bun 1.4.5 the SDK cannot inject diagnostics channels into packages that stay outside
the build (for example Express behind `react-router-serve`), so those produce no spans on Bun. Where a test depends on them, branch
on `RUNTIME` and say why in a comment. An app that starts its own server can instead bundle it with
`@sentry/bun/plugin`, as `nestjs-12/bun-build.mjs` does. Bun 1.3 renames classes in bundles (`class Foo2`), which
changes span names taken from class names, so `nestjs-12` pins Bun 1.4 (see #26289). `nuxt-4` builds with
Expand Down
7 changes: 6 additions & 1 deletion packages/bun/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"types": "./build/types/plugin.d.ts",
"default": "./build/cjs/plugin.js"
}
},
"./import": {
"import": "./build/import.mjs"
}
},
"publishConfig": {
Expand Down Expand Up @@ -70,5 +73,7 @@
"test:watch": "bun test --watch",
"yalc:publish": "pnpm dlx yalc publish --push --sig"
},
"sideEffects": false
"sideEffects": [
"./build/import.mjs"
]
}
28 changes: 20 additions & 8 deletions packages/bun/rollup.npm.config.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import { defineConfig } from 'rolldown';
import { makeBaseNPMConfig, makeNPMConfigVariants } from '@sentry-internal/rollup-utils';

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'],
}),
);
// The `@sentry/bun/import` entry (`bun --preload @sentry/bun/import app.ts`), which registers
// the orchestrion diagnostics-channel injection before the app loads.
const orchestrionRuntimeHook = defineConfig({
input: 'src/import.mjs',
external: /.*/,
output: { format: 'esm', file: 'build/import.mjs' },
});

export default [
orchestrionRuntimeHook,
...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'],
}),
),
];
17 changes: 17 additions & 0 deletions packages/bun/src/import.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Orchestrion runtime hook for Bun 1.4.5 and later.
*
* `init()` registers the same hook, but it only applies to modules imported
* after `init()`. Load this via the `--preload` argument to `bun run`, or the
* `preload` option in `bunfig.toml`, to also instrument modules imported
* before it, such as static imports in the entry module.
*
* For example:
*
* ```bash
* $ bun --preload @sentry/bun/import app.ts
* ```
*
* @module
*/
import '@sentry/node/import';
2 changes: 1 addition & 1 deletion packages/bun/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* });
* ```
*
* This is BUILD-ONLY. Runtime instrumentation (`bun run`) is currently not supported.
* For `bun run` on Bun 1.4.5 or later, preload `@sentry/bun/import` instead.
*
* @module
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/bun/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import { getErrorIntegrations, getTracingIntegrations } from '@sentry/server-uti
/**
* The tracing integrations for bun, added whenever spans are enabled. Most of them listen on
* the orchestrion diagnostics channels, which only exist when the app is built with
* `@sentry/bun/plugin`. Without the plugin, those integrations stay installed but create no spans.
* `@sentry/bun/plugin`, or runs in `bun run` on Bun 1.4.5 or later. Otherwise, those
* integrations stay installed but create no spans.
*/
function getPerformanceIntegrations(options: Options): Integration[] {
if (!hasSpansEnabled(options)) {
Expand Down
11 changes: 11 additions & 0 deletions packages/bun/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ export interface BaseBunOptions extends ServerRuntimeOptions {
*/
enableOpenTelemetrySetup?: boolean;

/**
* Controls whether the SDK instruments supported modules (e.g. Express) when they load in
* `bun run`, so they emit the diagnostics channels that the channel-based integrations
* subscribe to. Needs Bun 1.4.5 or later.
*
* Set this to `false` to opt out, for example when you build your app with `@sentry/bun/plugin`.
*
* @default true
*/
enableRuntimeChannelInjection?: boolean;

/**
* Override the runtime name reported in events.
* Defaults to 'bun' with the current Bun version if not specified.
Expand Down
Loading
Loading