✨ feat(sentry): add optional Sentry integration for bot and API#77
Conversation
Add Sentry error tracking to both the Litestar API and aiogram bot entry points. The integration is fully optional — if no `sentry` section is present in the YAML config, the app runs without it. - Add SentryConfig model with DSN, environment, and sample rate fields - Create init_sentry() helper in infrastructure layer - Call init_sentry() in both API and bot startup paths - Add sentry-sdk[litestar] dependency - Add unit tests for config and init_sentry - Document sentry section in config-example.yaml Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
PR Review: ✨ feat(sentry): add optional Sentry integration for bot and APIOverall the implementation is clean, minimal, and follows the project's architecture well. The opt-in pattern is correct and the lazy import is a nice touch. A few issues worth addressing before merge. 🔴 Critical Issues1. Dangerous production defaults for sample rates
traces_sample_rate: float = 1.0 # 100% tracing overhead in production
profiles_sample_rate: float = 1.0 # 100% profiling overhead in productionBoth fields default to traces_sample_rate: float = 0.1
profiles_sample_rate: float = 0.1Also, 2. No range validation on sample rate fieldsValues outside from pydantic import field_validator
@field_validator("traces_sample_rate", "profiles_sample_rate")
@classmethod
def validate_sample_rate(cls, v: float) -> float:
if not 0.0 <= v <= 1.0:
raise ValueError("Sample rate must be between 0.0 and 1.0")
return v🟡 Suggestions for Improvement3.
|
- Add field_validator for traces_sample_rate and profiles_sample_rate to enforce 0.0–1.0 range - Wrap sentry_sdk.init() in try/except so a bad DSN or network error doesn't crash the app on startup - Change default environment from "production" to "development" - Update tests to cover new validator and failure path Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Code Review — PR #77: Optional Sentry IntegrationOverall this is a clean, well-scoped addition that follows the project's conventions. Two issues need addressing before merge; the rest are suggestions. 🔴 Critical Issues1.
The better approach is an optional dependency group: [project.optional-dependencies]
sentry = ["sentry-sdk[litestar]>=2.0.0"]Installed when needed via 2. Dangerous production defaults: Both fields default to
Sentry's own documentation recommends Suggested safer defaults: traces_sample_rate: float = 0.1
profiles_sample_rate: float = 0.1And update 🟡 Suggestions3. DSN validation is absent
from pydantic import AnyUrl
dsn: AnyUrlor at minimum 4. The Sentry SDK defaults class SentryConfig(BaseModel):
...
send_default_pii: bool = Falsesentry_sdk.init(
...
send_default_pii=config.sentry.send_default_pii,
)5. No
release: str | None = Noneand passing it to 6. Misleading test in def test_no_sentry_config_skips_init(self):
config = _make_config(sentry=None)
with patch("sentry_sdk.init") as mock_init:
init_sentry(config)
mock_init.assert_not_called()When A more meaningful version: def test_no_sentry_config_skips_init(self):
config = _make_config(sentry=None)
# Ensure sentry_sdk is not imported at all when sentry is unconfigured
with patch.dict("sys.modules", {"sentry_sdk": None}):
init_sentry(config) # would raise ImportError if sentry_sdk were imported✅ What's Done Well
Questions
Verdict: Request ChangesThe two critical issues (hard dependency + unsafe production defaults) should be resolved before merge. Everything else is non-blocking but worth addressing in a follow-up. |
Pull Request
Description
Add optional Sentry error tracking to both the Litestar API and aiogram bot entry points. If no
sentrysection is present in the YAML config, the app runs exactly as before.Changes:
SentryConfigPydantic model with DSN, environment, and sample rate fieldsinit_sentry()helper insrc/infrastructure/sentry.pycreate_app) and bot (main) startup pathssentry-sdk[litestar]>=2.0.0dependency (aiogram integration is auto-discovered)config-example.yamlType of Change
Testing
Code Quality
pre-commit run --all-files)ruff check src/ tests/)ruff format src/ tests/ --check)Additional Notes
import sentry_sdkis insideinit_sentry()body so it only executes when Sentry is actually configuredChecklist for Reviewers:
🤖 Generated with Claude Code