SentrySDK配置Skill sentry-sdk-configuration

Sentry SDK配置技能用于初始化和配置Sentry SDK,以监控应用程序错误和性能。它包括设置数据源名称(DSN)、环境变量、采样率、集成框架等,适用于前端、后端、移动开发等多平台。关键词:Sentry, SDK配置, 错误监控, 性能监控, 应用监控, 前端开发, 后端开发, DevOps, 集成框架。

DevOps 0 次安装 0 次浏览 更新于 3/25/2026

名称:sentry-sdk-configuration 描述:用于在应用程序中初始化Sentry、配置SDK选项或在不同框架和平台中设置集成。 允许的工具:

  • Read
  • Write
  • Edit
  • Bash
  • Grep
  • Glob

Sentry - SDK配置

初始化和配置不同平台和框架中的Sentry SDK。

JavaScript/TypeScript

浏览器SDK

import * as Sentry from "@sentry/browser";

Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  environment: process.env.NODE_ENV,
  release: process.env.RELEASE_VERSION,
  tracesSampleRate: 1.0,
  replaysSessionSampleRate: 0.1,
  replaysOnErrorSampleRate: 1.0,
  integrations: [
    Sentry.browserTracingIntegration(),
    Sentry.replayIntegration(),
  ],
});

Node.js SDK

import * as Sentry from "@sentry/node";

Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  environment: process.env.NODE_ENV,
  release: process.env.RELEASE_VERSION,
  tracesSampleRate: 1.0,
  integrations: [
    Sentry.httpIntegration(),
    Sentry.expressIntegration(),
  ],
});

Next.js SDK

// sentry.client.config.ts
import * as Sentry from "@sentry/nextjs";

Sentry.init({
  dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
  tracesSampleRate: 1.0,
  replaysSessionSampleRate: 0.1,
  replaysOnErrorSampleRate: 1.0,
});
// sentry.server.config.ts
import * as Sentry from "@sentry/nextjs";

Sentry.init({
  dsn: process.env.SENTRY_DSN,
  tracesSampleRate: 1.0,
});

React SDK

import * as Sentry from "@sentry/react";

Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  integrations: [
    Sentry.browserTracingIntegration(),
    Sentry.reactRouterV6BrowserTracingIntegration({
      useEffect,
      useLocation,
      useNavigationType,
      createRoutesFromChildren,
      matchRoutes,
    }),
  ],
  tracesSampleRate: 1.0,
});

// 包装你的应用
const App = () => (
  <Sentry.ErrorBoundary fallback={<ErrorFallback />}>
    <YourApp />
  </Sentry.ErrorBoundary>
);

Python SDK

import sentry_sdk

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    environment=os.getenv("ENVIRONMENT"),
    release=os.getenv("RELEASE_VERSION"),
    traces_sample_rate=1.0,
    profiles_sample_rate=1.0,
)

Django集成

# settings.py
import sentry_sdk

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    integrations=[
        sentry_sdk.integrations.django.DjangoIntegration(),
    ],
    traces_sample_rate=1.0,
    send_default_pii=True,
)

Go SDK

import "github.com/getsentry/sentry-go"

func main() {
    err := sentry.Init(sentry.ClientOptions{
        Dsn:              "https://examplePublicKey@o0.ingest.sentry.io/0",
        Environment:      os.Getenv("ENVIRONMENT"),
        Release:          os.Getenv("RELEASE_VERSION"),
        TracesSampleRate: 1.0,
    })
    if err != nil {
        log.Fatalf("sentry.Init: %s", err)
    }
    defer sentry.Flush(2 * time.Second)
}

配置选项

采样率

Sentry.init({
  // 错误采样(1.0 = 100%)
  sampleRate: 1.0,

  // 事务/跟踪采样
  tracesSampleRate: 0.2,

  // 或使用采样器函数
  tracesSampler: (samplingContext) => {
    if (samplingContext.name.includes("/health")) {
      return 0; // 不要跟踪健康检查
    }
    return 0.2;
  },
});

过滤事件

Sentry.init({
  beforeSend(event, hint) {
    // 过滤特定错误
    if (event.exception?.values?.[0]?.type === "NetworkError") {
      return null;
    }
    return event;
  },
  ignoreErrors: [
    "ResizeObserver loop limit exceeded",
    /^Script error\.?$/,
  ],
  denyUrls: [
    /extensions\//i,
    /^chrome:\/\//i,
  ],
});

最佳实践

  1. 始终设置 environmentrelease
  2. 使用环境变量作为DSN
  3. 为生产环境配置适当的采样率
  4. 过滤嘈杂或不相关的错误
  5. 使用特定于框架的集成
  6. 尽可能早地初始化Sentry