error-tracking

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Error Tracking

错误跟踪

Sentry (Node.js / Express)

Sentry (Node.js / Express)

typescript
import * as Sentry from '@sentry/node';

Sentry.init({
  dsn: process.env.SENTRY_DSN,
  environment: process.env.NODE_ENV,
  release: process.env.APP_VERSION,
  tracesSampleRate: process.env.NODE_ENV === 'production' ? 0.1 : 1.0,
  integrations: [Sentry.expressIntegration()],
});

// Must be first middleware
app.use(Sentry.expressErrorHandler());

// Add context
app.use((req, res, next) => {
  Sentry.setUser({ id: req.user?.id, email: req.user?.email });
  Sentry.setTag('tenant', req.tenantId);
  next();
});

// Manual capture
try {
  await riskyOperation();
} catch (error) {
  Sentry.captureException(error, {
    extra: { orderId, userId },
    tags: { component: 'payment' },
  });
}
typescript
import * as Sentry from '@sentry/node';

Sentry.init({
  dsn: process.env.SENTRY_DSN,
  environment: process.env.NODE_ENV,
  release: process.env.APP_VERSION,
  tracesSampleRate: process.env.NODE_ENV === 'production' ? 0.1 : 1.0,
  integrations: [Sentry.expressIntegration()],
});

// Must be first middleware
app.use(Sentry.expressErrorHandler());

// Add context
app.use((req, res, next) => {
  Sentry.setUser({ id: req.user?.id, email: req.user?.email });
  Sentry.setTag('tenant', req.tenantId);
  next();
});

// Manual capture
try {
  await riskyOperation();
} catch (error) {
  Sentry.captureException(error, {
    extra: { orderId, userId },
    tags: { component: 'payment' },
  });
}

Sentry (React)

Sentry (React)

tsx
import * as Sentry from '@sentry/react';

Sentry.init({
  dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
  environment: process.env.NODE_ENV,
  integrations: [Sentry.browserTracingIntegration(), Sentry.replayIntegration()],
  tracesSampleRate: 0.1,
  replaysSessionSampleRate: 0.1,
  replaysOnErrorSampleRate: 1.0,
});

// Error boundary
const SentryErrorBoundary = Sentry.withErrorBoundary(App, {
  fallback: <ErrorPage />,
  showDialog: true,
});

// Component-level
const ProfilePage = Sentry.withProfiler(Profile);
tsx
import * as Sentry from '@sentry/react';

Sentry.init({
  dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
  environment: process.env.NODE_ENV,
  integrations: [Sentry.browserTracingIntegration(), Sentry.replayIntegration()],
  tracesSampleRate: 0.1,
  replaysSessionSampleRate: 0.1,
  replaysOnErrorSampleRate: 1.0,
});

// Error boundary
const SentryErrorBoundary = Sentry.withErrorBoundary(App, {
  fallback: <ErrorPage />,
  showDialog: true,
});

// Component-level
const ProfilePage = Sentry.withProfiler(Profile);

Sentry (Python)

Sentry (Python)

python
import sentry_sdk
from sentry_sdk.integrations.fastapi import FastApiIntegration

sentry_sdk.init(
    dsn=os.environ["SENTRY_DSN"],
    environment=os.environ.get("ENV", "development"),
    traces_sample_rate=0.1,
    integrations=[FastApiIntegration()],
)
python
import sentry_sdk
from sentry_sdk.integrations.fastapi import FastApiIntegration

sentry_sdk.init(
    dsn=os.environ["SENTRY_DSN"],
    environment=os.environ.get("ENV", "development"),
    traces_sample_rate=0.1,
    integrations=[FastApiIntegration()],
)

Sentry (Spring Boot)

Sentry (Spring Boot)

xml
<dependency>
  <groupId>io.sentry</groupId>
  <artifactId>sentry-spring-boot-starter-jakarta</artifactId>
</dependency>
yaml
sentry:
  dsn: ${SENTRY_DSN}
  environment: ${SPRING_PROFILES_ACTIVE}
  traces-sample-rate: 0.1
xml
<dependency>
  <groupId>io.sentry</groupId>
  <artifactId>sentry-spring-boot-starter-jakarta</artifactId>
</dependency>
yaml
sentry:
  dsn: ${SENTRY_DSN}
  environment: ${SPRING_PROFILES_ACTIVE}
  traces-sample-rate: 0.1

Source Maps Upload (CI/CD)

Source Maps 上传 (CI/CD)

yaml
undefined
yaml
undefined

GitHub Actions

GitHub Actions

  • name: Upload source maps to Sentry run: | npx @sentry/cli sourcemaps upload
    --release=${{ github.sha }}
    --org=my-org --project=my-app
    ./dist env: SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
undefined
  • name: Upload source maps to Sentry run: | npx @sentry/cli sourcemaps upload
    --release=${{ github.sha }}
    --org=my-org --project=my-app
    ./dist env: SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
undefined

Breadcrumbs

面包屑(Breadcrumbs)

typescript
Sentry.addBreadcrumb({
  category: 'payment',
  message: `Processing payment for order ${orderId}`,
  level: 'info',
  data: { orderId, amount },
});
typescript
Sentry.addBreadcrumb({
  category: 'payment',
  message: `Processing payment for order ${orderId}`,
  level: 'info',
  data: { orderId, amount },
});

Anti-Patterns

反模式

Anti-PatternFix
100% trace sample rate in productionUse 0.1-0.2 for production
No source maps uploadedUpload in CI/CD for readable stack traces
No release trackingSet
release
to git SHA or version
Capturing expected errorsOnly capture unexpected errors
No user contextSet user ID/email for debugging
PII in error dataScrub sensitive fields in
beforeSend
反模式修复方案
生产环境trace采样率100%生产环境使用0.1-0.2的采样率
未上传source maps在CI/CD流程中上传以获得可读的堆栈跟踪
无版本追踪
release
设置为git SHA或应用版本号
捕获预期内的错误仅捕获非预期的错误
无用户上下文设置用户ID/邮箱方便调试
错误数据中包含PII
beforeSend
中清理敏感字段

Production Checklist

生产环境检查清单

  • DSN configured per environment
  • Source maps uploaded in CI/CD
  • Release version set to git SHA
  • Sample rate tuned (0.1-0.2 for production)
  • User context attached
  • Alert rules configured for error spikes
  • PII scrubbing enabled
  • 已按环境配置DSN
  • 已在CI/CD中上传source maps
  • 已将release版本设置为git SHA
  • 已调整采样率(生产环境设为0.1-0.2)
  • 已附加用户上下文
  • 已配置错误突增的告警规则
  • 已启用PII敏感信息清理