Skip to content

Commit 5c0f43c

Browse files
committed
Link Sentry all the way
1 parent 51073d7 commit 5c0f43c

5 files changed

Lines changed: 50 additions & 5 deletions

File tree

astra_app/core/context_processors.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from email.utils import parseaddr
22

3+
import sentry_sdk
34
from django.conf import settings
45
from django.contrib.staticfiles.storage import staticfiles_storage
56
from django.urls import reverse
@@ -47,6 +48,8 @@ def chat_networks(_request) -> dict[str, object]:
4748
def build_info(_request) -> dict[str, object]:
4849
sentry_browser_bundle_src = ""
4950
sentry_browser_config: dict[str, object] | None = None
51+
sentry_trace = ""
52+
sentry_baggage = ""
5053
if settings.SENTRY_DSN:
5154
sentry_browser_bundle_src = staticfiles_storage.url("core/vendor/sentry/bundle.tracing.min.js")
5255
sentry_browser_config = {
@@ -56,12 +59,16 @@ def build_info(_request) -> dict[str, object]:
5659
"tracesSampleRate": settings.SENTRY_TRACES_SAMPLE_RATE,
5760
"tunnel": reverse("sentry-browser-tunnel"),
5861
}
62+
sentry_trace = sentry_sdk.get_traceparent() or ""
63+
sentry_baggage = sentry_sdk.get_baggage() or ""
5964

6065
return {
6166
"build_sha": get_build_sha(),
6267
"default_from_email_address": parseaddr(settings.DEFAULT_FROM_EMAIL)[1].strip(),
6368
"sentry_browser_bundle_src": sentry_browser_bundle_src,
6469
"sentry_browser_config": sentry_browser_config,
70+
"sentry_trace": sentry_trace,
71+
"sentry_baggage": sentry_baggage,
6572
}
6673

6774

astra_app/core/templates/core/base.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
<link rel="stylesheet" href="{% static 'core/css/login.css' %}" />
2020
{% endif %}
2121
{% if sentry_browser_bundle_src and sentry_browser_config %}
22+
{% if sentry_trace %}<meta name="sentry-trace" content="{{ sentry_trace }}">{% endif %}
23+
{% if sentry_baggage %}<meta name="baggage" content="{{ sentry_baggage }}">{% endif %}
2224
{{ sentry_browser_config|json_script:"sentry-browser-config" }}
2325
<script src="{{ sentry_browser_bundle_src }}"></script>
2426
<script>

astra_app/core/tests/test_sentry_browser_templates.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pathlib import Path
12
from types import SimpleNamespace
23
from unittest.mock import patch
34

@@ -20,7 +21,11 @@ def test_vendored_sentry_bundle_sourcemap_exists(self) -> None:
2021
SENTRY_TRACES_SAMPLE_RATE=0.25,
2122
)
2223
def test_build_info_exposes_sentry_browser_bundle_and_config(self) -> None:
23-
context = build_info(SimpleNamespace())
24+
with patch("core.context_processors.sentry_sdk.get_traceparent", return_value="trace-parent"), patch(
25+
"core.context_processors.sentry_sdk.get_baggage",
26+
return_value="sentry-sample=value",
27+
):
28+
context = build_info(SimpleNamespace())
2429

2530
self.assertEqual(
2631
context["sentry_browser_bundle_src"],
@@ -36,13 +41,17 @@ def test_build_info_exposes_sentry_browser_bundle_and_config(self) -> None:
3641
"tunnel": "/_ci/envelope/",
3742
},
3843
)
44+
self.assertEqual(context["sentry_trace"], "trace-parent")
45+
self.assertEqual(context["sentry_baggage"], "sentry-sample=value")
3946

4047
@override_settings(SENTRY_DSN="")
4148
def test_build_info_omits_sentry_browser_config_without_dsn(self) -> None:
4249
context = build_info(SimpleNamespace())
4350

4451
self.assertEqual(context["sentry_browser_bundle_src"], "")
4552
self.assertIsNone(context["sentry_browser_config"])
53+
self.assertEqual(context["sentry_trace"], "")
54+
self.assertEqual(context["sentry_baggage"], "")
4655

4756

4857
class SentryBrowserTemplateTests(TestCase):
@@ -71,7 +80,13 @@ def test_profile_page_includes_sentry_browser_bundle_and_tunnel_config(self) ->
7180
},
7281
)
7382

74-
with patch("core.freeipa.user.FreeIPAUser.get", return_value=freeipa_user):
83+
with patch("core.freeipa.user.FreeIPAUser.get", return_value=freeipa_user), patch(
84+
"core.context_processors.sentry_sdk.get_traceparent",
85+
return_value="trace-parent",
86+
), patch(
87+
"core.context_processors.sentry_sdk.get_baggage",
88+
return_value="sentry-sample=value",
89+
):
7590
response = self.client.get(f"/user/{username}/")
7691

7792
self.assertEqual(response.status_code, 200)
@@ -80,8 +95,19 @@ def test_profile_page_includes_sentry_browser_bundle_and_tunnel_config(self) ->
8095
'src="/static/core/vendor/sentry/bundle.tracing.min.js"',
8196
)
8297
self.assertContains(response, 'id="sentry-browser-config"')
98+
self.assertContains(response, '<meta name="sentry-trace" content="trace-parent">', html=True)
99+
self.assertContains(response, '<meta name="baggage" content="sentry-sample=value">', html=True)
83100
self.assertContains(response, "window.Sentry && window.Sentry.init")
84101
self.assertContains(response, '"environment": "staging"')
85102
self.assertContains(response, '"release": "build-123"')
86103
self.assertContains(response, '"tracesSampleRate": 0.25')
87-
self.assertContains(response, '"tunnel": "/_ci/envelope/"')
104+
self.assertContains(response, '"tunnel": "/_ci/envelope/"')
105+
106+
107+
class SentryCaddyConfigTests(SimpleTestCase):
108+
def test_caddy_configs_forward_sentry_trace_headers(self) -> None:
109+
repo_root = Path(__file__).resolve().parents[3]
110+
for relative_path in ("infra/systemd/Caddyfile", "infra/systemd/Caddyfile.j2"):
111+
source = (repo_root / relative_path).read_text(encoding="utf-8")
112+
self.assertIn("header_up sentry-trace", source)
113+
self.assertIn("header_up baggage", source)

infra/systemd/Caddyfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ accounts.almalinux.org {
1010
lb_policy least_conn
1111
lb_retries 2
1212
header_up X-Forwarded-For {http.request.header.CF-Connecting-IP}
13+
header_up sentry-trace {http.request.header.sentry-trace}
14+
header_up baggage {http.request.header.baggage}
1315
health_uri /readyz
1416
}
1517

1618
reverse_proxy 127.0.0.1:8001 127.0.0.1:8002 {
1719
lb_policy least_conn
1820
lb_retries 2
21+
header_up sentry-trace {http.request.header.sentry-trace}
22+
header_up baggage {http.request.header.baggage}
1923
health_uri /readyz
2024
}
2125
}

infra/systemd/Caddyfile.j2

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
# Clients must trust Caddy's internal CA (or use curl -k).
2121
tls internal
2222

23-
reverse_proxy 127.0.0.1:8001 127.0.0.1:8002
23+
reverse_proxy 127.0.0.1:8001 127.0.0.1:8002 {
24+
header_up sentry-trace {http.request.header.sentry-trace}
25+
header_up baggage {http.request.header.baggage}
26+
}
2427
}
2528
{% else %}
2629
:80 {
@@ -32,6 +35,9 @@
3235
# Clients must trust Caddy's internal CA (or use curl -k).
3336
tls internal
3437

35-
reverse_proxy 127.0.0.1:8001 127.0.0.1:8002
38+
reverse_proxy 127.0.0.1:8001 127.0.0.1:8002 {
39+
header_up sentry-trace {http.request.header.sentry-trace}
40+
header_up baggage {http.request.header.baggage}
41+
}
3642
}
3743
{% endif %}

0 commit comments

Comments
 (0)