: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
feat(web): log per-request worker RSS to surface memory growth (#65739)
Paul D'Ambraf0dce95aa29d
diff --git a/posthog/middleware.py b/posthog/middleware.pyindex d78ce48d6542..95f013c9ed23 100644--- a/posthog/middleware.py+++ b/posthog/middleware.py@@ -1,3 +1,4 @@+import osimport reimport jsonimport time@@ -64,6 +65,40 @@ from products.product_analytics.backend.models.insight import Insightfrom .auth import PersonalAPIKeyAuthentication+logger = structlog.get_logger(__name__)++# A finished request that grew the worker's RSS by at least this many MiB is logged as+# `request_memory_growth`. Tunable via env so the threshold can be tightened/relaxed in+# prod without a worker restart / code change. Set to 0 to log every request's growth+# (noisy — debugging only). Parsed defensively: a malformed value must never crash the+# module at import (which would take the worker down before it serves a request) — fall+# back to the default instead.+try:+ REQUEST_RSS_GROWTH_LOG_MB = float(os.getenv("WEB_REQUEST_RSS_GROWTH_LOG_MB", "100"))+except ValueError:+ REQUEST_RSS_GROWTH_LOG_MB = 100.0++# Page size is invariant for the lifetime of the process — hoist it so current_rss_mb()+# doesn't pay the sysconf syscall on every call. Fall back to 4096 (the common default)+# if sysconf is unavailable on the host (e.g. an exotic OS).+try:+ _PAGE_SIZE_BYTES: int = os.sysconf("SC_PAGE_SIZE")+except (OSError, ValueError):+ _PAGE_SIZE_BYTES = 4096+++def current_rss_mb() -> float | None:+ """Resident set size of the current worker process in MiB, read from+ /proc/self/statm (field 2 = resident pages). Returns None when unavailable,+ e.g. on non-Linux dev machines where /proc is absent."""+ try:+ with open("/proc/self/statm") as statm:+ resident_pages = int(statm.read().split()[1])+ except (OSError, ValueError, IndexError):+ return None+ return resident_pages * _PAGE_SIZE_BYTES / (1024 * 1024)++ALWAYS_ALLOWED_ENDPOINTS = ["static","_health",@@ -668,10 +703,19 @@ def per_request_logging_context_middleware(# roll out CloudFront in front of app.posthog.com. We can get the host# header from NGINX, but we really want to have a way to get to the# team_id given a host header, and we can't do that with NGINX.+ # Capture the worker's RSS at the start of the request and bind it to the+ # logging context, so it rides along on `request_started`/`request_finished`.+ # A worker OOM-killed mid-request (SIGKILL, uncatchable) never logs+ # `request_finished` — but its `request_started` line carries `rss_mb` and the+ # `worker_pid`, so the request in flight when a worker died is identifiable+ # after the fact. See `request_memory_growth` below for the finished-request view.+ rss_mb_start = current_rss_mb()structlog.contextvars.bind_contextvars(host=request.headers.get("host", ""),container_hostname=settings.CONTAINER_HOSTNAME,x_forwarded_for=request.headers.get("x-forwarded-for", ""),+ worker_pid=os.getpid(),+ rss_mb=round(rss_mb_start, 1) if rss_mb_start is not None else None,)# Forwarded by the PostHog MCP server (services/mcp) on every API call.@@ -706,7 +750,33 @@ def per_request_logging_context_middleware(if mcp_conversation_id:span.set_attribute("mcp.conversation_id", mcp_conversation_id)- return get_response(request)+ try:+ response = get_response(request)+ finally:+ # Flag requests that materially grew this worker's footprint. Steady+ # accumulation of these on a pod is the in-app fingerprint of the request+ # mix that walks RSS up to the cgroup limit and triggers the OOM kill.+ # This runs even when get_response raises — exception paths (large error+ # payloads, failed serialisation) are exactly the requests most likely to+ # spike RSS. worker_pid is added explicitly because django_structlog's+ # RequestMiddleware clears contextvars before this finally block fires.+ rss_mb_end = current_rss_mb()+ if (+ rss_mb_start is not None+ and rss_mb_end is not None+ and rss_mb_end - rss_mb_start >= REQUEST_RSS_GROWTH_LOG_MB+ ):+ logger.warning(+ "request_memory_growth",+ rss_mb_start=round(rss_mb_start, 1),+ rss_mb_end=round(rss_mb_end, 1),+ rss_mb_delta=round(rss_mb_end - rss_mb_start, 1),+ request_path=request.path,+ method=request.method,+ worker_pid=os.getpid(),+ )++ return responsereturn middlewarediff --git a/posthog/test/test_request_memory.py b/posthog/test/test_request_memory.pynew file mode 100644index 000000000000..763afca8bdd7--- /dev/null+++ b/posthog/test/test_request_memory.py@@ -0,0 +1,24 @@+import sys++import pytest++from posthog.middleware import current_rss_mb++[email protected](sys.platform != "linux", reason="requires /proc (Linux only)")+def test_current_rss_mb_returns_positive_on_linux():+ # On the Linux CI runners /proc/self/statm exists, so this returns the live RSS.+ rss = current_rss_mb()+ # A running Python process always has a non-trivial resident set.+ assert rss is not None+ assert rss > 0+++def test_current_rss_mb_handles_missing_proc(monkeypatch):+ # On platforms without /proc (e.g. macOS dev machines) the read fails and we+ # degrade to None rather than raising into the request path.+ def _boom(*_args, **_kwargs):+ raise OSError("no /proc here")++ monkeypatch.setattr("builtins.open", _boom)+ assert current_rss_mb() is None