:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(clickhouse): tag web request queries with the request id (#121809)
Phil Haackfda0ee48a92a
diff --git a/posthog/middleware.py b/posthog/middleware.pyindex 348333e39596..c77f775a7711 100644--- a/posthog/middleware.py+++ b/posthog/middleware.py@@ -552,7 +552,6 @@ API_REQUESTS_LATENCY_SECONDS = Histogram(class CHQueries:def __init__(self, get_response):self.get_response = get_response- self.logger = structlog.get_logger(__name__)def __call__(self, request: HttpRequest):"""Install monkey-patch on demand.@@ -566,7 +565,7 @@ class CHQueries:user = cast(User, request.user)with suppress(Exception):- if request_id := structlog.get_context(self.logger).get("request_id"):+ if request_id := structlog.contextvars.get_contextvars().get("request_id"):tag_queries(http_request_id=uuid.UUID(request_id))tag_queries(diff --git a/posthog/test/test_middleware.py b/posthog/test/test_middleware.pyindex 9ac1aeff7e9f..db5ef5169584 100644--- a/posthog/test/test_middleware.py+++ b/posthog/test/test_middleware.py@@ -14,11 +14,12 @@ from django.contrib.auth.models import AnonymousUserfrom django.core.cache import cachefrom django.http import HttpRequest, HttpResponse, HttpResponseRedirectfrom django.test import (+ AsyncClient,Client as DjangoClient,RequestFactory,SimpleTestCase,)-from django.urls import reverse+from django.urls import path, reverseimport structlogfrom loginas import settings as la_settings@@ -30,6 +31,7 @@ from social_core.exceptions import AuthCanceled, AuthFailed, AuthMissingParametefrom posthog.api.test.test_organization import create_organizationfrom posthog.api.test.test_team import create_team+from posthog.clickhouse.query_tagging import get_query_tagsfrom posthog.constants import AvailableFeaturefrom posthog.middleware import (ActivityLoggingMiddleware,@@ -2640,6 +2642,30 @@ def test_chqueries_middleware_tags_source(user_agent, expected_source):assert captured["feature"] is None+def _http_request_id_probe(request: HttpRequest) -> HttpResponse:+ return HttpResponse(str(get_query_tags().http_request_id))+++urlpatterns = [path("api/_http_request_id_probe/", _http_request_id_probe)]+++ "header_value,expected_tag",+ [+ pytest.param("0b8a6a4e-4c1f-4d2e-9a57-3f6f0f6c2b11", "0b8a6a4e-4c1f-4d2e-9a57-3f6f0f6c2b11", id="uuid"),+ # django-structlog binds the client's X-Request-ID header without checking that it is a UUID.+ pytest.param("not-a-uuid", "None", id="not_a_uuid"),+ ],+)+@override_settings(ROOT_URLCONF=__name__)+async def test_chqueries_middleware_tags_http_request_id_under_asgi(header_value, expected_tag):+ # AsyncClient runs settings.MIDDLEWARE through the ASGI handler, as production does.+ # There, django-structlog binds request_id on the event loop. CHQueries reads it from a worker thread.+ response = await AsyncClient().get("/api/_http_request_id_probe/", headers={"X-Request-ID": header_value})++ assert response.content.decode() == expected_tag++@parameterized.expand([("api_path_emits", "/api/projects/@current/query/", True),