Skip to content

Commit a5c1f5f

Browse files
committed
Add sentry integration
1 parent a8e623b commit a5c1f5f

File tree

5 files changed

+107
-2
lines changed

5 files changed

+107
-2
lines changed

.env.template

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ EMAIL_API_KEY=secret-api-key
1212
# Broker
1313
REDISCLOUD_URL=redis://user:password@host:6379
1414

15+
# Logging
16+
# Optional, if not set alerts will not be fired.
17+
# To activate do not forget about feature flag ENABLE_SENTRY, which is False by default.
18+
SENTRY_DSN=https://password@host/project
19+
# Can be development, staging or production only
20+
# Default value is production.
21+
SENTRY_ENVIRONMENT=production
22+
1523
#####
1624
# Feature Flags
1725
# All Feature Flags are optional and false by default.
@@ -22,3 +30,6 @@ ACTIVATE_USERS=true
2230
# Keep in mind proxy must pass x-forwarded-proto, x-forwarded-port and host in headers.
2331
# TRUSTED_HOST must be valid.
2432
ENABLE_HTTPS_REDIRECT=true
33+
# Enable Sentry Logging
34+
# Requires SENTRY_DSN environment variable to set.
35+
ENABLE_SENTRY=false

futuramaapi/apps/app.py

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from contextlib import asynccontextmanager
44
from typing import TYPE_CHECKING, Self
55

6+
import sentry_sdk
67
from fastapi import FastAPI
78
from fastapi.routing import APIRouter
89
from fastapi.staticfiles import StaticFiles
@@ -31,11 +32,25 @@ def __init__(
3132
*,
3233
lifespan: Lifespan[Self] | None,
3334
) -> None:
35+
self._init_sentry()
36+
3437
self.routers: list[APIRouter] = routers
3538
self.app: Starlette = self.get_app(lifespan)
3639

3740
self.build()
3841

42+
@staticmethod
43+
def _init_sentry() -> None:
44+
if feature_flags.enable_sentry is False or settings.sentry.dsn is None:
45+
return None
46+
47+
sentry_sdk.init(
48+
dsn=settings.sentry.dsn,
49+
environment=settings.sentry.environment,
50+
traces_sample_rate=settings.sentry.traces_sample_rate,
51+
profiles_sample_rate=settings.sentry.profiles_sample_rate,
52+
)
53+
3954
@abstractmethod
4055
def get_app(
4156
self,

futuramaapi/core/configs.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from base64 import urlsafe_b64encode
22
from functools import cached_property
33
from pathlib import Path
4-
from typing import Any
4+
from typing import Any, Literal
55
from urllib.parse import ParseResult, urlparse
66

77
from cryptography.fernet import Fernet
@@ -111,6 +111,30 @@ def pool(self) -> ConnectionPool:
111111
redis_settings = RedisSettings()
112112

113113

114+
class SentrySettings(BaseSettings):
115+
dsn: HttpUrl | None = None
116+
traces_sample_rate: float = Field(
117+
default=1.0,
118+
ge=0,
119+
le=1,
120+
)
121+
profiles_sample_rate: float = Field(
122+
default=1.0,
123+
ge=0,
124+
le=1,
125+
)
126+
# Not the best practice to split on environments and currently the code is totally abstract from
127+
# any environment, but it's really nice to have sentry divided on different environments.
128+
environment: Literal["development", "staging", "production"] = "production"
129+
130+
model_config = SettingsConfigDict(
131+
env_prefix="sentry_",
132+
)
133+
134+
135+
sentry_settings = SentrySettings()
136+
137+
114138
def _parse_list(value: str) -> list[str]:
115139
return [str(x).strip() for x in value.split(",")]
116140

@@ -144,6 +168,7 @@ class Settings(BaseSettings):
144168
secret_key: str
145169
email: EmailSettings = email_settings
146170
redis: RedisSettings = redis_settings
171+
sentry: SentrySettings = sentry_settings
147172

148173
@cached_property
149174
def fernet(self) -> Fernet:
@@ -184,6 +209,7 @@ class FeatureFlags(BaseSettings):
184209
enable_https_redirect: bool = False
185210
send_emails: bool = True
186211
activate_users: bool = False
212+
enable_sentry: bool = False
187213

188214

189215
feature_flags = FeatureFlags()

poetry.lock

+52-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ fastapi-mail = {extras = ["httpx"], version = "^1.4.1"}
2525
strawberry-graphql = "^0.230.0"
2626
cryptography = "^42.0.8"
2727
redis = "^5.0.7"
28+
pyyaml = "^6.0.1"
29+
sentry-sdk = {extras = ["fastapi"], version = "^2.10.0"}
2830

2931
[tool.poetry.group.dev.dependencies]
3032
pre-commit = "^3.7.0"

0 commit comments

Comments
 (0)