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

# 将 Sentry 添加到 Bun 应用

[Sentry](https://sentry.io) 是一个错误跟踪和性能监控平台。其 Bun SDK `@sentry/bun` 可以检测你的应用程序，自动收集错误和性能数据。

如果你还没有 Sentry 账户和项目，请在 [sentry.io](https://sentry.io/signup/) 创建一个，然后返回此页面。

***

首先，安装 Sentry Bun SDK。

```sh terminal icon="terminal" theme={null}
bun add @sentry/bun
```

***

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

```ts sentry.ts icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/typescript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=787d39d6a7d96d7f9540dc74344eba23" theme={null}
import * as Sentry from "@sentry/bun";

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

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

***

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

```ts sentry.ts icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/typescript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=787d39d6a7d96d7f9540dc74344eba23" theme={null}
setTimeout(() => {
  try {
    foo();
  } catch (e) {
    Sentry.captureException(e);
  }
}, 99);
```

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

***

要了解更多关于 Sentry Bun SDK 的信息，请参见 [Sentry 文档](https://docs.sentry.io/platforms/javascript/guides/bun)。
