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): surface a retry message instead of a 500 when the account picker is rate-limited (#121064)

Tom Owersd8c4f041f13d
diff --git a/posthog/models/integration/__init__.py b/posthog/models/integration/__init__.py
index 2fb1659d2e69..eb0922152a84 100644
--- a/posthog/models/integration/__init__.py
+++ b/posthog/models/integration/__init__.py
@@ -56,7 +56,12 @@ from .github import (
invalidate_github_repository_caches_for_installation,
)
from .gitlab import GitLabIntegration, GitLabIntegrationError
-from .google_ads import GoogleAdsIntegration, GoogleAdsScopeInsufficientError, google_ads_hierarchy_level
+from .google_ads import (
+ GoogleAdsIntegration,
+ GoogleAdsQuotaExhaustedError,
+ GoogleAdsScopeInsufficientError,
+ google_ads_hierarchy_level,
+)
from .google_cloud import (
GoogleCloudIntegration,
GoogleCloudServiceAccountIntegration,
@@ -207,6 +212,7 @@ __all__ = [
"validate_slack_request",
"google_ads_hierarchy_level",
"GoogleAdsIntegration",
+ "GoogleAdsQuotaExhaustedError",
"GoogleAdsScopeInsufficientError",
"is_unique_service_account_by_organization_id",
"GoogleCloudServiceAccountIntegration",
diff --git a/posthog/models/integration/google_ads.py b/posthog/models/integration/google_ads.py
index 81f07f3d3df3..44fbe16eb90c 100644
--- a/posthog/models/integration/google_ads.py
+++ b/posthog/models/integration/google_ads.py
@@ -29,6 +29,10 @@ class GoogleAdsScopeInsufficientError(ValidationError):
"""Google's consent screen lets a login sign in without ticking the Google Ads permission."""
+class GoogleAdsQuotaExhaustedError(Exception):
+ """Google rejected the request with a 429 RESOURCE_EXHAUSTED — a transient quota limit, not a bug."""
+
+
@retry(
stop=stop_after_attempt(3),
wait=wait_fixed(2),
@@ -142,6 +146,13 @@ class GoogleAdsIntegration:
"Please check the account permissions on the provider side."
)
+ if response.status_code == 429:
+ # A quota/rate-limit RESOURCE_EXHAUSTED on this interactive call isn't retried
+ # automatically (no Temporal activity wraps it, unlike the sync path), and it isn't a
+ # bug worth paging on, so skip capture_exception and let the caller tell the user to
+ # retry instead of surfacing a generic internal error.
+ raise GoogleAdsQuotaExhaustedError("Google Ads rate-limited this request (quota exhausted).")
+
if response.status_code != 200:
capture_exception(Exception(f"GoogleAdsIntegration: Failed to list accessible accounts: {response.text}"))
raise Exception("There was an internal error")
diff --git a/posthog/models/test/integration/test_google_ads.py b/posthog/models/test/integration/test_google_ads.py
index f056c89beb56..11770c35f759 100644
--- a/posthog/models/test/integration/test_google_ads.py
+++ b/posthog/models/test/integration/test_google_ads.py
@@ -10,7 +10,7 @@ from django.test import override_settings
import requests
-from posthog.models.integration import GoogleAdsIntegration, Integration
+from posthog.models.integration import GoogleAdsIntegration, GoogleAdsQuotaExhaustedError, Integration
class TestGoogleAdsIntegrationModel(BaseTest):
@@ -154,3 +154,19 @@ class TestGoogleAdsIntegrationModel(BaseTest):
GoogleAdsIntegration(self._integration()).list_google_ads_accessible_accounts()
assert mock_request.call_count == 3
+
+ @override_settings(GOOGLE_ADS_DEVELOPER_TOKEN="dev_token")
+ @patch("posthog.models.integration.google_ads.capture_exception")
+ @patch("posthog.models.integration.google_ads.requests.request")
+ def test_accessible_accounts_raises_quota_exhausted_on_429(self, mock_request, mock_capture_exception):
+ # A quota limit isn't a bug worth paging on, so it must raise a distinct, catchable error
+ # and skip capture_exception.
+ mock_request.return_value = MagicMock(
+ status_code=429,
+ text='{"error": {"status": "RESOURCE_EXHAUSTED", "message": "Resource has been exhausted (e.g. check quota)."}}',
+ )
+
+ with pytest.raises(GoogleAdsQuotaExhaustedError):
+ GoogleAdsIntegration(self._integration()).list_google_ads_accessible_accounts()
+
+ mock_capture_exception.assert_not_called()
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/source.py
index 02b22e48839f..5ef87519d606 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/source.py
@@ -10,6 +10,7 @@ from rest_framework.exceptions import ValidationError
from posthog.models.integration import (
ERROR_TOKEN_REFRESH_FAILED,
GoogleAdsIntegration,
+ GoogleAdsQuotaExhaustedError,
GoogleAdsScopeInsufficientError,
Integration,
OauthIntegration,
@@ -449,6 +450,12 @@ class GoogleAdsSource(
raise IntegrationAccountListingError(
"Google Ads did not respond in time while listing your accounts. Please try again."
) from e
+ except GoogleAdsQuotaExhaustedError as e:
+ # Unlike the sync path, no Temporal activity retries this interactive call, so a 429
+ # otherwise surfaces as an unhandled 500 instead of something the user can act on.
+ raise IntegrationAccountListingError(
+ "Google Ads rate-limited this request. Please wait a moment and try again."
+ ) from e
names_by_id = {account["id"]: account["name"] for account in accounts}
integration_accounts = [
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 fa86ee1210b2..e27849a30c45 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
@@ -1553,6 +1553,31 @@ class TestGetOAuthAccountsPermissionErrors:
GoogleAdsSource().get_oauth_accounts(1, 2)
+class TestGetOAuthAccountsQuotaExhausted:
+ def test_rate_limited_listing_is_actionable(self):
+ # A 429 RESOURCE_EXHAUSTED isn't retried automatically here, unlike the sync path's
+ # Temporal activity retries, so it must become the same actionable, catchable error the
+ # other listing failures raise rather than an unhandled 500.
+ cache.clear()
+ integration = mock.Mock(errors=None, kind="google-ads", sensitive_config={"access_token": "token"})
+ body = (
+ '{"error": {"status": "RESOURCE_EXHAUSTED", "message": "Resource has been exhausted (e.g. check quota)."}}'
+ )
+
+ with (
+ mock.patch.object(GoogleAdsSource, "get_oauth_integration", return_value=integration),
+ mock.patch(f"{_SOURCE_MODULE}.OauthIntegration") as mock_oauth,
+ mock.patch(
+ "posthog.models.integration.google_ads.requests.request",
+ return_value=mock.Mock(status_code=429, text=body),
+ ),
+ ):
+ mock_oauth.return_value.access_token_expired.return_value = False
+
+ with pytest.raises(IntegrationAccountListingError, match="rate-limited"):
+ GoogleAdsSource().get_oauth_accounts(1, 2)
+
+
class _DrainClock:
"""A monotonic clock the test drives itself, one second per window drained.