Skip to content

Commit

Permalink
Add Sentry support.
Browse files Browse the repository at this point in the history
Fixes #8
  • Loading branch information
mblayman committed Jul 27, 2023
1 parent 154c4d7 commit 1c75a49
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ EMAIL_BACKEND=django.core.mail.backends.console.EmailBackend
EMAIL_SENDGRID_REPLY_TO=[email protected]
PYTHONUNBUFFERED=1
SECRET_KEY="django-insecure-l@1xnj747ei)9ex(0zkfyy6zv@&*=i$wi9722=ji7)+s^_kuvy"
SENTRY_ENABLED=off
SENTRY_DSN=dsn_example
Empty file added journal/sentry/__init__.py
Empty file.
42 changes: 42 additions & 0 deletions journal/sentry/apps.py
Original file line number Diff line number Diff line change
@@ -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,
)
9 changes: 8 additions & 1 deletion project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -39,6 +40,7 @@
"journal.accounts",
"journal.core",
"journal.entries",
"journal.sentry",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -203,3 +205,8 @@
"rankdir": "BT",
"output": "models.png",
}

# sentry-sdk

SENTRY_ENABLED = env("SENTRY_ENABLED")
SENTRY_DSN = env("SENTRY_DSN")
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 8 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 1c75a49

Please sign in to comment.