Skip to content

Commit bd94701

Browse files
JoshFergeandrewshie-sentry
authored andcommitted
fix(warmup): set correct subdomain on request (#89361)
we've been trying to figure out why our language warmup code hasn't been working ([profile](https://sentry.sentry.io/explore/profiling/profile/sentry/407f9ad7de474f8b83afce24fbafd7d4/flamegraph/?colorCoding=by%20system%20vs%20application%20frame&fov=23628372%2C15%2C53948248%2C14&frameName=SentryLocaleMiddleware.process_request&query=&sorting=call%20order&tid=136114459358912&view=top%20down)). I noticed in production the `SubdomainMiddleware` is actually rejecting the warmup request. ([link to GCP Log](https://console.cloud.google.com/logs/query;cursorTimestamp=2025-04-10T22:16:21.263953117Z;duration=PT3H;query=labels.%22k8s-pod%2Fservice%22%3D%22getsentry-control%22%0Aresource.labels.pod_name%3D%22getsentry-control-web-rpc-production-89b5f5cd7-h6mx8%22%0A-textPayload%3D~%22closing%20because%22%0Atimestamp%3D%222025-04-10T22:16:20.848716206Z%22%0AinsertId%3D%22p42bh00ozwtb4t3s%22;summaryFields=:true:32:beginning?project=internal-sentry)), so it's not actually running. Locally and in tests, the `setting.ALLOWED_HOSTS` variable is set to `*`, so it always works, but in prod, we actually set it to a value, which causes this request to get rejected. The imports were done outside of the function, which is why they were still having an effect. There is no easy way to set `ALLOWED_HOSTS` easily in the `test_wsgi.py` tests, but I confirmed behavior by setting my `ALLOWED_HOSTS` to `["getsentry.net"]` and verifying the test passes locally.
1 parent 19b5c20 commit bd94701

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/sentry/wsgi.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
# Run WSGI handler for the application
2121
application = WSGIHandler()
2222

23+
24+
warmup_host_name = settings.ALLOWED_HOSTS[0] if settings.ALLOWED_HOSTS[0] != "*" else "127.0.0.1"
25+
2326
# trigger a warmup of the application
2427
application(
2528
{
2629
"PATH_INFO": reverse("sentry-warmup"),
2730
"REQUEST_METHOD": "GET",
28-
"SERVER_NAME": "127.0.0.1",
31+
"SERVER_NAME": warmup_host_name,
2932
"SERVER_PORT": "9001",
3033
"wsgi.input": io.BytesIO(),
3134
"wsgi.url_scheme": "https",

tests/sentry/test_wsgi.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,31 @@ def test_wsgi_init():
4242
subprocess.check_call(
4343
[sys.executable, "-c", SUBPROCESS_TEST_WGI_WARMUP],
4444
)
45+
46+
47+
SUBPROCESS_TEST_WGI_WARMUP_WITH_SUBDOMAIN = f"""
48+
import sys
49+
from django.conf import settings
50+
from sentry.runner import configure
51+
configure()
52+
settings.ALLOWED_HOSTS = [".test.com"]
53+
54+
import sentry.wsgi
55+
{assert_in_sys_modules}
56+
import django.urls.resolvers
57+
from django.conf import settings
58+
resolver = django.urls.resolvers.get_resolver()
59+
assert resolver._populated is True
60+
for lang, _ in settings.LANGUAGES:
61+
assert lang in resolver._reverse_dict
62+
"""
63+
64+
65+
def test_wsgi_init_with_subdomain():
66+
"""
67+
In production, we have settings.ALLOWED_HOSTS set, so override it for this test
68+
and ensure that the wsgi.py file correctly pre-loads the application it set.
69+
"""
70+
subprocess.check_call(
71+
[sys.executable, "-c", SUBPROCESS_TEST_WGI_WARMUP_WITH_SUBDOMAIN],
72+
)

0 commit comments

Comments
 (0)