feat: add Sentry error monitoring and tracing to FastAPI backend
- Install sentry-sdk[fastapi] with auto-enabled FastAPI + Anthropic integrations - Init before app = FastAPI() with env-aware sample rates (100% dev, 20% prod) - Filter /health endpoint from traces to reduce noise - Add SENTRY_DSN to config settings Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -118,6 +118,9 @@ class Settings(BaseSettings):
|
|||||||
"""Check if any AI provider is configured."""
|
"""Check if any AI provider is configured."""
|
||||||
return self.ANTHROPIC_API_KEY is not None or self.GOOGLE_AI_API_KEY is not None
|
return self.ANTHROPIC_API_KEY is not None or self.GOOGLE_AI_API_KEY is not None
|
||||||
|
|
||||||
|
# Monitoring
|
||||||
|
SENTRY_DSN: Optional[str] = None
|
||||||
|
|
||||||
# Deployment – auto-seed test data on PR environments
|
# Deployment – auto-seed test data on PR environments
|
||||||
SEED_ON_DEPLOY: bool = False
|
SEED_ON_DEPLOY: bool = False
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,21 @@ from slowapi import _rate_limit_exceeded_handler
|
|||||||
from slowapi.errors import RateLimitExceeded
|
from slowapi.errors import RateLimitExceeded
|
||||||
|
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
|
|
||||||
|
# Initialize Sentry before any other app code
|
||||||
|
import sentry_sdk
|
||||||
|
if settings.SENTRY_DSN:
|
||||||
|
sentry_sdk.init(
|
||||||
|
dsn=settings.SENTRY_DSN,
|
||||||
|
environment="development" if settings.DEBUG else "production",
|
||||||
|
send_default_pii=True,
|
||||||
|
traces_sample_rate=1.0 if settings.DEBUG else 0.2,
|
||||||
|
# Filter out noisy health check transactions
|
||||||
|
traces_sampler=lambda ctx: (
|
||||||
|
0.0 if ctx.get("transaction_context", {}).get("name", "").startswith("GET /health") else None
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
from app.core.database import init_db, async_session_maker
|
from app.core.database import init_db, async_session_maker
|
||||||
from app.core.logging_config import setup_logging
|
from app.core.logging_config import setup_logging
|
||||||
from app.core.middleware import RequestLoggingMiddleware, ErrorLoggingMiddleware
|
from app.core.middleware import RequestLoggingMiddleware, ErrorLoggingMiddleware
|
||||||
|
|||||||
@@ -39,6 +39,9 @@ google-genai>=1.0.0
|
|||||||
pgvector>=0.3.6
|
pgvector>=0.3.6
|
||||||
voyageai>=0.3.0
|
voyageai>=0.3.0
|
||||||
|
|
||||||
|
# Monitoring
|
||||||
|
sentry-sdk[fastapi]>=2.54.0
|
||||||
|
|
||||||
# Utilities
|
# Utilities
|
||||||
python-dotenv==1.0.1
|
python-dotenv==1.0.1
|
||||||
croniter>=2.0.0
|
croniter>=2.0.0
|
||||||
|
|||||||
Reference in New Issue
Block a user