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.

fix(warehouse-sources): surface unclassified CDC extraction failures to triage (#83455)

Tom Owers8d4dc279a08a
diff --git a/products/warehouse_sources/backend/temporal/data_imports/cdc/activities.py b/products/warehouse_sources/backend/temporal/data_imports/cdc/activities.py
index 2aa251b321aa..0e83aff67121 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/cdc/activities.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/cdc/activities.py
@@ -1597,6 +1597,12 @@ class CDCExtractActivity:
# here, mirroring what update_external_job_status does for non-CDC syncs.
if terminal and not marked_broken:
self._schedule_failure_digest()
+ # An unclassified failure stays retryable and never pauses the schedule, so a deterministic
+ # one re-fails every scheduled run indefinitely. Only _capture_non_retryable emits analytics,
+ # so these never reach error triage — capture the terminal case so the taxonomy can be taught
+ # to recognise it (and, where fatal, stop retrying).
+ if terminal and info.category == CDCErrorCategory.UNKNOWN:
+ self._capture_unclassified(exc)
self._emit_run_duration("failed")
return info
@@ -1625,6 +1631,30 @@ class CDCExtractActivity:
except Exception:
self.log.warning("cdc_non_retryable_capture_failed", exc_info=True)
+ def _capture_unclassified(self, exc: BaseException) -> None:
+ # Send the exception types in the cause chain — never str(exc), which can embed the customer's
+ # host, database, schema, or table names — so the taxonomy can be extended to catch this.
+ seen: set[int] = set()
+ type_names: list[str] = []
+ current: BaseException | None = exc
+ while current is not None and id(current) not in seen:
+ seen.add(id(current))
+ type_names.append(type(current).__name__)
+ current = current.__cause__ or current.__context__
+ # Best-effort: analytics must never mask the failure the caller is about to re-raise.
+ try:
+ posthoganalytics.capture(
+ distinct_id=get_machine_id(),
+ event="cdc extraction unclassified error",
+ properties={
+ "team_id": self.inputs.team_id,
+ "source_id": str(self.inputs.source_id),
+ "exception_types": type_names,
+ },
+ )
+ except Exception:
+ self.log.warning("cdc_unclassified_capture_failed", exc_info=True)
+
def _create_failure_visibility_jobs(self, friendly_error: str) -> None:
"""Create one terminal FAILED ExternalDataJob per job-less CDC schema for this run.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_extract_activity.py b/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_extract_activity.py
index 135a8c325a42..cd7bcb713336 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_extract_activity.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/cdc/tests/test_extract_activity.py
@@ -20,6 +20,7 @@ from products.warehouse_sources.backend.models.external_data_source import Exter
from products.warehouse_sources.backend.temporal.data_imports.cdc.activities import (
CDC_BACKPRESSURE_STUCK_AGE,
CDC_MAX_CHANGES_PER_READ,
+ CDC_MAX_EXTRACTION_ATTEMPTS,
CDC_ORPHAN_JOB_MIN_AGE,
CDC_ORPHANED_JOB_MESSAGE,
SLOT_INVALIDATION_RECOVERY_MESSAGE,
@@ -2427,6 +2428,68 @@ class TestErrorClassification:
assert schema.latest_error == cdc_error_info(CDCErrorCategory.CONNECTION_FAILED).friendly_message
mock_posthoganalytics.capture.assert_not_called()
+ @patch(
+ "products.warehouse_sources.backend.temporal.data_imports.cdc.activities.get_machine_id",
+ return_value="machine-1",
+ )
+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.posthoganalytics")
+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.activity")
+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.get_cdc_adapter")
+ @patch.object(CDCExtractActivity, "_get_cdc_schemas")
+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.ExternalDataSource")
+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.ExternalDataJob")
+ @patch("products.warehouse_sources.backend.temporal.data_imports.cdc.activities.close_old_connections")
+ def test_terminal_unclassified_error_is_captured_for_triage(
+ self,
+ mock_close_conns,
+ MockJob,
+ MockSourceModel,
+ mock_get_schemas,
+ mock_get_adapter,
+ mock_activity,
+ mock_posthoganalytics,
+ mock_get_machine_id,
+ ):
+ # An unclassified failure stays retryable and never pauses the schedule, so a deterministic one
+ # re-fails every scheduled run forever. Only the non-retryable path emits analytics, so without
+ # this capture these highest-volume retry loops stay invisible to error triage.
+ source = _make_source()
+ MockSourceModel.objects.get.return_value = source
+ schema = _make_schema("users", cdc_mode="streaming", source=source)
+ mock_get_schemas.return_value = [schema]
+
+ # A non-psycopg error the adapter can't classify falls back to retryable UNKNOWN.
+ mock_reader = MagicMock()
+ mock_reader.read_changes.side_effect = RuntimeError("arrow merge blew up")
+ mock_reader.truncated_tables = []
+ mock_adapter = MagicMock()
+ mock_adapter.create_reader.return_value = mock_reader
+ mock_adapter.is_slot_invalidation_error.return_value = False
+ mock_adapter.classify_error = PostgresCDCAdapter().classify_error
+ mock_get_adapter.return_value = mock_adapter
+
+ mock_activity.heartbeat = MagicMock()
+ # Retries exhausted: the failure is terminal, so the capture fires (unlike a mid-retry attempt).
+ mock_activity.info.return_value = MagicMock(
+ workflow_id="wf-1", workflow_run_id="run-1", attempt=CDC_MAX_EXTRACTION_ATTEMPTS
+ )
+
+ inputs = CDCExtractInput(team_id=1, source_id=source.id)
+ with (
+ patch("products.data_warehouse.backend.facade.tasks.schedule_external_data_failure_digest"),
+ pytest.raises(RuntimeError, match="arrow merge blew up"),
+ ):
+ cdc_extract_activity(inputs)
+
+ assert schema.latest_error == cdc_error_info(CDCErrorCategory.UNKNOWN).friendly_message
+ mock_posthoganalytics.capture.assert_called_once()
+ captured = mock_posthoganalytics.capture.call_args.kwargs
+ assert captured["event"] == "cdc extraction unclassified error"
+ assert captured["properties"]["source_id"] == str(source.id)
+ # The exception type — not str(exc), which could embed customer host/table names — is what a
+ # human needs to teach the taxonomy to recognise this failure.
+ assert "RuntimeError" in captured["properties"]["exception_types"]
+
class TestSlotInvalidationRecovery:
"""When the replication slot is invalidated/dropped on the source DB, the activity