Skip to content

Commit acdf4fb

Browse files
committed
feat(sentry): update sentry configuration
Refs: PT-1958
1 parent 025b74e commit acdf4fb

2 files changed

Lines changed: 47 additions & 7 deletions

File tree

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,8 @@ CORS_ALLOW_ALL_ORIGINS=True
4949
TOKEN_AUTH_BROWSER_TEST_ENABLED=1
5050
TOKEN_AUTH_BROWSER_TEST_JWT_256BIT_SIGN_SECRET=your-256-bit-secret
5151
TOKEN_AUTH_BROWSER_TEST_JWT_ISSUER=https://kultus-ui.test.hel.ninja,https://kultus-admin-ui.test.hel.ninja
52+
53+
# SENTRY_DSN=https://abcdefg@your.sentry.here/999
54+
# SENTRY_PROFILE_SESSION_SAMPLE_RATE=0
55+
# SENTRY_TRACES_SAMPLE_RATE=0
56+
# SENTRY_TRACES_IGNORE_PATHS=/healthz,/readiness

palvelutarjotin/settings.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
import environ
77
import sentry_sdk
8+
from corsheaders.defaults import default_headers
89
from csp.constants import SELF as CSP_SELF
910
from csp.constants import UNSAFE_INLINE as CSP_UNSAFE_INLINE
1011
from django.core.exceptions import ImproperlyConfigured
1112
from django.utils.translation import gettext_lazy as _
1213
from sentry_sdk.integrations.django import DjangoIntegration
14+
from sentry_sdk.types import SamplingContext
1315

1416
from palvelutarjotin import __version__
1517

@@ -47,7 +49,11 @@
4749
MAIL_MAILGUN_DOMAIN=(str, ""),
4850
MAIL_MAILGUN_API=(str, ""),
4951
SENTRY_DSN=(str, ""),
50-
SENTRY_ENVIRONMENT=(str, ""),
52+
SENTRY_ENVIRONMENT=(str, "local"),
53+
SENTRY_PROFILE_SESSION_SAMPLE_RATE=(float, None),
54+
SENTRY_RELEASE=(str, None),
55+
SENTRY_TRACES_SAMPLE_RATE=(float, None),
56+
SENTRY_TRACES_IGNORE_PATHS=(list, ["/healthz", "/readiness"]),
5157
CORS_ALLOWED_ORIGINS=(list, []),
5258
CORS_ALLOWED_ORIGIN_REGEXES=(list, []),
5359
CORS_ALLOW_ALL_ORIGINS=(bool, False),
@@ -163,12 +169,35 @@
163169

164170
COMMIT_HASH = env("OPENSHIFT_BUILD_COMMIT")
165171
VERSION = __version__
166-
sentry_sdk.init(
167-
dsn=env.str("SENTRY_DSN"),
168-
release=env.str("OPENSHIFT_BUILD_COMMIT", VERSION),
169-
environment=env("SENTRY_ENVIRONMENT"),
170-
integrations=[DjangoIntegration()],
171-
)
172+
173+
SENTRY_TRACES_SAMPLE_RATE = env("SENTRY_TRACES_SAMPLE_RATE")
174+
SENTRY_TRACES_IGNORE_PATHS = env.list("SENTRY_TRACES_IGNORE_PATHS")
175+
176+
177+
def sentry_traces_sampler(sampling_context: SamplingContext) -> float:
178+
# Respect parent sampling decision if one exists. Recommended by Sentry.
179+
if (parent_sampled := sampling_context.get("parent_sampled")) is not None:
180+
return float(parent_sampled)
181+
182+
# Exclude health check endpoints from tracing
183+
path = sampling_context.get("wsgi_environ", {}).get("PATH_INFO", "")
184+
if path.rstrip("/") in SENTRY_TRACES_IGNORE_PATHS:
185+
return 0
186+
187+
# Use configured sample rate for all other requests
188+
return SENTRY_TRACES_SAMPLE_RATE or 0
189+
190+
191+
if env("SENTRY_DSN"):
192+
sentry_sdk.init(
193+
dsn=env("SENTRY_DSN"),
194+
environment=env("SENTRY_ENVIRONMENT"),
195+
release=env("SENTRY_RELEASE"),
196+
integrations=[DjangoIntegration()],
197+
traces_sampler=sentry_traces_sampler,
198+
profile_session_sample_rate=env("SENTRY_PROFILE_SESSION_SAMPLE_RATE"),
199+
profile_lifecycle="trace",
200+
)
172201

173202
MEDIA_ROOT = env("MEDIA_ROOT")
174203
STATIC_ROOT = env("STATIC_ROOT")
@@ -270,6 +299,12 @@
270299
CORS_ALLOWED_ORIGIN_REGEXES = env.list("CORS_ALLOWED_ORIGIN_REGEXES")
271300
CORS_ALLOW_ALL_ORIGINS = env.bool("CORS_ALLOW_ALL_ORIGINS")
272301

302+
CORS_ALLOW_HEADERS = (
303+
*default_headers,
304+
"baggage",
305+
"sentry-trace",
306+
)
307+
273308
# Configure default CSP rules for different source types
274309
CSP_BLOB = "blob:"
275310
CSP_DATA = "data:"

0 commit comments

Comments
 (0)