: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
fix(data-warehouse): attribute warehouse-only syncs to a destination (#110162)
Tom Owers07fa1e8574e4
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex 3f1d950ffaae..9d860dee5993 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -9659,13 +9659,13 @@ snapshots:scenes-app-errortracking-linkedreports--several-reports-across-states--light:hash: v1.k794b7964.fe2161a3bb0e898689ef58a342d071c353ded71ac270a62998ea566055359e7a.AnoCs1sNoTY6J1yl24yuZPh_1Jkjugz-jnYxHgY8ZyUscenes-app-etl--healthy--dark:- hash: v1.k794b7964.37c9a1fc5b577570e5e9b335d39c3d5ecdcffb1a79869be4c15b75fac4d08e5b.Kgr83CF5IOkKAdyHYcofo29cTHn0tlLMNOx9xlfhCB0+ hash: v1.k794b7964.2db2add131ea077b296ee62e52f0f468917a37a3046d184b0802ea94d0ebd7da.nyXMgbG9Is8OHWPI61v0qLBOb9XNLClA5uwrqSWh2Ygscenes-app-etl--healthy--light:- hash: v1.k794b7964.a101fbf2bb7570eabb56a2dd2a7b536f103f544ec1db0fe859c568c8e439828e.r3rhY6vYx3o1WC80g6MO8X8OiEuStYShcFzaCvqKO80+ hash: v1.k794b7964.40a3df65bf4b44f8ee1eaafe56fb8abecb48339f47a39a918c9af4c3dd1d393e.v8nVc5frK8uZ7XEhaAUMx0pTpApv1gJkWfsHPlVN04cscenes-app-etl--needs-attention--dark:- hash: v1.k794b7964.088f22663ddc7743a45263b97b06c63475f440b0553af54a9988593b394515e1.gf50EpD2gDMAKHLvHe08xAjgTN7RUxzejQ2XvGr3q9c+ hash: v1.k794b7964.a31167943bf4df4081dda74a532653ab9b72c578cca023907bd9a79c58713473.XwfOOC2wCuW7RemE5vUoD_l1_Yqb4ZA3JZvn5fkO9kMscenes-app-etl--needs-attention--light:- hash: v1.k794b7964.ece2af8c07b1e69d0a885ea37d63f7aeac57ac4b71702c46ee49feb133cde67f.0ZbyBXdGqfTo1qTzbqyYsOg08ePKZKveJTcFKz56bsg+ hash: v1.k794b7964.baf6c5d33e97cfba98e911746be731f313be945ecc91d06d3321bcaf7e9c0d78.5c-O2ZGQGWBrcoR-AigyGLsMeVtJLveQh819LyawMw0scenes-app-events--event-explorer--dark:hash: v1.k794b7964.3bf3b01d0972be5ad8fe4a7408d20b10e2431ea8f28874aa0a63f7623c1af97c.ONFJgVNjjG1f32eqlxRZuleSgdLpLGIcFhHN2S4yvhkscenes-app-events--event-explorer--light:diff --git a/products/data_warehouse/backend/presentation/views/data_warehouse.py b/products/data_warehouse/backend/presentation/views/data_warehouse.pyindex 91062a91c3c4..0a38fd54f857 100644--- a/products/data_warehouse/backend/presentation/views/data_warehouse.py+++ b/products/data_warehouse/backend/presentation/views/data_warehouse.py@@ -511,7 +511,7 @@ class DataWarehouseViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):cursor.execute("""WITH external_jobs AS (- SELECT edj.id, edsrc.source_type as type, eds.name, edj.status,+ SELECT edj.id, edsrc.source_type as type, COALESCE(NULLIF(eds.label, ''), eds.name) as name, edj.status,COALESCE(edj.rows_synced, 0) as rows, edj.created_at,edj.finished_at, edj.latest_error, edj.workflow_run_id,null as origin, edj.pipeline_id as source_id@@ -644,7 +644,7 @@ class DataWarehouseViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):cursor.execute("""WITH external_jobs AS (- SELECT edj.id, edsrc.source_type as type, eds.name, edj.status,+ SELECT edj.id, edsrc.source_type as type, COALESCE(NULLIF(eds.label, ''), eds.name) as name, edj.status,COALESCE(edj.rows_synced, 0) as rows, edj.created_at,edj.finished_at, edj.latest_error, edj.workflow_run_id,null as origin, edj.pipeline_id as source_id@@ -957,7 +957,8 @@ class DataWarehouseViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):results.append({"id": str(schema.id),- "name": schema.name,+ # A Slack schema's name is the raw channel id; `label` is the human one.+ "name": schema.label or schema.name,"type": "external_data_sync","source_type": schema.source.source_type if schema.source else None,"status": sync_status,diff --git a/products/warehouse_sources/backend/temporal/data_imports/metrics.py b/products/warehouse_sources/backend/temporal/data_imports/metrics.pyindex 6e32e725036b..50bc06b474a8 100644--- a/products/warehouse_sources/backend/temporal/data_imports/metrics.py+++ b/products/warehouse_sources/backend/temporal/data_imports/metrics.py@@ -9,6 +9,8 @@ from posthog.kafka_client.routing import get_producerfrom posthog.kafka_client.topics import KAFKA_APP_METRICS2from posthog.models.event.util import format_clickhouse_timestamp+from products.warehouse_sources.backend.models.external_data_destination import get_or_create_warehouse_destination+if TYPE_CHECKING:from products.warehouse_sources.backend.models.external_data_job import ExternalDataJob@@ -129,7 +131,18 @@ def emit_data_import_app_metrics(job: "ExternalDataJob") -> None:# Each destination is also keyed on its own, without a schema. A source-level surface wants one# series per destination across every table, and the API filters `instance_id` by equality, so# without this row it would have to ask once per schema per destination.- for destination_id in job.destination_ids or []:+ # `destination_ids_for_run` returns an empty list when a schema resolves to the PostHog+ # warehouse alone, so the run stays byte-for-byte on the path it took before destinations+ # existed. Without this fallback those runs report no destination at all, and a project+ # that never configured one sees an empty rows-by-destination chart.+ destination_ids = list(job.destination_ids or [])+ if not destination_ids:+ try:+ destination_ids = [str(get_or_create_warehouse_destination(job.team_id).id)]+ except Exception:+ logger.exception("Failed to resolve the warehouse destination for data import metrics")++ for destination_id in destination_ids:payloads.extend(rows_for(f"{schema_instance_id}/{destination_id}"))payloads.extend(rows_for(str(destination_id)))diff --git a/products/warehouse_sources/backend/temporal/data_imports/tests/test_metrics_app_metrics2.py b/products/warehouse_sources/backend/temporal/data_imports/tests/test_metrics_app_metrics2.pyindex c330db364d6f..d7a76918c134 100644--- a/products/warehouse_sources/backend/temporal/data_imports/tests/test_metrics_app_metrics2.py+++ b/products/warehouse_sources/backend/temporal/data_imports/tests/test_metrics_app_metrics2.py@@ -47,7 +47,18 @@ def _make_job(return job-class TestDestinationScopedAppMetrics(TestCase):+class WarehouseDestinationFallbackTestCase(TestCase):+ def setUp(self) -> None:+ super().setUp()+ patcher = mock.patch(+ "products.warehouse_sources.backend.temporal.data_imports.metrics.get_or_create_warehouse_destination"+ )+ self.mock_get_warehouse_destination = patcher.start()+ self.mock_get_warehouse_destination.return_value.id = "warehouse-destination"+ self.addCleanup(patcher.stop)+++class TestDestinationScopedAppMetrics(WarehouseDestinationFallbackTestCase):def _payloads(self, job) -> list[dict]:with mock.patch("products.warehouse_sources.backend.temporal.data_imports.metrics.get_producer"@@ -55,9 +66,21 @@ class TestDestinationScopedAppMetrics(TestCase):emit_data_import_app_metrics(job)return [call.kwargs["data"] for call in mock_producer_cls.return_value.produce.call_args_list]- def test_a_run_without_destinations_emits_what_it_always_did(self) -> None:- # Every existing chart filters instance_id by a bare schema id. Emitting anything else for- # these runs would change what they already show.+ def test_a_run_without_destinations_is_attributed_to_the_warehouse(self) -> None:+ job = _make_job(status=ExternalDataJob.Status.COMPLETED)++ payloads = self._payloads(job)++ assert len(payloads) == 6+ assert {p["instance_id"] for p in payloads} == {+ str(job.schema_id),+ f"{job.schema_id}/warehouse-destination",+ "warehouse-destination",+ }+ self.mock_get_warehouse_destination.assert_called_once_with(job.team_id)++ def test_a_warehouse_destination_lookup_error_keeps_schema_metrics(self) -> None:+ self.mock_get_warehouse_destination.side_effect = RuntimeError("database unavailable")job = _make_job(status=ExternalDataJob.Status.COMPLETED)payloads = self._payloads(job)@@ -92,11 +115,12 @@ class TestDestinationScopedAppMetrics(TestCase):def test_the_schema_scoped_rows_are_unchanged_by_destinations(self) -> None:# The number a chart already shows must not move because a destination was added.- without = self._payloads(_make_job(status=ExternalDataJob.Status.COMPLETED))- job = _make_job(status=ExternalDataJob.Status.COMPLETED, destination_ids=["dest-a"])- schema_scoped = [p for p in self._payloads(job) if p["instance_id"] == str(job.schema_id)]+ job_without = _make_job(status=ExternalDataJob.Status.COMPLETED)+ without = [p for p in self._payloads(job_without) if p["instance_id"] == str(job_without.schema_id)]+ job_with = _make_job(status=ExternalDataJob.Status.COMPLETED, destination_ids=["dest-a"])+ with_destination = [p for p in self._payloads(job_with) if p["instance_id"] == str(job_with.schema_id)]- assert [(p["metric_kind"], p["metric_name"], p["count"]) for p in schema_scoped] == [+ assert [(p["metric_kind"], p["metric_name"], p["count"]) for p in with_destination] == [(p["metric_kind"], p["metric_name"], p["count"]) for p in without]@@ -122,7 +146,7 @@ class TestDestinationScopedAppMetrics(TestCase):]-class TestEmitDataImportAppMetrics(TestCase):+class TestEmitDataImportAppMetrics(WarehouseDestinationFallbackTestCase):@parameterized.expand([(ExternalDataJob.Status.COMPLETED, "success", "succeeded"),@@ -141,7 +165,7 @@ class TestEmitDataImportAppMetrics(TestCase):emit_data_import_app_metrics(job)produce_calls = mock_producer.produce.call_args_list- assert len(produce_calls) == 2+ assert len(produce_calls) == 6status_payload = produce_calls[0].kwargs["data"]assert produce_calls[0].kwargs["topic"] == KAFKA_APP_METRICS2@@ -171,7 +195,7 @@ class TestEmitDataImportAppMetrics(TestCase):status_calls = [call for call in mock_producer.produce.call_args_list if call.kwargs["data"]["metric_kind"] == "failure"]- assert len(status_calls) == 2+ assert len(status_calls) == 6assert all(call.kwargs["data"]["metric_name"] == "billing_limited" for call in status_calls)@parameterized.expand(@@ -190,8 +214,8 @@ class TestEmitDataImportAppMetrics(TestCase):emit_data_import_app_metrics(job)produce_calls = mock_producer.produce.call_args_list- assert len(produce_calls) == 1- assert produce_calls[0].kwargs["data"]["metric_kind"] == "success"+ assert len(produce_calls) == 3+ assert all(call.kwargs["data"]["metric_kind"] == "success" for call in produce_calls)def test_non_terminal_status_emits_nothing(self):job = _make_job(status=ExternalDataJob.Status.RUNNING)diff --git a/products/warehouse_sources/frontend/scenes/PipelineOverviewScene/PipelineOverviewScene.tsx b/products/warehouse_sources/frontend/scenes/PipelineOverviewScene/PipelineOverviewScene.tsxindex bf1f4b897d58..113c1384818e 100644--- a/products/warehouse_sources/frontend/scenes/PipelineOverviewScene/PipelineOverviewScene.tsx+++ b/products/warehouse_sources/frontend/scenes/PipelineOverviewScene/PipelineOverviewScene.tsx@@ -94,10 +94,7 @@ export function PipelineOverviewScene(): JSX.Element {</>) : null}- <SceneSection- title="Rows synced by destination"- description="Rows written to each destination. A row that reaches two destinations counts against both, so the series stack."- >+ <SceneSection title="Rows synced by destination"><RowsByDestination /></SceneSection>diff --git a/products/warehouse_sources/frontend/scenes/PipelineOverviewScene/RecentRuns.tsx b/products/warehouse_sources/frontend/scenes/PipelineOverviewScene/RecentRuns.tsxindex f7d05bfe887f..eb8c55c1a48d 100644--- a/products/warehouse_sources/frontend/scenes/PipelineOverviewScene/RecentRuns.tsx+++ b/products/warehouse_sources/frontend/scenes/PipelineOverviewScene/RecentRuns.tsx@@ -45,8 +45,8 @@ export function RecentRuns(): JSX.Element {dataSource={recentRunRows}rowKey="id"size="small"- loading={recentRunsLoading && recentRuns !== null}nouns={['run', 'runs']}+ pagination={{ pageSize: 15 }}columns={[{title: 'Table',