Skip to content

Commit dfa1848

Browse files
committed
Add Sentry error tracking support
1 parent fc5d97f commit dfa1848

4 files changed

Lines changed: 19 additions & 0 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dependencies = [
3131
'python-json-logger>=4.0',
3232
'celery[sqlalchemy]',
3333
'asgiref',
34+
'sentry-sdk',
3435
]
3536

3637
[tool.setuptools]

src/api/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
from importlib.resources import files
77
from typing import Any, Literal
88

9+
import sentry_sdk
10+
from sentry_sdk.integrations.fastapi import FastApiIntegration
11+
from sentry_sdk.integrations.starlette import StarletteIntegration
12+
913
from alembic import command
1014
from alembic.config import Config
1115
from fastapi import FastAPI
@@ -89,6 +93,13 @@ def _logging_config() -> dict[str, Any]:
8993
logging.config.dictConfig(_logging_config())
9094
logger = logging.getLogger(__name__)
9195

96+
_sentry_dsn = get_settings().sentry_dsn
97+
if _sentry_dsn:
98+
sentry_sdk.init(
99+
dsn=_sentry_dsn,
100+
integrations=[StarletteIntegration(), FastApiIntegration()],
101+
)
102+
92103

93104
class _FastAPI(FastAPI):
94105
def openapi(self) -> dict[str, Any]:

src/api/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Settings(BaseSettings):
2828
)
2929
pitr_wal_retention_days: int = 7
3030
deployment_password_secret: SecretBytes
31+
sentry_dsn: str | None = None
3132

3233

3334
@lru_cache

src/worker/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1+
import sentry_sdk
12
from celery import Celery
23
from pydantic_settings import BaseSettings, SettingsConfigDict
4+
from sentry_sdk.integrations.celery import CeleryIntegration
35

46

57
class Settings(BaseSettings):
68
model_config = SettingsConfigDict(env_prefix="vela_", case_sensitive=False)
79
broker_url: str
810
result_backend: str
11+
sentry_dsn: str | None = None
912

1013

1114
_settings = Settings() # type: ignore[call-arg]
1215

16+
if _settings.sentry_dsn:
17+
sentry_sdk.init(dsn=_settings.sentry_dsn, integrations=[CeleryIntegration()])
18+
1319
app = Celery("vela", broker=_settings.broker_url, backend=_settings.result_backend)
1420

1521
# Persist task name, args, and kwargs in celery_taskmeta so AsyncResult can

0 commit comments

Comments
 (0)