Build Sentry integrations that never take your application down via three pillars: safe initialization with graceful degradation, a circuit breaker that stops hammering Sentry when unreachable, and an offline event queue that buffers errors during outages. Every pattern prioritizes application uptime over telemetry completeness.
Prerequisites
@sentry/node v8+ (TypeScript) or sentry-sdk v2+ (Python)
A valid Sentry DSN from project settings at sentry.io
A fallback logging destination decided (console, file, or external logger)
Understanding of your application shutdown lifecycle (signal handlers, container orchestration)
Instructions
Step 1 — Safe Initialization with Graceful Degradation
Wrap Sentry.init() in try/catch so an invalid DSN, network error, or SDK bug never crashes the app. Track initialization state with a boolean flag. Protect beforeSend callbacks with their own error boundary.
Create lib/sentry-safe.ts with and . See for full implementation.
Never let Sentry.init() crash the process — wrap in try/catch, set sentryAvailable = false on failure
Verify client creation with Sentry.getClient() — invalid DSNs silently produce no client
Always log errors locally as baseline before attempting Sentry capture
Wrap user-supplied beforeSend hooks in nested try/catch — return raw event on hook failure
Step 2 — Circuit Breaker for Sentry Outages
When Sentry is unreachable, continued attempts waste resources and add latency. Track consecutive failures and trip open after a threshold. After cooldown, enter half-open state and send a single probe.
Implement SentryCircuitBreaker class with closed/open/half-open states. See circuit-breaker-pattern.md for full implementation. Expose state via health-checks.md endpoint.
Key rules:
Default: 5 failures to trip open, 60-second cooldown before half-open probe
In open state, skip Sentry calls entirely and log to fallback
On half-open success, reset to closed with zero failure count
Expose getStatus() for health check endpoints and monitoring dashboards
Step 3 — Offline Queue, Custom Transport, and Graceful Shutdown
Buffer events when network is unavailable and replay on reconnect. Use bounded file-based queue to survive restarts. Pair with signal handlers that flush via Sentry.close() before process exit.