|
8 | 8 |
|
9 | 9 | import environ |
10 | 10 | import sentry_sdk |
| 11 | +from corsheaders.defaults import default_headers |
11 | 12 | from django.conf.global_settings import LANGUAGES as GLOBAL_LANGUAGES |
12 | 13 | from django.core.exceptions import ImproperlyConfigured |
13 | 14 | from sentry_sdk.integrations.django import DjangoIntegration |
| 15 | +from sentry_sdk.types import SamplingContext |
14 | 16 |
|
15 | 17 | CONFIG_FILE_NAME = "config_dev.env" |
16 | 18 |
|
@@ -65,7 +67,10 @@ def get_git_revision_hash() -> str: |
65 | 67 | STATIC_URL=(str, "/static/"), |
66 | 68 | TRUST_X_FORWARDED_HOST=(bool, False), |
67 | 69 | 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), |
69 | 74 | COOKIE_PREFIX=(str, "hauki"), |
70 | 75 | INTERNAL_IPS=(list, []), |
71 | 76 | INSTANCE_NAME=(str, "Hauki"), |
@@ -178,14 +183,36 @@ def get_git_revision_hash() -> str: |
178 | 183 | "drf_spectacular", |
179 | 184 | ] + env("EXTRA_INSTALLED_APPS") |
180 | 185 |
|
| 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 | + |
181 | 204 | if env("SENTRY_DSN"): |
182 | 205 | 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"), |
186 | 209 | integrations=[DjangoIntegration()], |
| 210 | + traces_sampler=sentry_traces_sampler, |
| 211 | + profile_session_sample_rate=env.str("SENTRY_PROFILE_SESSION_SAMPLE_RATE"), |
| 212 | + profile_lifecycle="trace", |
187 | 213 | ) |
188 | 214 |
|
| 215 | + |
189 | 216 | MIDDLEWARE = [ |
190 | 217 | # CorsMiddleware should be placed as high as possible and above WhiteNoiseMiddleware |
191 | 218 | # in particular |
@@ -294,6 +321,11 @@ def get_git_revision_hash() -> str: |
294 | 321 | SECURE_PROXY_SSL_HEADER = env("SECURE_PROXY_SSL_HEADER") |
295 | 322 |
|
296 | 323 | CORS_ALLOW_ALL_ORIGINS = True |
| 324 | +CORS_ALLOW_HEADERS = ( |
| 325 | + *default_headers, |
| 326 | + "baggage", |
| 327 | + "sentry-trace", |
| 328 | +) |
297 | 329 | CSRF_COOKIE_NAME = f"{env('COOKIE_PREFIX')}-csrftoken" |
298 | 330 | SESSION_COOKIE_NAME = f"{env('COOKIE_PREFIX')}-sessionid" |
299 | 331 |
|
|
0 commit comments