-
Notifications
You must be signed in to change notification settings - Fork 1
Error Monitoring
arminrad edited this page Mar 16, 2026
·
2 revisions
Real-time error tracking and alerting with Sentry
Comprehensive error monitoring:
- Automatic error capture
- Real-time alerts
- Stack traces
- User context
- Release tracking
- Performance monitoring
Provider: Sentry.io
SENTRY_DSN=https://xxx@xxx.ingest.sentry.io/xxx
SENTRY_ENVIRONMENT=production
SENTRY_TRACES_SAMPLE_RATE=0.1# src/main.py
import sentry_sdk
sentry_sdk.init(
dsn=os.getenv("SENTRY_DSN"),
environment=os.getenv("SENTRY_ENVIRONMENT", "production"),
traces_sample_rate=float(os.getenv("SENTRY_TRACES_SAMPLE_RATE", "0.1")),
profiles_sample_rate=0.1,
)# Automatically captured
try:
result = risky_operation()
except Exception as e:
# Sentry captures this
raisefrom sentry_sdk.integrations.fastapi import FastApiIntegration
sentry_sdk.init(
dsn=dsn,
integrations=[FastApiIntegration()],
)from sentry_sdk import capture_exception
try:
process_payment()
except PaymentError as e:
capture_exception(e)
return {"error": "Payment failed"}from sentry_sdk import capture_message
capture_message("Unusual activity detected", level="warning")from sentry_sdk import set_user, set_tag, set_context
# User context
set_user({"id": user.id, "email": user.email})
# Tags for filtering
set_tag("payment_method", "stripe")
set_tag("plan", "pro")
# Additional context
set_context("payment", {
"amount": 100,
"currency": "usd"
})Sentry groups similar errors automatically:
# These will be grouped together
raise ValueError("Invalid amount: 100")
raise ValueError("Invalid amount: 200")
# → Grouped as "ValueError: Invalid amount"from sentry_sdk import configure_scope
with configure_scope() as scope:
scope.fingerprint = ["custom-group", error_type]sentry_sdk.init(
dsn=dsn,
ignore_errors=[
KeyboardInterrupt,
"ConnectionError",
],
before_send=filter_errors,
)
def filter_errors(event, hint):
# Don't send 404 errors
if event.get('request', {}).get('url', '').endswith('/favicon.ico'):
return None
return eventfrom sentry_sdk import start_transaction
with start_transaction(op="function", name="process_payment"):
result = process_payment()with start_transaction(op="task", name="sync_models"):
with sentry_sdk.start_span(op="db", description="fetch models"):
models = fetch_models()
with sentry_sdk.start_span(op="http", description="update pricing"):
update_pricing(models)# Create release
sentry-cli releases new v2.0.3
# Associate commits
sentry-cli releases set-commits v2.0.3 --auto
# Deploy
sentry-cli releases deploys v2.0.3 new -e production
# Finalize
sentry-cli releases finalize v2.0.3sentry_sdk.init(
dsn=dsn,
release="v2.0.3",
)- Go to Alerts → Create Alert
- Set conditions:
- When: Error count > 10 in 1 minute
- Filter: environment:production
- Actions:
- Send email
- Slack notification
- PagerDuty
- Error frequency: Too many errors
- New issues: First time seeing error
- Regression: Previously resolved error returns
- Performance: Slow transactions
- Add context: User ID, request ID, etc.
- Filter noise: Ignore expected errors
- Set sample rate: Don't send 100% of events
- Use releases: Track which version has issues
- Assign ownership: Team/person per error type
- Resolve issues: Mark as resolved when fixed
- Review regularly: Weekly error review
- Error rate: Errors per minute
- Affected users: Unique users hit by errors
- Crash-free sessions: % sessions without errors
- Release health: Errors per release
- Issues: All error groups
- Performance: Transaction performance
- Releases: Deployment tracking
- User Feedback: User-reported issues
import logging
from sentry_sdk.integrations.logging import LoggingIntegration
sentry_logging = LoggingIntegration(
level=logging.INFO,
event_level=logging.ERROR
)
sentry_sdk.init(
dsn=dsn,
integrations=[sentry_logging],
)Cause: Wrong DSN or environment
Solution: Check SENTRY_DSN and test with:
sentry_sdk.capture_message("Test message")Cause: Sample rate too high
Solution: Reduce traces_sample_rate
Cause: Context not set before error
Solution: Set context early in request:
set_user({"id": user_id})Last Updated: December 2024 Status: Production Ready
- Monitoring-System — Full monitoring architecture
- Prometheus-Setup — Metric infrastructure
- Performance-Monitoring — Performance-specific metrics
- Troubleshooting — Common issues and resolutions
Reading Path (start here, in order)
- Conceptual Model
- Stability Definition
- Conceptual Model Features
- Features
- Delta Report
- Features-Acceptance-Criteria
Testing
Security & Access
Billing
Monitoring
Features
Providers
Operations
Data References