: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(mixpanel): treat retryable rate-limit errors as recoverable noise (#72153)
Tom Owersf3fe64138115
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/README.md b/products/warehouse_sources/backend/temporal/data_imports/sources/README.mdindex 570c4fc4053b..b54ccd7c71d4 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/README.md+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/README.md@@ -228,3 +228,16 @@ Common errors to handle:- **401 Unauthorized**: Invalid or expired API keys- **403 Forbidden**: Missing required scopes or permissions- **Invalid/Expired tokens**: OAuth tokens that need re-authentication++## Retryable Errors++Sources that retry transient failures internally (rate limits, transient 5xx) can re-raise once their own retries are exhausted.+Temporal then retries the whole activity, so the failure is self-recovering — but by default it is logged as an exception and shows up as error-tracking noise.++Override `get_retryable_errors()` to return a set of partial error messages the source already retries.+Matching errors are logged at `warning` instead of `exception` before being re-raised for Temporal's retry:++```python+def get_retryable_errors(self) -> set[str]:+ return {"Example API error (retryable)"}+```diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/base.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/base.pyindex c581c021c020..ed710af5e2e2 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/base.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/base.py@@ -196,6 +196,19 @@ class _BaseSource(ABC, Generic[ConfigType]):return {}+ def get_retryable_errors(self) -> set[str]:+ """Returns partial error messages the source already retries internally.++ A source that exhausts its own retries (rate limits, transient 5xx) and re-raises still+ lets Temporal retry the whole activity, so the failure is transient and self-recovering.+ Matching errors are logged at `warning` rather than `exception`, keeping benign,+ recoverable failures out of error tracking as noise.++ Each entry is a partial error message matched against `str(error)`.+ """++ return set()+def get_canonical_descriptions(self) -> CanonicalDescriptions:"""Curated, documentation-sourced descriptions for this source's well-known tables/endpoints.diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mixpanel/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mixpanel/source.pyindex 5cbad4241e3f..cbea3d72c059 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mixpanel/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mixpanel/source.py@@ -164,6 +164,12 @@ Authenticate with a [Mixpanel Service Account](https://developer.mixpanel.com/re),}+ def get_retryable_errors(self) -> set[str]:+ # A 429 (rate limit) or 5xx is retried internally honoring Retry-After; if those retries+ # still exhaust, the failure is transient and self-recovering, so let Temporal retry the+ # activity without surfacing it as tracked exception noise.+ return {"Mixpanel API error (retryable)"}+def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[MixpanelResumeConfig]:return ResumableSourceManager[MixpanelResumeConfig](inputs, MixpanelResumeConfig)diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mixpanel/test_mixpanel.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mixpanel/test_mixpanel.pyindex c6e276368165..3f6244a2b5be 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mixpanel/test_mixpanel.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mixpanel/test_mixpanel.py@@ -27,6 +27,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.mixpanel.mvalidate_credentials,)from products.warehouse_sources.backend.temporal.data_imports.sources.mixpanel.settings import MIXPANEL_ENDPOINTS+from products.warehouse_sources.backend.temporal.data_imports.sources.mixpanel.source import MixpanelSourceLOGGER = structlog.get_logger()@@ -173,6 +174,18 @@ class TestCheckResponse:assert exc_info.value.retry_after is None+class TestRetryableErrors:+ def test_retryable_marker_matches_raised_message(self) -> None:+ # The source keeps a self-recovering 429/5xx out of error tracking by matching+ # get_retryable_errors() against what _check_response raises; this guards the two in sync,+ # so a change to either the raised message or the marker can't silently revive the noise.+ with pytest.raises(MixpanelRetryableError) as exc_info:+ _check_response(FakeResponse(status_code=429), "https://data.mixpanel.com/api/2.0/export", LOGGER) # type: ignore[arg-type]+ markers = MixpanelSource().get_retryable_errors()+ assert markers+ assert any(marker in str(exc_info.value) for marker in markers)++class TestParseRetryAfter:@parameterized.expand([diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/import_data_sync.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/import_data_sync.pyindex 3ac14ecc8e04..9a41b4e0efe7 100644--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/import_data_sync.py+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/import_data_sync.py@@ -314,22 +314,32 @@ async def _handle_import_error(Errors the source classifies as non-retryable (bad credentials, a deleted ormisconfigured remote — e.g. a MongoDB ``mongodb+srv://`` hostname whose DNS record nolonger resolves) are handed to ``handle_non_retryable_error``, which stops the job after- a few attempts instead of retrying up to the activity's maximum. Everything else is- re-raised so Temporal retries it as usual.+ a few attempts instead of retrying up to the activity's maximum.++ Errors the source classifies as retryable (rate limits, transient 5xx) reach us only after+ the source's own retries are exhausted. Temporal retries the whole activity and the error is+ transient and self-recovering, so we log at ``warning`` rather than ``exception`` to keep+ this benign, recoverable failure out of error tracking.++ Everything else is logged as an exception and re-raised so Temporal retries it as usual."""source_cls = SourceRegistry.get_source(job_inputs.job_type)- non_retryable_errors = source_cls.get_non_retryable_errors()error_msg = str(error)- is_non_retryable_error = any(- non_retryable_error in error_msg for non_retryable_error in non_retryable_errors.keys()- )- if is_non_retryable_error:++ non_retryable_errors = source_cls.get_non_retryable_errors()+ if any(match in error_msg for match in non_retryable_errors):await handle_non_retryable_error(job_inputs, error_msg, logger, error)- else:- await logger.aexception(error_msg)- await logger.adebug("Error encountered during import_data_activity - re-raising")++ retryable_errors = source_cls.get_retryable_errors()+ if any(match in error_msg for match in retryable_errors):+ await logger.awarning(error_msg)+ await logger.adebug("Source-classified retryable error - re-raising for Temporal retry")raise error+ await logger.aexception(error_msg)+ await logger.adebug("Error encountered during import_data_activity - re-raising")+ raise error+async def _run(job_inputs: PipelineInputs,diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_import_data_sync.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_import_data_sync.pyindex db81a3229639..7d408b895f09 100644--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_import_data_sync.py+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_import_data_sync.py@@ -130,6 +130,29 @@ async def test_unparseable_config_routes_through_handler():source.source_for_pipeline.assert_not_called()+async def test_source_classified_retryable_error_logged_as_warning_not_exception():+ # A rate-limit / transient error the source retries internally reaches _handle_import_error only+ # once those retries exhaust. Temporal retries the whole activity, so it must be logged at+ # warning (not aexception, which mints error-tracking noise) while still being re-raised.+ error = Exception("Mixpanel API error (retryable): status=429, url=https://data.mixpanel.com/api/2.0/export")+ source = mock.MagicMock(spec=SimpleSource)+ source.get_non_retryable_errors.return_value = {}+ source.get_retryable_errors.return_value = {"Mixpanel API error (retryable)"}++ logger = mock.MagicMock()+ logger.awarning = mock.AsyncMock()+ logger.aexception = mock.AsyncMock()+ logger.adebug = mock.AsyncMock()++ with mock.patch.object(module.SourceRegistry, "get_source", return_value=source):+ with pytest.raises(Exception, match="retryable"):+ await module._handle_import_error(mock.MagicMock(), logger, error)++ logger.awarning.assert_awaited_once()+ logger.aexception.assert_not_awaited()++def _incremental_schema(*, is_incremental: bool, lookback_seconds: int | None) -> mock.MagicMock:schema = mock.MagicMock()schema.should_use_incremental_field = True