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(ingestion): publish metrics from the personhog shadow lane jobs (#108812)

Nick Best46611dc86e7a
diff --git a/posthog/dags/personhog_shadow_drift.py b/posthog/dags/personhog_shadow_drift.py
index b48fab3bb222..512bf2d70b1f 100644
--- a/posthog/dags/personhog_shadow_drift.py
+++ b/posthog/dags/personhog_shadow_drift.py
@@ -16,6 +16,7 @@ from contextlib import closing
import dagster
import psycopg2
+from prometheus_client import CollectorRegistry, Gauge
from posthog.dags.common import JobOwners
from posthog.dags.personhog_shadow_lane import (
@@ -32,6 +33,10 @@ from posthog.dags.personhog_shadow_lane import (
wait_for_quiescence,
)
from posthog.dataclasses import frozen
+from posthog.metrics import pushed_metrics_registry
+
+SETTLE_METRICS_JOB = "personhog_shadow_lane_settle"
+DRIFT_METRICS_JOB = "personhog_shadow_lane_drift"
@frozen
@@ -280,6 +285,63 @@ def compute_shadow_drift(connection: psycopg2.extensions.connection, sample_size
]
+def record_settle_gauges(
+ registry: CollectorRegistry, database: str, settle_seconds: float, completed_at: float
+) -> None:
+ Gauge(
+ "posthog_personhog_shadow_lane_settle_seconds",
+ "Time the shadow persons database took to stop receiving writes after the lane stopped",
+ ["database"],
+ registry=registry,
+ ).labels(database=database).set(settle_seconds)
+ Gauge(
+ "posthog_personhog_shadow_lane_settle_last_success_timestamp_seconds",
+ "Unix time when the shadow persons database last settled after a stop",
+ ["database"],
+ registry=registry,
+ ).labels(database=database).set(completed_at)
+
+
+def record_drift_gauges(
+ registry: CollectorRegistry, database: str, reports: list[DriftCategoryReport], completed_at: float
+) -> None:
+ def per_category(name: str, help_text: str) -> Gauge:
+ return Gauge(
+ f"posthog_personhog_shadow_lane_drift_{name}", help_text, ["database", "category"], registry=registry
+ )
+
+ legacy_rows = per_category("legacy_rows", "Live rows the legacy path holds")
+ personhog_rows = per_category("personhog_rows", "Live rows the personhog path holds")
+ missing_in_personhog = per_category("missing_in_personhog_rows", "Keys only the legacy path holds")
+ missing_in_legacy = per_category("missing_in_legacy_rows", "Keys only the personhog path holds")
+ mismatched = per_category("mismatched_rows", "Keys both paths hold with different content; excludes version")
+ ratio = per_category("ratio", "Drifted keys over all compared keys, from 0 to 1")
+ field_mismatched = Gauge(
+ "posthog_personhog_shadow_lane_drift_field_mismatched_rows",
+ "Keys both paths hold where one field differs. A key with two different fields counts in both",
+ ["database", "category", "field"],
+ registry=registry,
+ )
+
+ for report in reports:
+ labels = {"database": database, "category": report.category}
+ legacy_rows.labels(**labels).set(report.legacy_total)
+ personhog_rows.labels(**labels).set(report.personhog_total)
+ missing_in_personhog.labels(**labels).set(report.missing_in_personhog)
+ missing_in_legacy.labels(**labels).set(report.missing_in_legacy)
+ mismatched.labels(**labels).set(report.mismatched_rows)
+ ratio.labels(**labels).set(report.drift_pct / 100)
+ for field_name, count in report.field_mismatches.items():
+ field_mismatched.labels(field=field_name, **labels).set(count)
+
+ Gauge(
+ "posthog_personhog_shadow_lane_drift_last_success_timestamp_seconds",
+ "Unix time when the last drift report finished",
+ ["database"],
+ registry=registry,
+ ).labels(database=database).set(completed_at)
+
+
class ShadowLaneStopConfig(dagster.Config):
namespace: str = SHADOW_NAMESPACE
consumer_deployment: str = SHADOW_CONSUMER_DEPLOYMENT
@@ -356,6 +418,8 @@ def wait_for_shadow_settle(context: dagster.OpExecutionContext, config: ShadowSe
waited = time.monotonic() - started
context.log.info(f"Shadow persons database settled after {waited:.0f}s ({polls} polls)")
+ with pushed_metrics_registry(f"{SETTLE_METRICS_JOB}_{config.shadow_db_env_var}") as registry:
+ record_settle_gauges(registry, config.shadow_db_env_var, waited, time.time())
context.add_output_metadata({"settle_seconds": dagster.MetadataValue.float(round(waited, 1))})
@@ -364,6 +428,9 @@ def report_shadow_drift(context: dagster.OpExecutionContext, config: ShadowDrift
with closing(shadow_db_connection(config.shadow_db_env_var)) as connection:
reports = compute_shadow_drift(connection, config.sample_size)
+ with pushed_metrics_registry(f"{DRIFT_METRICS_JOB}_{config.shadow_db_env_var}") as registry:
+ record_drift_gauges(registry, config.shadow_db_env_var, reports, time.time())
+
header = "| category | legacy | personhog | missing in personhog | missing in legacy | mismatched | drift % |"
separator = "|---|---|---|---|---|---|---|"
lines = [header, separator]
diff --git a/posthog/dags/personhog_shadow_lane.py b/posthog/dags/personhog_shadow_lane.py
index 6b13bba79dd4..a1420394e748 100644
--- a/posthog/dags/personhog_shadow_lane.py
+++ b/posthog/dags/personhog_shadow_lane.py
@@ -31,15 +31,19 @@ from kubernetes import (
client as k8s_client,
config as k8s_config,
)
+from prometheus_client import CollectorRegistry, Gauge
from pydantic import Field
from posthog.dags.common import JobOwners
+from posthog.metrics import pushed_metrics_registry
SHADOW_NAMESPACE = "ingestion-analytics-team2-shadow"
SHADOW_CONSUMER_DEPLOYMENT = "ingestion-analytics-team2-shadow-consumer"
SHADOW_PROCESSOR_DEPLOYMENT = "ingestion-analytics-team2-shadow-processor"
SHADOW_DB_URL_ENV_VAR = "PERSONS_SHADOW_DB_URL"
+START_METRICS_JOB = "personhog_shadow_lane_start"
+
# Every table the lane writes, per path. The reset truncates both lists in one
# statement so a validation run starts from state where "row missing on one
# side" can only mean drift. Kept in sync with rust/persons_migrations/.
@@ -324,6 +328,29 @@ def _reset_shadow_state(
context.log.info("Shadow persons database reset complete")
+def record_start_gauges(registry: CollectorRegistry, config: ShadowLaneStartConfig, completed_at: float) -> None:
+ Gauge(
+ "posthog_personhog_shadow_lane_start_last_success_timestamp_seconds",
+ "Unix time when the shadow lane last started with every replica ready",
+ ["namespace"],
+ registry=registry,
+ ).labels(namespace=config.namespace).set(completed_at)
+ Gauge(
+ "posthog_personhog_shadow_lane_start_reset_state",
+ "1 when the last start truncated the shadow persons database first, 0 when it resumed from existing state",
+ ["namespace"],
+ registry=registry,
+ ).labels(namespace=config.namespace).set(1 if config.reset_state else 0)
+ replicas = Gauge(
+ "posthog_personhog_shadow_lane_start_replicas",
+ "Replicas the last start requested, by deployment",
+ ["namespace", "deployment"],
+ registry=registry,
+ )
+ replicas.labels(namespace=config.namespace, deployment=config.consumer_deployment).set(config.consumer_replicas)
+ replicas.labels(namespace=config.namespace, deployment=config.processor_deployment).set(config.processor_replicas)
+
+
@dagster.op
def start_shadow_lane(context: dagster.OpExecutionContext, config: ShadowLaneStartConfig) -> None:
apps = apps_api()
@@ -358,6 +385,9 @@ def start_shadow_lane(context: dagster.OpExecutionContext, config: ShadowLaneSta
)
)
+ with pushed_metrics_registry(f"{START_METRICS_JOB}_{config.namespace}") as registry:
+ record_start_gauges(registry, config, time.time())
+
context.add_output_metadata(
{
"reset_state": dagster.MetadataValue.bool(config.reset_state),
diff --git a/posthog/dags/tests/test_personhog_shadow_drift.py b/posthog/dags/tests/test_personhog_shadow_drift.py
index 55d9584bf49e..8053f347be85 100644
--- a/posthog/dags/tests/test_personhog_shadow_drift.py
+++ b/posthog/dags/tests/test_personhog_shadow_drift.py
@@ -4,8 +4,9 @@ import pytest
import psycopg2
import psycopg2.extras
+from prometheus_client import CollectorRegistry
-from posthog.dags.personhog_shadow_drift import compute_shadow_drift
+from posthog.dags.personhog_shadow_drift import DriftCategoryReport, compute_shadow_drift, record_drift_gauges
from posthog.persons_db import persons_db_url
TEAM_ID = 990000123
@@ -101,3 +102,59 @@ def test_compute_shadow_drift_counts_each_category() -> None:
assert (hash_keys.legacy_total, hash_keys.personhog_total) == (2, 2)
assert (hash_keys.missing_in_personhog, hash_keys.missing_in_legacy, hash_keys.mismatched_rows) == (0, 0, 1)
assert hash_keys.samples == [f"team={TEAM_ID} person={matched} flag=flag-2 hash_key_mismatch"]
+
+
+def test_drift_gauges_keep_each_count_under_its_own_name_and_category() -> None:
+ reports = [
+ DriftCategoryReport(
+ category="persons",
+ legacy_total=90,
+ personhog_total=95,
+ missing_in_personhog=5,
+ missing_in_legacy=10,
+ mismatched_rows=10,
+ field_mismatches={"properties": 7, "version": 40},
+ samples=[],
+ ),
+ DriftCategoryReport(
+ category="distinct_ids",
+ legacy_total=3,
+ personhog_total=3,
+ missing_in_personhog=0,
+ missing_in_legacy=0,
+ mismatched_rows=0,
+ field_mismatches={"version": 0},
+ samples=[],
+ ),
+ ]
+ registry = CollectorRegistry()
+
+ record_drift_gauges(registry, "PERSONS_SHADOW_DB_URL", reports, completed_at=1_700_000_000.0)
+
+ def sample(name: str, **labels: str) -> float | None:
+ return registry.get_sample_value(
+ f"posthog_personhog_shadow_lane_drift_{name}", {"database": "PERSONS_SHADOW_DB_URL", **labels}
+ )
+
+ assert {
+ name: sample(name, category="persons")
+ for name in (
+ "legacy_rows",
+ "personhog_rows",
+ "missing_in_personhog_rows",
+ "missing_in_legacy_rows",
+ "mismatched_rows",
+ "ratio",
+ )
+ } == {
+ "legacy_rows": 90,
+ "personhog_rows": 95,
+ "missing_in_personhog_rows": 5,
+ "missing_in_legacy_rows": 10,
+ "mismatched_rows": 10,
+ "ratio": 0.25,
+ }
+ assert sample("field_mismatched_rows", category="persons", field="version") == 40
+ assert sample("field_mismatched_rows", category="persons", field="properties") == 7
+ assert sample("ratio", category="distinct_ids") == 0
+ assert sample("last_success_timestamp_seconds") == 1_700_000_000.0