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(data-imports): attach source/schema identity to repartition exceptions (#78081)

Tom Owers14bfcc29a5d3
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py
index 215c166680ac..4b39805305bd 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/repartition_table.py
@@ -56,6 +56,7 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline
DELTA_REPARTITION_DURATION_SECONDS,
DELTA_REPARTITION_TOTAL,
)
+from products.warehouse_sources.backend.temporal.data_imports.sources.common.job_context import bind_job_context
LOGGER = get_logger(__name__)
@@ -298,6 +299,20 @@ def _maybe_repartition_table(inputs: RepartitionActivityInputs, logger: Filterin
)
return
+ # Attach the same source/schema identity the import activity does, so an exception captured
+ # anywhere below (budget exhaustion, an unexpected rewrite failure) can be attributed to a
+ # connector and table instead of landing in error tracking with no sync context.
+ bind_job_context(
+ team_id=inputs.team_id,
+ source_type=schema.source.source_type,
+ external_data_source_id=inputs.source_id,
+ external_data_schema_id=inputs.schema_id,
+ external_data_job_id=inputs.job_id,
+ schema_name=schema.name,
+ sync_type=schema.sync_type,
+ pipeline_version=job.pipeline_version,
+ )
+
# `resolved_s3_folder_name` is authoritative for the Delta folder, not the row's own name: a row
# renamed during the multi-schema migration keeps its folder pinned to the original path (name
# `public.users`, folder `users`), and the pipeline writes there too. Deriving the folder from
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_repartition_table.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_repartition_table.py
index 1b4ed5f2b547..b207b1d14a6b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_repartition_table.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_repartition_table.py
@@ -1,11 +1,14 @@
import uuid
import datetime as dt
+import contextvars
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
from parameterized import parameterized
+from posthog.exceptions_capture import ambient_exception_properties
+
from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.delta.errors import (
TransientObjectStoreError,
)
@@ -105,6 +108,57 @@ class TestRepartitionActivityDeltaFolder:
assert mock_job_model.objects.get.call_args.kwargs == {"id": JOB_ID}
+class TestJobContextBinding:
+ """The import activity tags every captured exception with the sync's source/schema identity
+ (`warehouse_sources_*` properties) via `bind_job_context`; this activity previously never called
+ it, so an exception captured here (e.g. `RepartitionBudgetExceededError`) landed in error
+ tracking with no way to attribute it to a connector, table, or sync mode."""
+
+ @patch(f"{MODULE}.capture_repartition_event")
+ @patch(f"{MODULE}.HeartbeaterSync")
+ @patch(f"{MODULE}.repartition_table_in_place", new_callable=AsyncMock)
+ @patch(f"{MODULE}.DeltaTableRef")
+ @patch(f"{MODULE}.is_auto_repartition_enabled", return_value=True)
+ @patch(f"{MODULE}.ExternalDataJob")
+ @patch(f"{MODULE}.ExternalDataSchema")
+ def test_activity_binds_source_and_schema_identity_for_captured_exceptions(
+ self,
+ mock_schema_model: MagicMock,
+ mock_job_model: MagicMock,
+ _mock_enabled: MagicMock,
+ _mock_helper_cls: MagicMock,
+ mock_repartition: AsyncMock,
+ _mock_heartbeater: MagicMock,
+ _mock_capture_event: MagicMock,
+ ) -> None:
+ schema = _schema(name="public.charges", s3_folder_name="charges")
+ schema.source = MagicMock(source_type="Stripe")
+ schema.sync_type = "incremental"
+ mock_schema_model.objects.select_related.return_value.get.return_value = schema
+ mock_job_model.objects.get.return_value.pipeline_version = "v3"
+ mock_repartition.return_value = {"outcome": "completed"}
+
+ # Runs in a copied context so the ambient exception properties `bind_job_context` sets
+ # don't leak into other tests sharing this interpreter thread.
+ ctx = contextvars.copy_context()
+ captured_props: dict = {}
+
+ def _run() -> None:
+ _maybe_repartition_table(
+ RepartitionActivityInputs(team_id=TEAM_ID, schema_id=SCHEMA_ID, job_id=JOB_ID, source_id=SOURCE_ID),
+ MagicMock(),
+ )
+ captured_props.update(ambient_exception_properties())
+
+ ctx.run(_run)
+
+ assert captured_props["warehouse_sources_source_type"] == "Stripe"
+ assert captured_props["warehouse_sources_schema_name"] == "public.charges"
+ assert captured_props["warehouse_sources_sync_type"] == "incremental"
+ assert captured_props["warehouse_sources_pipeline_version"] == "v3"
+ assert captured_props["warehouse_sources_job_id"] == JOB_ID
+
+
NO_ACTIVITY_CONTEXT = object()