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(google_ads): retry quota RESOURCE_EXHAUSTED with in-process backoff (#66774)

Tom Owers7a797a210289
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/google_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/google_ads.py
index d5a8c1637de8..0f6a4caabaf3 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/google_ads.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/google_ads.py
@@ -533,32 +533,52 @@ def google_ads_source(
)
-# Google flags both ``UNAVAILABLE`` (e.g. its frontend returning ``502:Bad Gateway`` — the request
-# never reached a healthy backend) and ``INTERNAL`` ("Internal error encountered." from the
-# backend) as transient, retry-with-backoff statuses: a fresh attempt after a short backoff usually
+# Google flags ``UNAVAILABLE`` (e.g. its frontend returning ``502:Bad Gateway`` — the request never
+# reached a healthy backend), ``INTERNAL`` ("Internal error encountered." from the backend), and
+# ``RESOURCE_EXHAUSTED`` ("Resource has been exhausted (e.g. check quota)." — a quota/rate-limit
+# rejection) as transient, retry-with-backoff statuses: a fresh attempt after a short backoff usually
# succeeds. Riding the blip out in-process keeps the whole import activity from failing — which
# would otherwise re-fetch schemas, rebuild the gRPC client, and restart pagination from the last
# checkpoint — and avoids the captured error-tracking noise.
_MAX_TRANSIENT_SEARCH_ATTEMPTS = 4
-_TRANSIENT_GRPC_STATUS_CODES = frozenset({grpc.StatusCode.UNAVAILABLE, grpc.StatusCode.INTERNAL})
+_TRANSIENT_GRPC_STATUS_CODES = frozenset(
+ {grpc.StatusCode.UNAVAILABLE, grpc.StatusCode.INTERNAL, grpc.StatusCode.RESOURCE_EXHAUSTED}
+)
+
+# A client-side "Received message larger than max" abort also carries ``RESOURCE_EXHAUSTED`` (see the
+# receive-limit note at the top of this module), but it is deterministic — a retry re-requests the
+# same oversized page and fails identically — so it is excluded from the transient set. Raising the
+# receive limit, not retrying, is what addresses it.
+_RECEIVE_LIMIT_EXHAUSTED_SIGNATURE = "Received message larger than max"
def _is_transient_grpc_error(exc: BaseException) -> bool:
"""Return True for a transient gRPC failure Google's guidance says to retry.
The gapic transport usually surfaces these as ``google.api_core.exceptions.ServiceUnavailable``
- / ``InternalServerError``, but the raw ``grpc`` ``_InactiveRpcError`` (whose ``code()`` returns
- the ``StatusCode``) can also propagate. The Google Ads SDK additionally re-wraps the transport
- error in a ``GoogleAdsException`` when it can pull an ads ``failure`` from the trailing metadata
- (e.g. a backend ``DEADLINE_EXCEEDED`` returned alongside the status); the gRPC status then lives
- on the wrapped ``error``, so we unwrap and inspect it too.
+ / ``InternalServerError`` / ``ResourceExhausted``, but the raw ``grpc`` ``_InactiveRpcError``
+ (whose ``code()`` returns the ``StatusCode``) can also propagate. The Google Ads SDK additionally
+ re-wraps the transport error in a ``GoogleAdsException`` when it can pull an ads ``failure`` from
+ the trailing metadata (e.g. a backend ``DEADLINE_EXCEEDED`` returned alongside the status); the
+ gRPC status then lives on the wrapped ``error``, so we unwrap and inspect it too.
"""
if isinstance(exc, google_api_exceptions.ServiceUnavailable | google_api_exceptions.InternalServerError):
return True
candidate: typing.Any = exc.error if isinstance(exc, GoogleAdsException) else exc
+ # ``ResourceExhausted`` exposes ``code`` as an HTTP int, not a callable ``StatusCode``, so the
+ # gapic-wrapped form is matched by type rather than via the ``code()`` check below.
+ if isinstance(candidate, google_api_exceptions.ResourceExhausted):
+ return _RECEIVE_LIMIT_EXHAUSTED_SIGNATURE not in str(candidate)
code = getattr(candidate, "code", None)
- return callable(code) and code() in _TRANSIENT_GRPC_STATUS_CODES
+ if not callable(code):
+ return False
+ status = code()
+ if status not in _TRANSIENT_GRPC_STATUS_CODES:
+ return False
+ if status == grpc.StatusCode.RESOURCE_EXHAUSTED:
+ return _RECEIVE_LIMIT_EXHAUSTED_SIGNATURE not in str(candidate)
+ return True
_T = typing.TypeVar("_T")
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py
index 94e63c8bf7c0..ddb3658060ff 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py
@@ -661,17 +661,25 @@ class TestTransientClientInitErrorDetection:
class _StatusCodeRpcError(grpc.RpcError):
"""A raw gRPC error whose ``code()`` reports a given ``StatusCode`` (``_InactiveRpcError`` shape)."""
- def __init__(self, status_code: grpc.StatusCode):
+ def __init__(self, status_code: grpc.StatusCode, message: str = ""):
self._status_code = status_code
+ self._message = message
def code(self) -> grpc.StatusCode:
return self._status_code
+ def __str__(self) -> str:
+ return self._message
+
def _grpc_unavailable_error() -> grpc.RpcError:
return _StatusCodeRpcError(grpc.StatusCode.UNAVAILABLE)
+def _grpc_resource_exhausted_error(message: str = "") -> grpc.RpcError:
+ return _StatusCodeRpcError(grpc.StatusCode.RESOURCE_EXHAUSTED, message)
+
+
def _google_ads_exception_wrapping(grpc_error: grpc.RpcError) -> GoogleAdsException:
"""A ``GoogleAdsException`` carrying a transport-level error on ``error``, the shape the SDK
raises when it can pull an ads ``failure`` from the trailing metadata alongside the gRPC status.
@@ -696,6 +704,32 @@ class TestTransientGrpcErrorDetection:
# gapic wrapper and the raw _InactiveRpcError must both be ridden out in-process.
(google_api_exceptions.InternalServerError("500 Internal error encountered."), True),
(_grpc_internal_error(), True),
+ # A quota/rate-limit RESOURCE_EXHAUSTED ("Resource has been exhausted (e.g. check
+ # quota).") is Google-flagged retryable — both the gapic wrapper (whose ``code`` is an
+ # HTTP int, not a callable ``StatusCode``) and the raw _InactiveRpcError must be ridden out.
+ (google_api_exceptions.ResourceExhausted("Resource has been exhausted (e.g. check quota)."), True),
+ (_grpc_resource_exhausted_error("Resource has been exhausted (e.g. check quota)."), True),
+ # The SDK can also wrap a RESOURCE_EXHAUSTED transport status in a GoogleAdsException — the
+ # unwrapped raw _InactiveRpcError then takes the ``code()`` path with the signature guard.
+ (
+ _google_ads_exception_wrapping(
+ _grpc_resource_exhausted_error("Resource has been exhausted (e.g. check quota).")
+ ),
+ True,
+ ),
+ # A client-side "Received message larger than max" abort is RESOURCE_EXHAUSTED too, but is
+ # deterministic — it must not be retried in-process regardless of which shape it arrives as.
+ (
+ google_api_exceptions.ResourceExhausted("Received message larger than max (90000000 vs. 67108864)"),
+ False,
+ ),
+ (_grpc_resource_exhausted_error("Received message larger than max (90000000 vs. 67108864)"), False),
+ (
+ _google_ads_exception_wrapping(
+ _grpc_resource_exhausted_error("Received message larger than max (90000000 vs. 67108864)")
+ ),
+ False,
+ ),
# A different gapic error must not be treated as transient.
(google_api_exceptions.PermissionDenied("PERMISSION_DENIED"), False),
# Google Ads API errors carry no transient gRPC status — they route through the existing
@@ -733,6 +767,11 @@ class TestSearchTransientRetry:
_google_ads_exception_wrapping(_grpc_unavailable_error()),
google_api_exceptions.InternalServerError("500 Internal error encountered."),
_grpc_internal_error(),
+ google_api_exceptions.ResourceExhausted("Resource has been exhausted (e.g. check quota)."),
+ _grpc_resource_exhausted_error("Resource has been exhausted (e.g. check quota)."),
+ _google_ads_exception_wrapping(
+ _grpc_resource_exhausted_error("Resource has been exhausted (e.g. check quota).")
+ ),
],
)
def test_rides_out_transient_error(self, error):