: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
chore(hogql): improve create_hogql_database span instrumentation (#61865)
Robbieb6da039791b5
diff --git a/posthog/hogql/database/database.py b/posthog/hogql/database/database.pyindex 7cde87395659..335dc5fec44f 100644--- a/posthog/hogql/database/database.py+++ b/posthog/hogql/database/database.py@@ -893,6 +893,8 @@ class Database(BaseModel):if timings is None:timings = HogQLTimings()+ db_span = trace.get_current_span()+from posthog.hogql.query import create_default_modifiers_for_teamfrom posthog.models import Team@@ -913,9 +915,7 @@ class Database(BaseModel):# Team is definitely not None at this point, make mypy believe thatteam = cast("Team", team)- # Set team_id for the create_hogql_database tracing span- span = trace.get_current_span()- span.set_attribute("team_id", team.pk)+ db_span.set_attribute("team_id", team.pk)with timings.measure("feature_flags", emit_span=True):is_managed_viewset_enabled = posthoganalytics.feature_enabled(@@ -1171,7 +1171,7 @@ class Database(BaseModel):def to_printed_clickhouse(self, context):return self.parent_table.to_printed_clickhouse(context)- with timings.measure("select"):+ with timings.measure("select", emit_span=True):tables_query = (# `queryable()` drops soft-deleted tables and orphans left by a soft-deleted# source, so an orphan can't shadow the live table sharing its name.@@ -1200,66 +1200,70 @@ class Database(BaseModel):connection_id=cast(str, database._connection_id),)]- sync_warnings_now = datetime.now(UTC)- for table in tables:- if (- not database._is_direct_query()- and table.external_data_source- and table.external_data_source.access_method == ExternalDataSource.AccessMethod.DIRECT- ):- continue-- with timings.measure(f"table_{table.name}"):- s3_table = table.hogql_definition(modifiers)+ with timings.measure("build_tables", emit_span=True):+ sync_warnings_now = datetime.now(UTC)+ for table in tables:+ if (+ not database._is_direct_query()+ and table.external_data_source+ and table.external_data_source.access_method == ExternalDataSource.AccessMethod.DIRECT+ ):+ continue- sync_warnings = get_warehouse_sync_warnings(table, now=sync_warnings_now)- if sync_warnings:- database._data_warehouse_sync_warnings[str(table.id)] = sync_warnings- primary_table = s3_table+ with timings.measure(f"table_{table.name}"):+ s3_table = table.hogql_definition(modifiers)- # If the warehouse table has no _properties_ field, then set it as a virtual table- if s3_table.fields.get("properties") is None:- s3_table.fields["properties"] = WarehousePropertiesVirtualTable(- fields=s3_table.fields, parent_table=s3_table, hidden=True- )+ sync_warnings = get_warehouse_sync_warnings(table, now=sync_warnings_now)+ if sync_warnings:+ database._data_warehouse_sync_warnings[str(table.id)] = sync_warnings+ primary_table = s3_table- if table.external_data_source:- if not database._is_direct_query():- warehouse_tables.add_child(TableNode(name=table.name, table=s3_table))- else:- self_managed_warehouse_tables.add_child(TableNode(name=table.name, table=s3_table))-- if table.external_data_source:- for index, table_key in enumerate(- _get_warehouse_table_keys(table, direct_query=database._is_direct_query())- ):- table_for_key = s3_table if index == 0 else s3_table.model_copy(deep=True)- table_chain = table_key.split(".")- table_conflict_mode: Literal["override", "ignore"] = (- "override"- if database._is_direct_query()- and table.external_data_source- and table.external_data_source.access_method == ExternalDataSource.AccessMethod.DIRECT- else "ignore"+ # If the warehouse table has no _properties_ field, then set it as a virtual table+ if s3_table.fields.get("properties") is None:+ s3_table.fields["properties"] = WarehousePropertiesVirtualTable(+ fields=s3_table.fields, parent_table=s3_table, hidden=True)- # For a chain of type a.b.c, we want to create a nested table node- # where a is the parent, b is the child of a, and c is the child of b- # where a.b.c will contain the table- warehouse_tables.add_child(- TableNode.create_nested_for_chain(table_chain, table_for_key),- table_conflict_mode=table_conflict_mode,- )+ if table.external_data_source:+ if not database._is_direct_query():+ warehouse_tables.add_child(TableNode(name=table.name, table=s3_table))+ else:+ self_managed_warehouse_tables.add_child(TableNode(name=table.name, table=s3_table))++ if table.external_data_source:+ for index, table_key in enumerate(+ _get_warehouse_table_keys(table, direct_query=database._is_direct_query())+ ):+ table_for_key = s3_table if index == 0 else s3_table.model_copy(deep=True)+ table_chain = table_key.split(".")+ table_conflict_mode: Literal["override", "ignore"] = (+ "override"+ if database._is_direct_query()+ and table.external_data_source+ and table.external_data_source.access_method+ == ExternalDataSource.AccessMethod.DIRECT+ else "ignore"+ )++ # For a chain of type a.b.c, we want to create a nested table node+ # where a is the parent, b is the child of a, and c is the child of b+ # where a.b.c will contain the table+ warehouse_tables.add_child(+ TableNode.create_nested_for_chain(table_chain, table_for_key),+ table_conflict_mode=table_conflict_mode,+ )++ joined_table_chain = ".".join(table_chain)+ table_for_key.name = joined_table_chain+ warehouse_tables_dot_notation_mapping[joined_table_chain] = table.name+ if table.external_data_source.access_method == ExternalDataSource.AccessMethod.DIRECT:+ database._direct_access_warehouse_table_names.add(joined_table_chain)+ if index == 0:+ primary_table = table_for_key- joined_table_chain = ".".join(table_chain)- table_for_key.name = joined_table_chain- warehouse_tables_dot_notation_mapping[joined_table_chain] = table.name- if table.external_data_source.access_method == ExternalDataSource.AccessMethod.DIRECT:- database._direct_access_warehouse_table_names.add(joined_table_chain)- if index == 0:- primary_table = table_for_key+ warehouse_tables_to_process.append((primary_table, table))- warehouse_tables_to_process.append((primary_table, table))+ db_span.set_attribute("warehouse_table_count", len(tables))def define_mappings(root_node: TableNode, get_table: Callable):table: Table | None = None