Skip to content

Commit 33b729d

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

4 files changed

Lines changed: 18 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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
from importlib.resources import files
77
from typing import Any, Literal
88

9+
import sentry_sdk
910
from alembic import command
1011
from alembic.config import Config
1112
from fastapi import FastAPI
1213
from fastapi.middleware.cors import CORSMiddleware
1314
from fastapi.routing import APIRoute
1415
from pydantic import BaseModel
16+
from sentry_sdk.integrations.fastapi import FastApiIntegration
17+
from sentry_sdk.integrations.starlette import StarletteIntegration
1518

1619
from ..deployment.monitors.health import vm_monitor
1720
from .backup import router as backup_router
@@ -89,6 +92,13 @@ def _logging_config() -> dict[str, Any]:
8992
logging.config.dictConfig(_logging_config())
9093
logger = logging.getLogger(__name__)
9194

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

93103
class _FastAPI(FastAPI):
94104
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)