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): stop reporting Meta Ads rate limits as error-tracking noise (#79867)

Tom Owers02c3a73ed33d
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/meta_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/meta_ads.py
index 8df4c664c77f..20c62071761d 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/meta_ads.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/meta_ads.py
@@ -469,17 +469,21 @@ def _raise_meta_api_error(response: Response) -> typing.NoReturn:
Permanent auth/permission failures raise a clean, user-actionable message
that ``MetaAdsSource.get_non_retryable_errors`` matches on, so the job fails
- fast instead of burning retries. A momentary backend blip (see
- ``_is_transient_error``) that has already exhausted its in-process retries is
- tagged so ``MetaAdsSource.get_retryable_errors`` can keep the self-recovering
- failure out of error tracking once Temporal retries the activity, excluding
- anything the shrink ladders can still act on, which ends at
- ``_raise_shrink_exhausted_error`` instead. The raw response is appended for
- debugging.
+ fast instead of burning retries. Throttling (see ``_is_rate_limit_error``) and
+ a momentary backend blip (see ``_is_transient_error``) that has already
+ exhausted its in-process retries are both tagged so
+ ``MetaAdsSource.get_retryable_errors`` can keep the self-recovering failure out
+ of error tracking once Temporal retries the activity, excluding anything the
+ shrink ladders can still act on, which ends at ``_raise_shrink_exhausted_error``
+ instead. The raw response is appended for debugging.
Everything else raises the raw response and stays retryable.
"""
if _is_permanent_auth_error(response):
raise Exception(f"{META_AUTH_ERROR_MESSAGE} (Meta API response: {response.status_code} - {response.text})")
+ if _is_rate_limit_error(response):
+ raise Exception(
+ f"{META_RATE_LIMIT_ERROR_MESSAGE} (Meta API response: {response.status_code} - {response.text})"
+ )
if _is_transient_error(response) and not _should_shrink_request(response):
raise Exception(f"Meta API request failed (retryable): {response.status_code} - {response.text}")
raise Exception(f"Meta API request failed: {response.status_code} - {response.text}")
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/source.py
index 5c05cde24a59..3637de559d66 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/source.py
@@ -39,6 +39,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.generated_
)
from products.warehouse_sources.backend.temporal.data_imports.sources.meta_ads.meta_ads import (
META_AUTH_ERROR_MESSAGE,
+ META_RATE_LIMIT_ERROR_MESSAGE,
SHRINK_EXHAUSTED_ERROR_MESSAGE,
MetaAdsAuthError,
MetaAdsRateLimitError,
@@ -142,11 +143,17 @@ class MetaAdsSource(ResumableSource[MetaAdsSourceConfig, MetaAdsResumeConfig], O
}
def get_retryable_errors(self) -> set[str]:
- # Meta error codes 1 ("API Unknown") and 2 ("API Service") are momentary backend blips
- # Meta's own docs recommend simply retrying. `meta_ads._raise_meta_api_error` tags them
- # with this marker once `_get_with_transient_retry`'s in-process retries are exhausted, so
- # the eventual Temporal-level retry doesn't page us as a bug.
- return {"Meta API request failed (retryable)"}
+ return {
+ # Meta error codes 1 ("API Unknown") and 2 ("API Service") are momentary backend blips
+ # Meta's own docs recommend simply retrying. `meta_ads._raise_meta_api_error` tags them
+ # with this marker once `_get_with_transient_retry`'s in-process retries are exhausted, so
+ # the eventual Temporal-level retry doesn't page us as a bug.
+ "Meta API request failed (retryable)",
+ # Meta throttling (rate limiting) — the request itself is fine, Meta just rejected it
+ # for volume. Only waiting helps, and the sync already retries via Temporal, so this
+ # shouldn't page us as a bug either.
+ META_RATE_LIMIT_ERROR_MESSAGE,
+ }
def get_schemas(
self,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/test_meta_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/test_meta_ads.py
index 5d08e089018c..1ea30bded488 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/test_meta_ads.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/test_meta_ads.py
@@ -1343,6 +1343,24 @@ class TestRetryableErrors:
_raise_meta_api_error(_mock_response(500, body))
assert any(pattern in str(exc_info.value) for pattern in patterns)
+ @pytest.mark.parametrize("code", sorted(meta_ads_module.META_RATE_LIMIT_ERROR_CODES))
+ def test_rate_limit_error_matches_retryable_pattern(self, code: int) -> None:
+ # Real-world Meta throttling response: code 17 "User request limit reached" with
+ # is_transient: false. It must be tagged retryable rather than falling through to the
+ # generic, unclassified message and getting reported to error tracking on every attempt.
+ body = {
+ "error": {
+ "message": "User request limit reached",
+ "type": "OAuthException",
+ "code": code,
+ "is_transient": False,
+ }
+ }
+ patterns = MetaAdsSource().get_retryable_errors()
+ with pytest.raises(Exception) as exc_info:
+ _raise_meta_api_error(_mock_response(400, body))
+ assert any(pattern in str(exc_info.value) for pattern in patterns)
+
def test_empty_body_500_matches_retryable_pattern(self) -> None:
# Meta (or a fronting proxy) occasionally returns a bare 500 with an empty body — no
# JSON, no error code to classify by. It must still be tagged retryable rather than