使用 Sentry
有关安装和配置 Sentry 以进行崩溃报告的指南。
Sentry 是一个崩溃报告平台,为你提供 实时了解生产环境部署情况的信息,以重现和修复崩溃。
它会在用户使用你的应用时通知你遇到的异常或错误,并在网页仪表板上为你进行整理。报告的异常包括堆栈跟踪、设备信息、版本以及其他相关上下文信息,且这些信息会自动收集。你还可以提供针对你的应用的额外上下文,例如当前路由和用户ID。
🌐 It notifies you of exceptions or errors that your users run into while using your app and organizes them for you on a web dashboard. Reported exceptions include stacktraces, device info, version, and other relevant context automatically. You can also provide additional context that is specific to your application such as the current route and user id.
你将学到什么
🌐 What you'll learn
本指南涵盖了将 Sentry 与你的 Expo 项目集成的三个主要方面:
🌐 This guide covers three main aspects of integrating Sentry with your Expo projects:
- 在你的 React Native 应用中安装和配置 Sentry
- 将 Sentry 与 EAS 结合使用:
- 设置 Sentry-Expo 集成,以便直接在你的 EAS 仪表板中查看崩溃报告和会话回放
安装和配置 Sentry
🌐 Install and configure Sentry
1
注册 Sentry 账户并创建一个项目
🌐 Sign up for a Sentry account and create a project
在继续安装 Sentry 之前,你需要确保已经创建了 Sentry 账户和项目:
🌐 Before proceeding with installing Sentry, you'll need to make sure you have created a Sentry account and project:
1.1
注册 Sentry(免费套餐每月支持最多 5,000 个事件),并在你的仪表板中创建一个项目。请记下你的 组织标识(organization slug)、项目名称(project name) 和 DSN,因为稍后你会用到它们:
- 组织标识 可在你的 组织设置 选项卡中使用
- 项目名称可以在你项目的设置 > 项目标签中找到(在列表中查找)
- DSN 可在你项目的 设置 > 项目 > 项目名称 > SDK 设置 部分 > 客户端密钥(DSN) 标签下找到。
1.2
前往 开发者设置 > 认证令牌 页面,并创建一个新的 组织认证令牌。该令牌会自动设置 Source Map 上传和版本创建的权限。请保存它。
🌐 Go to the Developer Settings > Auth Tokens page and create a new Organization Auth Token. The token is automatically scoped for Source Map Upload and Release Creation. Save it.
一旦你拥有以下每项内容:组织标识符、项目名称、DSN 和 认证令牌,你就可以开始操作了。
🌐 Once you have each of these: organization slug, project name, DSN, and auth token, you're all set to proceed.
2
使用 Sentry 向导来设置你的项目
🌐 Use the Sentry wizard to set up your project
在你的 Expo 项目中设置 Sentry 最简单的方法是使用 Sentry 向导。该工具会自动为你的项目配置正确的设置。
🌐 The easiest way to set up Sentry in your Expo project is to use the Sentry wizard. This tool will automatically configure your project with the right settings.
在你的项目目录中运行以下命令:
🌐 Run the following command in your project directory:
- npx @sentry/wizard@latest -i reactNative向导将会:
🌐 The wizard will:
- 安装所需的依赖
- 配置你的项目以使用 Sentry
- 自动设置 Metro 配置
- 向你的应用添加必要的初始化代码
按照向导中的提示完成设置过程。向导将引导你登录 Sentry 账户,并获取有关你的项目的所有正确信息。
🌐 Follow the prompts in the wizard to complete the setup process. The wizard will guide you to log in to your Sentry account and fetch all the correct information regarding your project.
3
验证配置
🌐 Verify the configuration
创建应用的新发布版本,并验证其是否正确上传了源映射。你可能希望在应用中添加一个按钮来测试其是否正常工作以及源映射是否按照预期连接,例如:
🌐 Create a new release build of your app and verify that it uploads source maps correctly. You may want to add a button in your app to test that it is working and sourcemaps are wired up as expected, for example:
import { Button } from 'react-native'; // Inside some component <Button title="按我" onPress={() => { throw new Error('Hello, again, Sentry!'); }}/>
与 EAS 构建一起使用
🌐 Usage with EAS Build
确保在你的构建环境中设置了 SENTRY_AUTH_TOKEN,Sentry 将会自动为你上传源映射。如果你使用环境变量而不是应用配置中的属性,请确保这些也已设置。
🌐 Ensure that SENTRY_AUTH_TOKEN is set in your build environment, and Sentry will automatically upload source maps for you. If you use environment variables rather than properties in your app config, ensure that those are set as well.
使用上述说明,在使用 EAS Build 时无需额外操作即可将 Sentry 集成到你的项目中。
🌐 Using the above instructions, no additional work is needed to integrate Sentry into your project when using EAS Build.
与 EAS 更新的使用
🌐 Usage with EAS Update
运行 eas update 后,将源映射上传到 Sentry:
🌐 After running eas update, upload the source maps to Sentry:
# Pass in the "dist" directory generated by `eas update` to the upload script- npx sentry-expo-upload-sourcemaps dist就是这样!你更新时的错误现在将在 Sentry 中正确显示符号信息。
🌐 That's it! Errors for your updates will now be properly symbolicated in Sentry.
你想要通过一个命令发布更新和源码映射吗?
你可以使用 && 将命令串联在一起,以在一步中发布更新并上传源映射:
🌐 You can chain the commands together with && to publish an update and upload the sourcemaps in one step:
- eas update --branch <branch> && npx sentry-expo-upload-sourcemaps dist你想要将额外的与更新相关的元数据附加到错误报告中吗?
将 Sentry 配置为在你的作用域中标记有关更新的信息,可以让你在 Sentry 仪表板上看到某些更新中发生的错误。
🌐 Configuring Sentry to tag your scope with information about your update allows you to see errors happening on certain updates in the Sentry dashboard.
尽早在应用生命周期的全局范围内添加以下代码片段。
🌐 Add the following snippet in the global scope as early as possible in your application's lifecycle.
import * as Sentry from '@sentry/react-native'; import * as Updates from 'expo-updates'; const manifest = Updates.manifest; const metadata = 'metadata' in manifest ? manifest.metadata : undefined; const extra = 'extra' in manifest ? manifest.extra : undefined; const updateGroup = metadata && 'updateGroup' in metadata ? metadata.updateGroup : undefined; Sentry.init({ // dsn, release, dist, etc... }); const scope = Sentry.getGlobalScope(); scope.setTag('expo-update-id', Updates.updateId); scope.setTag('expo-is-embedded-update', Updates.isEmbeddedLaunch); if (typeof updateGroup === 'string') { scope.setTag('expo-update-group-id', updateGroup); const owner = extra?.expoClient?.owner ?? '[account]'; const slug = extra?.expoClient?.slug ?? '[project]'; scope.setTag( 'expo-update-debug-url', `https://expo.dev/accounts/${owner}/projects/${slug}/updates/${updateGroup}` ); } else if (Updates.isEmbeddedLaunch) { // This will be `true` if the update is the one embedded in the build, and not one downloaded from the updates server. scope.setTag('expo-update-debug-url', 'not applicable for embedded updates'); }
配置完成后,相关更新的信息将显示在错误的标签部分:
🌐 Once configured, information about the associated update will show up in an error's tag section:
Sentry 与 EAS 仪表板集成
🌐 Sentry integration with EAS dashboard
Sentry 与 Expo 的集成允许你直接在 EAS 仪表板中查看你的 Expo 应用部署的崩溃报告和会话重播。此集成提供了指向 Sentry 堆栈跟踪的直接链接,包含完整的上下文、会话重播和调试功能。
🌐 The Sentry integration with Expo allows you to view crash reports and Session Replays for your Expo app deployments directly within your EAS dashboard. This integration provides a direct link to Sentry stack traces with full context, session replays, and debugging capabilities.
安装
🌐 Install
信息 安装此集成需要 Sentry 所有者、管理员或管理权限。
- 登录到你的 Expo 账户并打开 账户设置 > 概览。
- 在 连接 下,点击 Sentry 旁的 连接。
- 登录到你的 Sentry 账户并接受将其集成到你的组织中。你将被重定向回 账户设置。
链接你的项目
🌐 Link your project
连接账户后,你需要将你的 EAS 项目链接到你的 Sentry 项目:
🌐 After connecting your accounts, you need to link your EAS Project to your Sentry Project:
- 在 EAS 中打开 项目 > [你的项目] > 配置 > 项目设置。
- 点击 链接,然后从下拉菜单中选择你的 Sentry 项目。
用法
🌐 Usage
要在 EAS 仪表板中查看你的 Sentry 数据,请打开 Projects > [Your Project] > Updates > Deployments > [Deployment] 来查看某个 Release 的 Sentry 数据。
🌐 To see your Sentry data in EAS dashboard, open Projects > [Your Project] > Updates > Deployments > [Deployment] to view Sentry data from a Release.
通过此集成,你可以:
🌐 With this integration, you can:
- 直接在 EAS 仪表板中查看崩溃报告
- 访问会话回放,查看错误发生前的具体情况
- 获取包含完整上下文的详细堆栈跟踪
- 在 EAS 和 Sentry 之间无缝导航以进行调试
了解有关 Sentry 的更多信息
🌐 Learn more about Sentry
Sentry 不仅仅能捕捉致命错误,了解更多关于如何使用 Sentry 的信息,请参阅他们的 JavaScript 使用 文档。
🌐 Sentry does more than just catch fatal errors, learn more about how to use Sentry from their JavaScript usage documentation.