Skip to content

Commit

Permalink
Add sentry integration
Browse files Browse the repository at this point in the history
  • Loading branch information
koldakov committed Jul 22, 2024
1 parent a8e623b commit a5c1f5f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 2 deletions.
11 changes: 11 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ EMAIL_API_KEY=secret-api-key
# Broker
REDISCLOUD_URL=redis://user:password@host:6379

# Logging
# Optional, if not set alerts will not be fired.
# To activate do not forget about feature flag ENABLE_SENTRY, which is False by default.
SENTRY_DSN=https://password@host/project
# Can be development, staging or production only
# Default value is production.
SENTRY_ENVIRONMENT=production

#####
# Feature Flags
# All Feature Flags are optional and false by default.
Expand All @@ -22,3 +30,6 @@ ACTIVATE_USERS=true
# Keep in mind proxy must pass x-forwarded-proto, x-forwarded-port and host in headers.
# TRUSTED_HOST must be valid.
ENABLE_HTTPS_REDIRECT=true
# Enable Sentry Logging
# Requires SENTRY_DSN environment variable to set.
ENABLE_SENTRY=false
15 changes: 15 additions & 0 deletions futuramaapi/apps/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from contextlib import asynccontextmanager
from typing import TYPE_CHECKING, Self

import sentry_sdk
from fastapi import FastAPI
from fastapi.routing import APIRouter
from fastapi.staticfiles import StaticFiles
Expand Down Expand Up @@ -31,11 +32,25 @@ def __init__(
*,
lifespan: Lifespan[Self] | None,
) -> None:
self._init_sentry()

self.routers: list[APIRouter] = routers
self.app: Starlette = self.get_app(lifespan)

self.build()

@staticmethod
def _init_sentry() -> None:
if feature_flags.enable_sentry is False or settings.sentry.dsn is None:
return None

sentry_sdk.init(
dsn=settings.sentry.dsn,
environment=settings.sentry.environment,
traces_sample_rate=settings.sentry.traces_sample_rate,
profiles_sample_rate=settings.sentry.profiles_sample_rate,
)

@abstractmethod
def get_app(
self,
Expand Down
28 changes: 27 additions & 1 deletion futuramaapi/core/configs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from base64 import urlsafe_b64encode
from functools import cached_property
from pathlib import Path
from typing import Any
from typing import Any, Literal
from urllib.parse import ParseResult, urlparse

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


class SentrySettings(BaseSettings):
dsn: HttpUrl | None = None
traces_sample_rate: float = Field(
default=1.0,
ge=0,
le=1,
)
profiles_sample_rate: float = Field(
default=1.0,
ge=0,
le=1,
)
# Not the best practice to split on environments and currently the code is totally abstract from
# any environment, but it's really nice to have sentry divided on different environments.
environment: Literal["development", "staging", "production"] = "production"

model_config = SettingsConfigDict(
env_prefix="sentry_",
)


sentry_settings = SentrySettings()


def _parse_list(value: str) -> list[str]:
return [str(x).strip() for x in value.split(",")]

Expand Down Expand Up @@ -144,6 +168,7 @@ class Settings(BaseSettings):
secret_key: str
email: EmailSettings = email_settings
redis: RedisSettings = redis_settings
sentry: SentrySettings = sentry_settings

@cached_property
def fernet(self) -> Fernet:
Expand Down Expand Up @@ -184,6 +209,7 @@ class FeatureFlags(BaseSettings):
enable_https_redirect: bool = False
send_emails: bool = True
activate_users: bool = False
enable_sentry: bool = False


feature_flags = FeatureFlags()
53 changes: 52 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ fastapi-mail = {extras = ["httpx"], version = "^1.4.1"}
strawberry-graphql = "^0.230.0"
cryptography = "^42.0.8"
redis = "^5.0.7"
pyyaml = "^6.0.1"
sentry-sdk = {extras = ["fastapi"], version = "^2.10.0"}

[tool.poetry.group.dev.dependencies]
pre-commit = "^3.7.0"
Expand Down

0 comments on commit a5c1f5f

Please sign in to comment.