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.

chore(hogql): add latency metrics for editor-assist queries (#90101)

George Munyorob39408f2bb83
diff --git a/posthog/api/services/query.py b/posthog/api/services/query.py
index a3184674d40b..a4b5ba3a23e5 100644
--- a/posthog/api/services/query.py
+++ b/posthog/api/services/query.py
@@ -24,6 +24,7 @@ from posthog.hogql.compiler.bytecode import execute_hog
from posthog.hogql.constants import LimitContext
from posthog.hogql.context import HogQLContext
from posthog.hogql.direct_connection import resolve_database_for_connection
+from posthog.hogql.editor_assist_metrics import EDITOR_ASSIST_DURATION_SECONDS
from posthog.hogql.errors import ExposedHogQLError, ResolutionError
from posthog.hogql.metadata import get_hogql_metadata
from posthog.hogql.modifiers import create_default_modifiers_for_team
@@ -298,18 +299,20 @@ def process_query_model(
result: dict | BaseModel | RawCachedQueryResponse
if isinstance(query, HogQLAutocomplete):
- _, database = resolve_database_for_connection(
- team,
- query.connectionId,
- user=user,
- error_factory=ValidationError,
- modifiers=create_default_modifiers_for_team(team),
- )
- return get_hogql_autocomplete(query=query, team=team, database_arg=database, user=user)
+ with EDITOR_ASSIST_DURATION_SECONDS.labels(kind="autocomplete").time():
+ _, database = resolve_database_for_connection(
+ team,
+ query.connectionId,
+ user=user,
+ error_factory=ValidationError,
+ modifiers=create_default_modifiers_for_team(team),
+ )
+ return get_hogql_autocomplete(query=query, team=team, database_arg=database, user=user)
if isinstance(query, HogQLMetadata):
- metadata_query = HogQLMetadata.model_validate(query)
- return get_hogql_metadata(query=metadata_query, team=team, user=user)
+ with EDITOR_ASSIST_DURATION_SECONDS.labels(kind="metadata").time():
+ metadata_query = HogQLMetadata.model_validate(query)
+ return get_hogql_metadata(query=metadata_query, team=team, user=user)
if isinstance(query, DatabaseSchemaQuery):
return process_database_schema_query(team, query, user=user)
diff --git a/posthog/hogql/database/database.py b/posthog/hogql/database/database.py
index 4d9b9c9ab663..13e7903e5239 100644
--- a/posthog/hogql/database/database.py
+++ b/posthog/hogql/database/database.py
@@ -150,6 +150,7 @@ from posthog.hogql.database.schema.web_stats_preaggregated import WebStatsPreagg
from posthog.hogql.database.schema.web_vitals_paths_preaggregated import WebVitalsPathsPreaggregatedTable
from posthog.hogql.database.utils import get_join_field_chain, qualify_join_key_expr
from posthog.hogql.database.warehouse_join_resolvers import data_warehouse_resolver_params
+from posthog.hogql.editor_assist_metrics import HOGQL_DATABASE_BUILD_DURATION_SECONDS
from posthog.hogql.errors import QueryError, ResolutionError, TableAccessDeniedError
from posthog.hogql.modifiers import create_default_modifiers_for_team
from posthog.hogql.parser import parse_expr
@@ -1375,19 +1376,21 @@ class Database(BaseModel):
if timings is None:
timings = HogQLTimings()
- sources = Database._fetch_sources(
- team_id,
- team=team,
- user=user,
- user_access_control=user_access_control,
- modifiers=modifiers,
- timings=timings,
- connection_id=connection_id,
- bypass_warehouse_access_control=bypass_warehouse_access_control,
- )
- return Database._build_from_sources(
- sources, timings=timings, build_postgres_foreign_keys=build_postgres_foreign_keys
- )
+ with HOGQL_DATABASE_BUILD_DURATION_SECONDS.labels(phase="fetch_sources").time():
+ sources = Database._fetch_sources(
+ team_id,
+ team=team,
+ user=user,
+ user_access_control=user_access_control,
+ modifiers=modifiers,
+ timings=timings,
+ connection_id=connection_id,
+ bypass_warehouse_access_control=bypass_warehouse_access_control,
+ )
+ with HOGQL_DATABASE_BUILD_DURATION_SECONDS.labels(phase="build_from_sources").time():
+ return Database._build_from_sources(
+ sources, timings=timings, build_postgres_foreign_keys=build_postgres_foreign_keys
+ )
@staticmethod
def _fetch_sources(
diff --git a/posthog/hogql/editor_assist_metrics.py b/posthog/hogql/editor_assist_metrics.py
new file mode 100644
index 000000000000..3dd24e9d2417
--- /dev/null
+++ b/posthog/hogql/editor_assist_metrics.py
@@ -0,0 +1,15 @@
+from prometheus_client import Histogram
+
+EDITOR_ASSIST_DURATION_SECONDS = Histogram(
+ "hogql_editor_assist_duration_seconds",
+ "End-to-end server time of HogQL editor-assist queries (autocomplete and metadata)",
+ labelnames=["kind"],
+ buckets=(0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0),
+)
+
+HOGQL_DATABASE_BUILD_DURATION_SECONDS = Histogram(
+ "hogql_database_build_duration_seconds",
+ "Duration of HogQL database construction phases",
+ labelnames=["phase"],
+ buckets=(0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5),
+)