From 309502d68b55e9e2f6b7f225536c481a11d190bb Mon Sep 17 00:00:00 2001 From: Zach Fuller Date: Sat, 28 Sep 2024 17:55:14 -0700 Subject: [PATCH] Sentry Monitoring (#75) * loading sentry dsn from env file * only call init on sentry sdk if enabled * removed sentry debug endpoint --- pyproject.toml | 1 + src/smolvault/config.py | 2 ++ src/smolvault/main.py | 10 +++++++++- tests/testing.env | 2 ++ uv.lock | 20 ++++++++++++++++++++ 5 files changed, 34 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 36c7046..46cefff 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ dependencies = [ "hypercorn>=0.17.3", "pyjwt[crypto]>=2.9.0", "bcrypt>=4.2.0", + "sentry-sdk[fastapi]>=2.14.0", ] [build-system] diff --git a/src/smolvault/config.py b/src/smolvault/config.py index b0f2158..d867a1d 100644 --- a/src/smolvault/config.py +++ b/src/smolvault/config.py @@ -13,6 +13,8 @@ class Settings(BaseSettings): user_whitelist: str users_limit: int daily_upload_limit_bytes: int + sentry_enabled: bool + sentry_dsn: str model_config = SettingsConfigDict(env_file=".env") diff --git a/src/smolvault/main.py b/src/smolvault/main.py index 2cccd98..e12fb56 100644 --- a/src/smolvault/main.py +++ b/src/smolvault/main.py @@ -6,6 +6,7 @@ from logging.handlers import RotatingFileHandler from typing import Annotated +import sentry_sdk from fastapi import BackgroundTasks, Depends, FastAPI, File, Form, HTTPException, UploadFile from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.gzip import GZipMiddleware @@ -31,6 +32,14 @@ ) logger = logging.getLogger(__name__) +settings: Settings = get_settings() +if settings.sentry_enabled: + sentry_sdk.init( + dsn=settings.sentry_dsn, + traces_sample_rate=1.0, # Set traces_sample_rate to 1.0 to capture 100% + profiles_sample_rate=1.0, + ) + app = FastAPI(title="smolvault", docs_url=None, redoc_url=None) app.add_middleware(GZipMiddleware, minimum_size=1000) @@ -43,7 +52,6 @@ ) -settings: Settings = get_settings() s3_client = S3Client(bucket_name=settings.smolvault_bucket) cache = CacheManager(cache_dir=settings.smolvault_cache) diff --git a/tests/testing.env b/tests/testing.env index 3530ae6..d78b4ec 100644 --- a/tests/testing.env +++ b/tests/testing.env @@ -5,3 +5,5 @@ SMOLVAULT_CACHE="./uploads/" USER_WHITELIST="1,2" USERS_LIMIT="3" DAILY_UPLOAD_LIMIT_BYTES="50000" +SENTRY_ENABLED="false" +SENTRY_DSN="none" diff --git a/uv.lock b/uv.lock index a005924..90b308a 100644 --- a/uv.lock +++ b/uv.lock @@ -1550,6 +1550,24 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/3c/4a/b221409913760d26cf4498b7b1741d510c82d3ad38381984a3ddc135ec66/s3transfer-0.10.2-py3-none-any.whl", hash = "sha256:eca1c20de70a39daee580aef4986996620f365c4e0fda6a86100231d62f1bf69", size = 82716 }, ] +[[package]] +name = "sentry-sdk" +version = "2.14.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "certifi" }, + { name = "urllib3" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/3c/23/6527e56fb17817153c37d702d6b9ed0a2f75ed213fd98a176c1b8894ad20/sentry_sdk-2.14.0.tar.gz", hash = "sha256:1e0e2eaf6dad918c7d1e0edac868a7bf20017b177f242cefe2a6bcd47955961d", size = 282948 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/40/de/956ce1d71459fa1af0486ca141fc605ac16f7c8855750668ff663e2b436a/sentry_sdk-2.14.0-py2.py3-none-any.whl", hash = "sha256:b8bc3dc51d06590df1291b7519b85c75e2ced4f28d9ea655b6d54033503b5bf4", size = 311425 }, +] + +[package.optional-dependencies] +fastapi = [ + { name = "fastapi" }, +] + [[package]] name = "setuptools" version = "75.1.0" @@ -1581,6 +1599,7 @@ dependencies = [ { name = "pyjwt", extra = ["crypto"] }, { name = "python-dotenv" }, { name = "python-multipart" }, + { name = "sentry-sdk", extra = ["fastapi"] }, { name = "sqlmodel" }, ] @@ -1612,6 +1631,7 @@ requires-dist = [ { name = "pyjwt", extras = ["crypto"], specifier = ">=2.9.0" }, { name = "python-dotenv", specifier = ">=1.0.1" }, { name = "python-multipart", specifier = ">=0.0.9" }, + { name = "sentry-sdk", extras = ["fastapi"], specifier = ">=2.14.0" }, { name = "sqlmodel", specifier = ">=0.0.21" }, ]