Skip to main content

Add Sentry to a Bun app

还没翻译译自英文(bun.com)本页 9 段都还没译

Sentry is an error tracking and performance monitoring platform. Its Bun SDK, @sentry/bun, instruments your application to automatically collect error and performance data.

If you don't have a Sentry account and project yet, create one at sentry.io, then return to this page.


First, install the Sentry Bun SDK.

bun add @sentry/bun

Then initialize the SDK with your Sentry DSN in its own file. You can find your DSN in your Sentry project settings.

import * as Sentry from "@sentry/bun";

// Ensure to call this before importing any other modules!
Sentry.init({
  dsn: "__SENTRY_DSN__",

  // Add Performance Monitoring by setting tracesSampleRate
  // We recommend adjusting this value in production
  tracesSampleRate: 1.0,
});

Start your app with --preload so this file runs before any of your app's modules. Bun evaluates a file's imports before its own code, so calling Sentry.init() at the top of your entry file would still run after everything that file imports.

bun --preload ./sentry.ts index.ts

Verify that Sentry is working by capturing a test error:

setTimeout(() => {
  try {
    foo();
  } catch (e) {
    Sentry.captureException(e);
  }
}, 99);

To view and resolve the recorded error, log into sentry.io and open your project. Clicking the error's title opens a page with details, where you can mark it as resolved.


To learn more about the Sentry Bun SDK, see the Sentry documentation.

翻訳 © 2026 fanyi.dev · 原文 © 2026 the Bun project (LICENSE.md names no copyright holder) (MIT)