adding-error-tracking
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAdd Error Tracking (Sentry)
添加错误追踪(Sentry)
Use this skill when the user asks to add error tracking, crash reporting, exception monitoring, or performance tracing.
当用户要求添加错误追踪、崩溃报告、异常监控或性能追踪时,使用此技能。
Steps
步骤
-
Detect the framework — check, config files, and directory structure to determine the stack (Next.js, React, Node.js, Python/Django/Flask, etc.).
package.json -
Install the SDK
- Next.js:
npx @sentry/wizard@latest -i nextjs - React (Vite):
npm install @sentry/react - Node.js:
npm install @sentry/node - Python:
pip install sentry-sdk
- Next.js:
-
Initialize Sentry
-
For Next.js, the wizard creates,
sentry.client.config.ts, andsentry.server.config.ts. Verify they exist and contain the DSN.sentry.edge.config.ts -
For React (Vite), init in:
main.tsxtsximport * as Sentry from "@sentry/react"; Sentry.init({ dsn: import.meta.env.VITE_SENTRY_DSN, integrations: [Sentry.browserTracingIntegration()], tracesSampleRate: 1.0, }); -
For Node.js, init at the very top of the entry file before any other imports.
-
-
Add environment variables
NEXT_PUBLIC_SENTRY_DSN=https://...@sentry.io/... SENTRY_AUTH_TOKEN=sntrys_... SENTRY_ORG=your-org SENTRY_PROJECT=your-project -
Add error boundary — wrap the app (or critical subtrees) withand a fallback UI.
Sentry.ErrorBoundary -
Source maps — for production builds, configure the Sentry webpack/vite plugin to upload source maps. For Next.js, this is handled byin
withSentryConfig.next.config.js -
Test the integration — add a temporary button that throws an error to verify events appear in the Sentry dashboard.
-
检测框架 —— 检查、配置文件和目录结构以确定技术栈(Next.js、React、Node.js、Python/Django/Flask等)。
package.json -
安装SDK
- Next.js:
npx @sentry/wizard@latest -i nextjs - React (Vite):
npm install @sentry/react - Node.js:
npm install @sentry/node - Python:
pip install sentry-sdk
- Next.js:
-
初始化Sentry
-
对于Next.js,向导会创建、
sentry.client.config.ts和sentry.server.config.ts。验证这些文件是否存在并包含DSN。sentry.edge.config.ts -
对于React (Vite),在中初始化:
main.tsxtsximport * as Sentry from "@sentry/react"; Sentry.init({ dsn: import.meta.env.VITE_SENTRY_DSN, integrations: [Sentry.browserTracingIntegration()], tracesSampleRate: 1.0, }); -
对于Node.js,在入口文件的最顶部、任何其他导入之前进行初始化。
-
-
添加环境变量
NEXT_PUBLIC_SENTRY_DSN=https://...@sentry.io/... SENTRY_AUTH_TOKEN=sntrys_... SENTRY_ORG=your-org SENTRY_PROJECT=your-project -
添加错误边界 —— 使用包裹应用(或关键子树)并设置回退UI。
Sentry.ErrorBoundary -
源映射 —— 对于生产构建,配置Sentry webpack/vite插件以上传源映射。对于Next.js,此操作由中的
next.config.js处理。withSentryConfig -
测试集成 —— 添加一个临时按钮来抛出错误,验证事件是否出现在Sentry仪表盘中。
Notes
注意事项
- Set to a lower value (e.g.
tracesSampleRate) in production to control costs.0.1 - Add and
sentry.propertiesto.sentryclirc..gitignore - Never commit .
SENTRY_AUTH_TOKEN
- 在生产环境中将设置为较低的值(例如
tracesSampleRate)以控制成本。0.1 - 将和
sentry.properties添加到.sentryclirc中。.gitignore - 切勿提交。
SENTRY_AUTH_TOKEN