Skip to content

Commit d1e866c

Browse files
committed
feat(sentry): update sentry configuration
Refs: HAUKI-710
1 parent 0be5814 commit d1e866c

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

config_dev.env.example

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,17 @@ STATIC_URL=/static/
139139

140140
# Sentry environment is an optional tag that can be included in sentry
141141
# reports. It is used to separate deployments within Sentry UI
142-
SENTRY_ENVIRONMENT=local-development-unconfigured
142+
SENTRY_ENVIRONMENT=local
143+
144+
# Sentry profile session sample rate determines the percentage of profiling sessions
145+
# that are sampled for performance monitoring. Set as a float between 0.0 (0%) and 1.0 (100%).
146+
# You probably want to use a lower rate in production.
147+
# SENTRY_PROFILE_SESSION_SAMPLE_RATE=1.0
148+
149+
# Sentry traces sample rate determines the percentage of transactions that are sampled
150+
# for performance monitoring. Set as a float between 0.0 (0%) and 1.0 (100%).
151+
# You probably want to use a lower rate in production.
152+
# SENTRY_TRACES_SAMPLE_RATE=1.0
143153

144154
# Mailgun API credentials
145155
# If Hauki incorporates the django notificator app https://github.com/City-of-Helsinki/django-ilmoitin,

hauki/settings.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88

99
import environ
1010
import sentry_sdk
11+
from corsheaders.defaults import default_headers
1112
from django.conf.global_settings import LANGUAGES as GLOBAL_LANGUAGES
1213
from django.core.exceptions import ImproperlyConfigured
1314
from sentry_sdk.integrations.django import DjangoIntegration
15+
from sentry_sdk.types import SamplingContext
1416

1517
CONFIG_FILE_NAME = "config_dev.env"
1618

@@ -65,7 +67,10 @@ def get_git_revision_hash() -> str:
6567
STATIC_URL=(str, "/static/"),
6668
TRUST_X_FORWARDED_HOST=(bool, False),
6769
SENTRY_DSN=(str, ""),
68-
SENTRY_ENVIRONMENT=(str, "development"),
70+
SENTRY_ENVIRONMENT=(str, "local"),
71+
SENTRY_PROFILE_SESSION_SAMPLE_RATE=(float, None),
72+
SENTRY_RELEASE=(str, None),
73+
SENTRY_TRACES_SAMPLE_RATE=(float, None),
6974
COOKIE_PREFIX=(str, "hauki"),
7075
INTERNAL_IPS=(list, []),
7176
INSTANCE_NAME=(str, "Hauki"),
@@ -178,14 +183,36 @@ def get_git_revision_hash() -> str:
178183
"drf_spectacular",
179184
] + env("EXTRA_INSTALLED_APPS")
180185

186+
187+
SENTRY_TRACES_SAMPLE_RATE = env.float("SENTRY_TRACES_SAMPLE_RATE")
188+
189+
190+
def sentry_traces_sampler(sampling_context: SamplingContext) -> float:
191+
# Respect parent sampling decision if one exists. Recommended by Sentry.
192+
if (parent_sampled := sampling_context.get("parent_sampled")) is not None:
193+
return float(parent_sampled)
194+
195+
# Exclude health check endpoints from tracing
196+
path = sampling_context.get("wsgi_environ", {}).get("PATH_INFO", "")
197+
if path.rstrip("/") in ["/healthz", "/readiness"]:
198+
return 0
199+
200+
# Use configured sample rate for all other requests
201+
return SENTRY_TRACES_SAMPLE_RATE or 0
202+
203+
181204
if env("SENTRY_DSN"):
182205
sentry_sdk.init(
183-
dsn=env("SENTRY_DSN"),
184-
environment=env("SENTRY_ENVIRONMENT"),
185-
release=get_git_revision_hash(),
206+
dsn=env.str("SENTRY_DSN"),
207+
environment=env.str("SENTRY_ENVIRONMENT"),
208+
release=env.str("SENTRY_RELEASE"),
186209
integrations=[DjangoIntegration()],
210+
traces_sampler=sentry_traces_sampler,
211+
profile_session_sample_rate=env.str("SENTRY_PROFILE_SESSION_SAMPLE_RATE"),
212+
profile_lifecycle="trace",
187213
)
188214

215+
189216
MIDDLEWARE = [
190217
# CorsMiddleware should be placed as high as possible and above WhiteNoiseMiddleware
191218
# in particular
@@ -294,6 +321,11 @@ def get_git_revision_hash() -> str:
294321
SECURE_PROXY_SSL_HEADER = env("SECURE_PROXY_SSL_HEADER")
295322

296323
CORS_ALLOW_ALL_ORIGINS = True
324+
CORS_ALLOW_HEADERS = (
325+
*default_headers,
326+
"baggage",
327+
"sentry-trace",
328+
)
297329
CSRF_COOKIE_NAME = f"{env('COOKIE_PREFIX')}-csrftoken"
298330
SESSION_COOKIE_NAME = f"{env('COOKIE_PREFIX')}-sessionid"
299331

0 commit comments

Comments
 (0)