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(hogql): export database build duration metric (#114014)

Jovan Sakovicd1da327fe9e6
diff --git a/posthog/hogql/database/database.py b/posthog/hogql/database/database.py
index 752717bb0d65..b7cb635a5f01 100644
--- a/posthog/hogql/database/database.py
+++ b/posthog/hogql/database/database.py
@@ -172,6 +172,7 @@ from posthog.hogql.parser import parse_expr
from posthog.hogql.timings import HogQLTimings
from posthog.exceptions_capture import capture_exception
+from posthog.otel_metrics import OtelInstrumentFactory
from posthog.ph_client import feature_enabled_or_false
from posthog.schema_enums import DatabaseSerializedFieldType, PersonsOnEventsMode, SessionTableVersion
from posthog.scopes import APIScopeObject
@@ -218,6 +219,7 @@ if TYPE_CHECKING:
)
tracer = trace.get_tracer(__name__)
+_OTEL_DATABASE = OtelInstrumentFactory("hogql.database")
@dataclasses.dataclass
@@ -1734,6 +1736,8 @@ class Database(BaseModel):
Query execution must omit schema_table_names because it can prune unrelated warehouse tables.
"""
+ build_started = time.perf_counter()
+
if timings is None:
timings = HogQLTimings()
@@ -1754,7 +1758,7 @@ class Database(BaseModel):
SOURCES_CACHE_EVENTS.labels(result="bypass").inc()
def fetch_fresh() -> HogQLDatabaseSources:
- with HOGQL_DATABASE_BUILD_DURATION_SECONDS.labels(phase="fetch_sources").time():
+ with _OTEL_DATABASE.timed_histogram_twin(HOGQL_DATABASE_BUILD_DURATION_SECONDS, {"phase": "fetch_sources"}):
return Database._fetch_sources(
team_id,
team=team,
@@ -1792,11 +1796,18 @@ class Database(BaseModel):
is_hogql_warehouse_access_control_enabled=_evaluate_warehouse_access_control_flag(cast("Team", team)),
)
- with HOGQL_DATABASE_BUILD_DURATION_SECONDS.labels(phase="build_from_sources").time():
- return Database._build_from_sources(
+ with _OTEL_DATABASE.timed_histogram_twin(
+ HOGQL_DATABASE_BUILD_DURATION_SECONDS, {"phase": "build_from_sources"}
+ ):
+ database = Database._build_from_sources(
sources, timings=timings, build_postgres_foreign_keys=build_postgres_foreign_keys
)
+ total_seconds = time.perf_counter() - build_started
+ HOGQL_DATABASE_BUILD_DURATION_SECONDS.labels(phase="total").observe(total_seconds)
+ _OTEL_DATABASE.record_histogram_twin(HOGQL_DATABASE_BUILD_DURATION_SECONDS, total_seconds, {"phase": "total"})
+ return database
+
@staticmethod
def _sources_cache_key(
*,
diff --git a/posthog/hogql/database/test/test_database.py b/posthog/hogql/database/test/test_database.py
index c30b9b072d77..c96963963e61 100644
--- a/posthog/hogql/database/test/test_database.py
+++ b/posthog/hogql/database/test/test_database.py
@@ -393,6 +393,16 @@ class TestDatabase(BaseTest, QueryMatchingTest):
Database.create_for(team_id=missing_team_id)
self.assertIn(str(missing_team_id), str(cm.exception))
+ def test_create_hogql_database_records_duration_metrics(self):
+ with patch("posthog.hogql.database.database._OTEL_DATABASE.record_histogram_twin") as record:
+ Database.create_for(team=self.team)
+
+ assert {call.args[2]["phase"] for call in record.call_args_list} == {
+ "fetch_sources",
+ "build_from_sources",
+ "total",
+ }
+
@pytest.mark.usefixtures("unittest_snapshot")
def test_serialize_database_no_person_on_events(self):
with override_settings(PERSON_ON_EVENTS_V2_OVERRIDE=False):