: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(db): record how long postgres connections take to open (#130370)
Thiago Salvatore2654eefeed53
diff --git a/posthog/db_backends/failopen/base.py b/posthog/db_backends/failopen/base.pyindex 39c976071389..527a39631eee 100644--- a/posthog/db_backends/failopen/base.py+++ b/posthog/db_backends/failopen/base.py@@ -1,8 +1,8 @@from __future__ import annotationsfrom django.db import InterfaceError, OperationalError-from django.db.backends.postgresql.base import DatabaseWrapper as PostgresDatabaseWrapper+from posthog.db_backends.postgresql.base import DatabaseWrapper as PostgresDatabaseWrapperfrom posthog.db_circuit_breaker import get_circuit_breakerdiff --git a/posthog/db_backends/postgresql/base.py b/posthog/db_backends/postgresql/base.pynew file mode 100644index 000000000000..51460591ede0--- /dev/null+++ b/posthog/db_backends/postgresql/base.py@@ -0,0 +1,23 @@+import time++from django.db.backends.postgresql.base import DatabaseWrapper as PostgresDatabaseWrapper++import posthoganalytics++CONNECTION_OPEN_METRIC = "postgres.connection.open.duration"+MICROSECONDS_PER_SECOND = 1_000_000+++class DatabaseWrapper(PostgresDatabaseWrapper):+ def connect(self) -> None:+ started_at = time.perf_counter()+ super().connect()+ client = posthoganalytics.default_client+ if client is None:+ return+ client.metrics.histogram(+ CONNECTION_OPEN_METRIC,+ (time.perf_counter() - started_at) * MICROSECONDS_PER_SECOND,+ unit="us",+ attributes={"alias": self.alias},+ )diff --git a/posthog/settings/data_stores.py b/posthog/settings/data_stores.pyindex f80dc3fa2cd0..70b533201779 100644--- a/posthog/settings/data_stores.py+++ b/posthog/settings/data_stores.py@@ -32,6 +32,8 @@ PERSON_TABLE_NAME: str = os.getenv("PERSON_TABLE_NAME", "posthog_person")# Database# https://docs.djangoproject.com/en/2.2/ref/settings/#databases+POSTGRES_ENGINE = "posthog.db_backends.postgresql"+def postgres_config(host: str) -> dict:"""Generate the config map we need for a postgres database.@@ -46,7 +48,7 @@ def postgres_config(host: str) -> dict:"""return {- "ENGINE": "django.db.backends.postgresql_psycopg2",+ "ENGINE": POSTGRES_ENGINE,"NAME": get_from_env("POSTHOG_DB_NAME"),"USER": os.getenv("POSTHOG_DB_USER", "postgres"),"PASSWORD": os.getenv("POSTHOG_DB_PASSWORD", ""),@@ -85,7 +87,7 @@ else:if DATABASE_URL:DATABASES: dict[str, dict[str, Any]] = {- "default": dict(dj_database_url.config(default=DATABASE_URL, conn_max_age=0))+ "default": dict(dj_database_url.config(default=DATABASE_URL, engine=POSTGRES_ENGINE, conn_max_age=0))}if DISABLE_SERVER_SIDE_CURSORS:diff --git a/posthog/test/test_postgresql_backend.py b/posthog/test/test_postgresql_backend.pynew file mode 100644index 000000000000..2ee675a60482--- /dev/null+++ b/posthog/test/test_postgresql_backend.py@@ -0,0 +1,19 @@+from unittest import mock++from django.db import connections+from django.test import TestCase++from posthog.db_backends.postgresql.base import CONNECTION_OPEN_METRIC+++class TestPostgresqlBackend(TestCase):+ def test_opening_a_connection_records_how_long_it_took(self) -> None:+ connection = connections.create_connection("default")+ self.addCleanup(connection.close)++ with mock.patch("posthog.db_backends.postgresql.base.posthoganalytics") as analytics:+ connection.connect()++ analytics.default_client.metrics.histogram.assert_called_once_with(+ CONNECTION_OPEN_METRIC, mock.ANY, unit="us", attributes={"alias": "default"}+ )