> ## Documentation Index
> Fetch the complete documentation index at: https://bun.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 在 Bun 应用中添加 Sentry

[Sentry](https://sentry.io) 是一个以开发者为中心的错误跟踪和性能监控平台。Sentry 提供了一个针对 Bun 的一流 SDK——`@sentry/bun`，它可以为你的 Bun 应用自动收集错误和性能数据。

还没有账号或 Sentry 项目？请前往 [sentry.io](https://sentry.io/signup/)，然后再返回本页。

***

要开始在 Bun 中使用 Sentry，首先安装 Sentry Bun SDK。

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun add @sentry/bun
```

***

接着，在应用入口文件中使用你的 Sentry DSN 初始化 Sentry SDK。你可以在 Sentry 项目设置中找到你的 DSN。

```ts sentry.ts icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217" theme={"theme":{"light":"github-light","dark":"dracula"}}
import * as Sentry from "@sentry/bun";

// 确保在导入任何其他模块之前调用此方法！
Sentry.init({
  dsn: "__SENTRY_DSN__",

  // 通过设置 tracesSampleRate 添加性能监控
  // 我们建议在生产环境中调整该值
  tracesSampleRate: 1.0,
});
```

***

你可以通过捕获一个测试错误来验证 Sentry 是否正常工作：

```ts sentry.ts icon="https://mintcdn.com/bun-zhcndoc/cnUTwgMuf4cCrwC-/icons/typescript.svg?fit=max&auto=format&n=cnUTwgMuf4cCrwC-&q=85&s=e7767043c9e885c34f2d6c8fe2a95217" theme={"theme":{"light":"github-light","dark":"dracula"}}
setTimeout(() => {
  try {
    foo();
  } catch (e) {
    Sentry.captureException(e);
  }
}, 99);
```

要查看并解决记录的错误，请登录 [sentry.io](https://sentry.io/) 并打开你的项目。点击错误标题将打开一个页面，显示详细信息并可将其标记为已解决。

***

想了解更多关于 Sentry 及使用 Sentry Bun SDK 的信息，请查看 [Sentry 文档](https://docs.sentry.io/platforms/javascript/guides/bun)。
