Skip to content

: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.

feat(api): record postgres query count per api route (#126313)

Paul D'Ambra65a0252d62ed
diff --git a/posthog/middleware.py b/posthog/middleware.py
index 851ad203d8e7..b683fb2ec913 100644
--- a/posthog/middleware.py
+++ b/posthog/middleware.py
@@ -5,7 +5,7 @@ import time
import uuid
import posixpath
from collections.abc import Callable
-from contextlib import suppress
+from contextlib import nullcontext, suppress
from datetime import UTC, datetime, timedelta
from enum import StrEnum
from ipaddress import ip_address, ip_network
@@ -29,6 +29,7 @@ from django.utils.deprecation import MiddlewareMixin
from django.utils.http import http_date, url_has_allowed_host_and_scheme
import structlog
+import posthoganalytics
from django_prometheus.middleware import Metrics
from loginas.utils import is_impersonated_session, restore_original_login
from opentelemetry import trace
@@ -638,19 +639,30 @@ class QueryTimeCountingMiddleware:
"properties",
"person",
]
+ LABELLED_METHODS = frozenset({"GET", "POST", "PUT", "PATCH", "DELETE"})
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request: HttpRequest):
- if not settings.CAPTURE_TIME_TO_SEE_DATA or not self._should_instrument(request):
+ add_server_timing = settings.CAPTURE_TIME_TO_SEE_DATA and self._should_instrument(request)
+ record_pg_queries = request.path.startswith("/api/")
+ if not add_server_timing and not record_pg_queries:
return self.get_response(request)
pg_query_counter, ch_query_counter = QueryCounter(), QueryCounter()
start_time = time.perf_counter()
- with connection.execute_wrapper(pg_query_counter), clickhouse_query_counter(ch_query_counter):
+ with (
+ connection.execute_wrapper(pg_query_counter),
+ clickhouse_query_counter(ch_query_counter) if add_server_timing else nullcontext(),
+ ):
response: HttpResponse = self.get_response(request)
+ if record_pg_queries:
+ self._record_pg_queries(request, response, pg_query_counter.count)
+ if not add_server_timing:
+ return response
+
server_timing = self._construct_header(
durations_ms={
"django": (time.perf_counter() - start_time) * 1000,
@@ -671,6 +683,25 @@ class QueryTimeCountingMiddleware:
response.headers["Server-Timing"] = server_timing
return response
+ def _record_pg_queries(self, request: HttpRequest, response: HttpResponse, query_count: int) -> None:
+ match = request.resolver_match
+ client = posthoganalytics.default_client
+ if match is None or client is None:
+ return
+ method = request.method or ""
+ client.metrics.histogram(
+ "api.request.pg_queries",
+ query_count,
+ unit="{query}",
+ attributes={
+ # The URLconf bounds view_name. The raw path carries ids, so it must never be a label.
+ "route": match.view_name,
+ # A client can send any method token, so unknown methods share one label.
+ "method": method if method in self.LABELLED_METHODS else "other",
+ "status_class": f"{response.status_code // 100}xx",
+ },
+ )
+
def _construct_header(self, durations_ms: dict[str, float], counts: dict[str, int]) -> str:
parts = [f"{key};dur={round(value)}" for key, value in durations_ms.items()]
parts += [f'{key};desc="{value}"' for key, value in counts.items()]
diff --git a/posthog/test/test_middleware.py b/posthog/test/test_middleware.py
index ffa25ff7be37..6ba4cd054cd5 100644
--- a/posthog/test/test_middleware.py
+++ b/posthog/test/test_middleware.py
@@ -12,6 +12,7 @@ from unittest.mock import MagicMock, patch
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.core.cache import cache
+from django.db import connection
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.test import (
AsyncClient,
@@ -3022,6 +3023,50 @@ def test_query_time_counting_middleware_should_instrument(path, expected) -> Non
assert middleware._should_instrument(request) is expected
+def _insight_detail(request: HttpRequest, project_id: int, pk: int) -> HttpResponse:
+ assert len(connection.execute_wrappers) == 1
+ return HttpResponse()
+
+
+class _PgQueryCountRoutes:
+ urlpatterns = [path("api/projects/<int:project_id>/insights/<int:pk>/", _insight_detail, name="insight-detail")]
+
+
+@override_settings(ROOT_URLCONF=_PgQueryCountRoutes, MIDDLEWARE=["posthog.middleware.QueryTimeCountingMiddleware"])
+class TestQueryTimeCountingMiddlewarePgQueryMetric(SimpleTestCase):
+ @parameterized.expand(
+ [
+ ("server_timing_on", True, "GET", "GET"),
+ ("server_timing_off", False, "POST", "POST"),
+ ("unknown_method", False, "PURGE", "other"),
+ ]
+ )
+ def test_records_route_name_not_path(
+ self, _name: str, capture_time_to_see_data: bool, method: str, expected_method_label: str
+ ) -> None:
+ client = MagicMock()
+ with (
+ override_settings(CAPTURE_TIME_TO_SEE_DATA=capture_time_to_see_data),
+ patch("posthog.middleware.posthoganalytics.default_client", client),
+ ):
+ self.client.generic(method, "/api/projects/2/insights/7/")
+
+ client.metrics.histogram.assert_called_once_with(
+ "api.request.pg_queries",
+ 0,
+ unit="{query}",
+ attributes={"route": "insight-detail", "method": expected_method_label, "status_class": "2xx"},
+ )
+
+ @parameterized.expand([("unresolved_api_path", "/api/unknown/"), ("non_api_path", "/insights/7/")])
+ def test_skips_requests_without_an_api_route(self, _name: str, path_: str) -> None:
+ client = MagicMock()
+ with patch("posthog.middleware.posthoganalytics.default_client", client):
+ self.client.get(path_)
+
+ client.metrics.histogram.assert_not_called()
+
+
class TestPerRequestLoggingContextMiddlewareMcpHeaders(SimpleTestCase):
"""Covers MCP session header binding inside `per_request_logging_context_middleware`."""