diff --git a/.env.example b/.env.example index 50d6cc1..0b05f33 100644 --- a/.env.example +++ b/.env.example @@ -5,3 +5,5 @@ EMAIL_BACKEND=django.core.mail.backends.console.EmailBackend EMAIL_SENDGRID_REPLY_TO=reply-to@fakesendgrid.com PYTHONUNBUFFERED=1 SECRET_KEY="django-insecure-l@1xnj747ei)9ex(0zkfyy6zv@&*=i$wi9722=ji7)+s^_kuvy" +SENTRY_ENABLED=off +SENTRY_DSN=dsn_example diff --git a/journal/sentry/__init__.py b/journal/sentry/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/journal/sentry/apps.py b/journal/sentry/apps.py new file mode 100644 index 0000000..b7f6126 --- /dev/null +++ b/journal/sentry/apps.py @@ -0,0 +1,42 @@ +import sentry_sdk +from django.apps import AppConfig +from django.conf import settings +from sentry_sdk.integrations.django import DjangoIntegration + + +def traces_sampler(sampling_context): # pragma: no cover + """Select a sample rate off of the requested path. + + The root endpoint seemed to get hammered by some bot and ate a huge percent + of transactions in a week. I don't care about that page right now, + so ignore it. + """ + path = sampling_context.get("wsgi_environ", {}).get("PATH_INFO", "") + if path == "/": + return 0 + + return 1.0 + + +class SentryConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "journal.sentry" + + def ready(self): # pragma: no cover + """Initialize Sentry. + + Sentry initialization is moved to an application ready timeframe + because it triggers circular imports with settings when used with + type checking when django-stubs is enabled. + """ + if not settings.SENTRY_ENABLED: + return + + sentry_sdk.init( + dsn=settings.SENTRY_DSN, + integrations=[ + DjangoIntegration(), + ], + traces_sampler=traces_sampler, + send_default_pii=True, + ) diff --git a/project/settings.py b/project/settings.py index e9ffffd..03775d1 100644 --- a/project/settings.py +++ b/project/settings.py @@ -6,10 +6,11 @@ BASE_DIR = Path(__file__).resolve().parent.parent env = environ.Env( + ACCOUNT_DEFAULT_HTTP_PROTOCOL=(str, "https"), ALLOWED_HOSTS=(list, []), DEBUG=(bool, False), EMAIL_BACKEND=(str, "FIXME: replace with anymail backend Issue #10"), - ACCOUNT_DEFAULT_HTTP_PROTOCOL=(str, "https"), + SENTRY_ENABLED=(bool, True), ) environ.Env.read_env(BASE_DIR / ".env") @@ -39,6 +40,7 @@ "journal.accounts", "journal.core", "journal.entries", + "journal.sentry", ] MIDDLEWARE = [ @@ -203,3 +205,8 @@ "rankdir": "BT", "output": "models.png", } + +# sentry-sdk + +SENTRY_ENABLED = env("SENTRY_ENABLED") +SENTRY_DSN = env("SENTRY_DSN") diff --git a/requirements.in b/requirements.in index 991daf8..baa7f1f 100644 --- a/requirements.in +++ b/requirements.in @@ -3,3 +3,4 @@ django-allauth==0.54.0 django-environ==0.10.0 django-extensions==3.2.3 gunicorn==20.1.0 +sentry-sdk==1.28.1 diff --git a/requirements.txt b/requirements.txt index c744118..90d49e7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,7 +7,9 @@ asgiref==3.6.0 # via django certifi==2023.5.7 - # via requests + # via + # requests + # sentry-sdk cffi==1.15.1 # via cryptography charset-normalizer==3.1.0 @@ -45,10 +47,14 @@ requests==2.31.0 # requests-oauthlib requests-oauthlib==1.3.1 # via django-allauth +sentry-sdk==1.28.1 + # via -r requirements.in sqlparse==0.4.4 # via django urllib3==2.0.2 - # via requests + # via + # requests + # sentry-sdk # The following packages are considered to be unsafe in a requirements file: # setuptools