Skip to content

Commit 082d6cf

Browse files
committed
refactor(sentry): use RUNTIME_ENVIRONMENT by default
move module-level variables into configure function, unneeded outside module, wait for Django startup before using settings
1 parent 1ce493b commit 082d6cf

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

benefits/sentry.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import subprocess
55

6+
from django.conf import settings
67
import sentry_sdk
78
from sentry_sdk.integrations.django import DjangoIntegration
89
from sentry_sdk.scrubber import EventScrubber, DEFAULT_DENYLIST
@@ -11,7 +12,6 @@
1112

1213
logger = logging.getLogger(__name__)
1314

14-
SENTRY_ENVIRONMENT = os.environ.get("SENTRY_ENVIRONMENT", "local")
1515
SENTRY_CSP_REPORT_URI = None
1616

1717

@@ -80,19 +80,21 @@ def get_traces_sample_rate():
8080

8181

8282
def configure():
83-
SENTRY_DSN = os.environ.get("SENTRY_DSN")
84-
if SENTRY_DSN:
83+
sentry_dsn = os.environ.get("SENTRY_DSN")
84+
sentry_environment = os.environ.get("SENTRY_ENVIRONMENT", settings.RUNTIME_ENVIRONMENT())
85+
86+
if sentry_dsn:
8587
release = get_release()
86-
logger.info(f"Enabling Sentry for environment '{SENTRY_ENVIRONMENT}', release '{release}'...")
88+
logger.info(f"Enabling Sentry for environment '{sentry_environment}', release '{release}'...")
8789

8890
# https://docs.sentry.io/platforms/python/configuration/
8991
sentry_sdk.init(
90-
dsn=SENTRY_DSN,
92+
dsn=sentry_dsn,
9193
integrations=[
9294
DjangoIntegration(),
9395
],
9496
traces_sample_rate=get_traces_sample_rate(),
95-
environment=SENTRY_ENVIRONMENT,
97+
environment=sentry_environment,
9698
release=release,
9799
in_app_include=["benefits"],
98100
# send_default_pii must be False (the default) for a custom EventScrubber/denylist

docs/configuration/environment-variables.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ Enables [sending events to Sentry](../../deployment/troubleshooting/#error-monit
219219

220220
[`environment` config value](https://docs.sentry.io/platforms/python/configuration/options/#environment)
221221

222-
Segments errors by which deployment they occur in. This defaults to `local`, and can be set to match one of the [environment names](../../deployment/infrastructure/#environments).
222+
Segments errors by which deployment they occur in. This defaults to `dev`, and can be set to match one of the [environment names](../../deployment/infrastructure/#environments).
223+
224+
`local` may also be used for local testing of the Sentry integration.
223225

224226
### `SENTRY_REPORT_URI`
225227

0 commit comments

Comments
 (0)