: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(metrics): expose metric_events in HogQL (#66169)
Daniel Viscac55c6152fe27
diff --git a/posthog/hogql/database/database.py b/posthog/hogql/database/database.pyindex 60176a95d577..6b9428e54f27 100644--- a/posthog/hogql/database/database.py+++ b/posthog/hogql/database/database.py@@ -99,7 +99,13 @@ from posthog.hogql.database.schema.logs import LogAttributesTable, LogsKafkaMetrfrom posthog.hogql.database.schema.marketing_conversions_preaggregated import MarketingConversionsPreaggregatedTablefrom posthog.hogql.database.schema.marketing_costs_preaggregated import MarketingCostsPreaggregatedTablefrom posthog.hogql.database.schema.marketing_touchpoints_preaggregated import MarketingTouchpointsPreaggregatedTable-from posthog.hogql.database.schema.metrics import MetricAttributesTable, MetricsKafkaMetricsTable, MetricsTable+from posthog.hogql.database.schema.metrics import (+ MetricAttributesTable,+ MetricSamplesTable,+ MetricSeriesTable,+ MetricsKafkaMetricsTable,+ MetricsTable,+)from posthog.hogql.database.schema.numbers import NumbersTablefrom posthog.hogql.database.schema.person_distinct_id_overrides import (PersonDistinctIdOverridesTable,@@ -375,6 +381,8 @@ def _construct_database_root_node(*, include_posthog_tables: bool) -> TableNode:name="session_replay_features", table=SessionReplayFeaturesTable()),"metrics": TableNode(name="metrics", table=MetricsTable()),+ "metric_samples": TableNode(name="metric_samples", table=MetricSamplesTable()),+ "metric_series": TableNode(name="metric_series", table=MetricSeriesTable()),"metric_attributes": TableNode(name="metric_attributes", table=MetricAttributesTable()),"metrics_kafka_metrics": TableNode(name="metrics_kafka_metrics", table=MetricsKafkaMetricsTable()),"error_tracking_fingerprint_issue_state": TableNode(diff --git a/posthog/hogql/database/schema/metrics.py b/posthog/hogql/database/schema/metrics.pyindex 89ed6ff4e102..0f3614439dc1 100644--- a/posthog/hogql/database/schema/metrics.py+++ b/posthog/hogql/database/schema/metrics.py@@ -102,6 +102,71 @@ class MetricsTable(Table):return "metrics"+class MetricSamplesTable(Table):+ description: str = "Raw metric emissions: one tiny row per sample (value + timestamp), keyed to a series via `series_fingerprint` and carrying an optional `trace_id` for the metric->trace pivot. Join to `metric_series` for labels. Distinct from `metrics`, which is pre-aggregated."+ workload: Workload | None = Workload.LOGS++ fields: dict[str, FieldOrTable] = {+ "team_id": IntegerDatabaseField(name="team_id", nullable=False),+ "metric_name": StringDatabaseField(name="metric_name", nullable=False),+ "series_fingerprint": IntegerDatabaseField(+ name="series_fingerprint",+ nullable=False,+ description="Hash of the series' label set; join key to `metric_series`.",+ ),+ "timestamp": DateTimeDatabaseField(+ name="timestamp", nullable=False, description="When the metric was emitted (UTC)."+ ),+ "value": FloatDatabaseField(name="value", nullable=False, description="The emitted value."),+ "trace_id": StringDatabaseField(+ name="trace_id",+ nullable=False,+ description="Trace this emission belongs to; empty if none. Pivot to spans/logs.",+ ),+ "span_id": StringDatabaseField(name="span_id", nullable=False),+ "trace_flags": IntegerDatabaseField(name="trace_flags", nullable=False),+ }++ def to_printed_clickhouse(self, context):+ return "metric_samples"++ def to_printed_hogql(self):+ return "metric_samples"+++class MetricSeriesTable(Table):+ description: str = "One row per unique metric series (metric + label set), keyed by `series_fingerprint`. Labels are stored here once and joined to `metric_samples` at query time."+ workload: Workload | None = Workload.LOGS++ fields: dict[str, FieldOrTable] = {+ "team_id": IntegerDatabaseField(name="team_id", nullable=False),+ "metric_name": StringDatabaseField(name="metric_name", nullable=False),+ "series_fingerprint": IntegerDatabaseField(+ name="series_fingerprint",+ nullable=False,+ description="Hash of the label set; join key from `metric_samples`.",+ ),+ "metric_type": StringDatabaseField(+ name="metric_type",+ nullable=False,+ description="OTel metric type (gauge, sum, histogram, summary, exponential_histogram).",+ ),+ "unit": StringDatabaseField(name="unit", nullable=False),+ "service_name": StringDatabaseField(name="service_name", nullable=False),+ "resource_attributes": MapStringDatabaseField(name="resource_attributes", nullable=False),+ "attributes": MapStringDatabaseField(name="attributes", nullable=False),+ "last_seen": DateTimeDatabaseField(+ name="last_seen", nullable=False, description="Most recent sample timestamp seen for this series."+ ),+ }++ def to_printed_clickhouse(self, context):+ return "metric_series"++ def to_printed_hogql(self):+ return "metric_series"++class MetricAttributesTable(Table):description: str = "Distinct metric attribute key/value pairs with occurrence counts, used to power metric attribute autocomplete and faceting."workload: Workload | None = Workload.LOGS