sentry-sdk-configuration
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSentry - SDK Configuration
Sentry - SDK配置
Initialize and configure Sentry SDKs across different platforms and frameworks.
在不同平台和框架中初始化并配置Sentry SDK。
JavaScript/TypeScript
JavaScript/TypeScript
Browser SDK
Browser SDK
typescript
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(),
],
});typescript
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
Node.js SDK
typescript
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(),
],
});typescript
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
Next.js SDK
typescript
// 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,
});typescript
// sentry.server.config.ts
import * as Sentry from "@sentry/nextjs";
Sentry.init({
dsn: process.env.SENTRY_DSN,
tracesSampleRate: 1.0,
});typescript
// 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,
});typescript
// sentry.server.config.ts
import * as Sentry from "@sentry/nextjs";
Sentry.init({
dsn: process.env.SENTRY_DSN,
tracesSampleRate: 1.0,
});React SDK
React SDK
tsx
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,
});
// Wrap your app
const App = () => (
<Sentry.ErrorBoundary fallback={<ErrorFallback />}>
<YourApp />
</Sentry.ErrorBoundary>
);tsx
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
Python SDK
python
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,
)python
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 Integration
Django集成
python
undefinedpython
undefinedsettings.py
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,
)
undefinedimport 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,
)
undefinedGo SDK
Go SDK
go
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)
}go
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)
}Configuration Options
配置选项
Sample Rates
采样率
typescript
Sentry.init({
// Error sampling (1.0 = 100%)
sampleRate: 1.0,
// Transaction/trace sampling
tracesSampleRate: 0.2,
// Or use a sampler function
tracesSampler: (samplingContext) => {
if (samplingContext.name.includes("/health")) {
return 0; // Don't trace health checks
}
return 0.2;
},
});typescript
Sentry.init({
// 错误采样(1.0 = 100%)
sampleRate: 1.0,
// 事务/追踪采样
tracesSampleRate: 0.2,
// 或使用采样器函数
tracesSampler: (samplingContext) => {
if (samplingContext.name.includes("/health")) {
return 0; // 不追踪健康检查
}
return 0.2;
},
});Filtering Events
过滤事件
typescript
Sentry.init({
beforeSend(event, hint) {
// Filter out specific errors
if (event.exception?.values?.[0]?.type === "NetworkError") {
return null;
}
return event;
},
ignoreErrors: [
"ResizeObserver loop limit exceeded",
/^Script error\.?$/,
],
denyUrls: [
/extensions\//i,
/^chrome:\/\//i,
],
});typescript
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,
],
});Best Practices
最佳实践
- Always set and
environmentrelease - Use environment variables for DSN
- Configure appropriate sample rates for production
- Filter noisy or irrelevant errors
- Use framework-specific integrations
- Initialize Sentry as early as possible
- 始终设置和
environmentrelease - 使用环境变量存储DSN
- 为生产环境配置合适的采样率
- 过滤无意义或不相关的错误
- 使用框架特定的集成
- 尽早初始化Sentry