: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(google_ads): raise gRPC receive limit for large search pages (#63764)
Tom Owers104b6c2da7ae
diff --git a/posthog/temporal/data_imports/sources/google_ads/google_ads.py b/posthog/temporal/data_imports/sources/google_ads/google_ads.pyindex 27a9be615ae0..643daf04e119 100644--- a/posthog/temporal/data_imports/sources/google_ads/google_ads.py+++ b/posthog/temporal/data_imports/sources/google_ads/google_ads.py@@ -6,6 +6,7 @@ from django.conf import settingsfrom django.db import close_old_connectionsimport pyarrow as pa+from google.ads.googleads import client as google_ads_client_modulefrom google.ads.googleads.client import GoogleAdsClientfrom google.ads.googleads.v23.common import types as ga_commonfrom google.ads.googleads.v23.enums import types as ga_enums@@ -36,9 +37,36 @@ from products.data_warehouse.backend.types import IncrementalFieldType# `GoogleAdsServiceClient.DEFAULT_ENDPOINT`.GOOGLE_ADS_HOST = "googleads.googleapis.com"+# The Google Ads SDK hardcodes `grpc.max_receive_message_length` to 64 MiB. A single+# `GoogleAdsService.Search` page can carry up to 10,000 rows, and wide resources routinely+# serialize past 64 MiB — when that happens the gRPC client aborts the call with a+# RESOURCE_EXHAUSTED "Received message larger than max" before we process any row, failing+# the whole sync. We can't ask the API for smaller pages (`page_size` is rejected with+# PAGE_SIZE_NOT_SUPPORTED as of v17), so the only lever is raising the client's receive+# limit. 512 MiB leaves comfortable headroom over the largest payloads we've observed.+GRPC_MAX_RECEIVE_MESSAGE_LENGTH = 512 * 1024 * 1024+_GRPC_MAX_RECEIVE_MESSAGE_LENGTH_KEY = "grpc.max_receive_message_length"+++def _ensure_grpc_receive_limit() -> None:+ """Raise the Google Ads gRPC client's inbound message cap in place.++ ``get_service`` reads the SDK's module-level ``_GRPC_CHANNEL_OPTIONS`` each time it builds+ a channel, so rewriting the entry here makes every channel we subsequently create pick up+ the higher limit. The update is idempotent and safe to call repeatedly.+ """+ options = google_ads_client_module._GRPC_CHANNEL_OPTIONS+ for index, (key, _value) in enumerate(options):+ if key == _GRPC_MAX_RECEIVE_MESSAGE_LENGTH_KEY:+ options[index] = (key, GRPC_MAX_RECEIVE_MESSAGE_LENGTH)+ return+ options.append((_GRPC_MAX_RECEIVE_MESSAGE_LENGTH_KEY, GRPC_MAX_RECEIVE_MESSAGE_LENGTH))+def google_ads_client(config: GoogleAdsSourceConfigUnion, team_id: int) -> GoogleAdsClient:"""Initialize a `GoogleAdsClient` with provided config."""+ _ensure_grpc_receive_limit()+if isinstance(config, GoogleAdsSourceConfig):# Temporal activities run in a thread pool where Django DB connections can go# stale between uses (Postgres closes the connection server-side). Thisdiff --git a/posthog/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py b/posthog/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.pyindex bb4a49b44841..2430fcc6d385 100644--- a/posthog/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py+++ b/posthog/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py@@ -209,6 +209,39 @@ class TestGoogleAdsNonRetryableErrors:assert "admin" in friendly.lower()+class TestGrpcReceiveLimit:+ # The largest search page observed aborting syncs in production was ~103 MB; the gRPC+ # client must accept at least that much for the resource to sync at all.+ _SDK_DEFAULT = 64 * 1024 * 1024+ _LARGEST_OBSERVED_PAYLOAD = 103_046_535++ def test_raises_sdk_default_receive_limit(self):+ from google.ads.googleads import client as google_ads_client_module++ from posthog.temporal.data_imports.sources.google_ads.google_ads import (+ GRPC_MAX_RECEIVE_MESSAGE_LENGTH,+ _ensure_grpc_receive_limit,+ )++ _ensure_grpc_receive_limit()++ options = dict(google_ads_client_module._GRPC_CHANNEL_OPTIONS)+ assert options["grpc.max_receive_message_length"] == GRPC_MAX_RECEIVE_MESSAGE_LENGTH+ assert GRPC_MAX_RECEIVE_MESSAGE_LENGTH > self._SDK_DEFAULT+ assert GRPC_MAX_RECEIVE_MESSAGE_LENGTH > self._LARGEST_OBSERVED_PAYLOAD++ def test_is_idempotent(self):+ from google.ads.googleads import client as google_ads_client_module++ from posthog.temporal.data_imports.sources.google_ads.google_ads import _ensure_grpc_receive_limit++ _ensure_grpc_receive_limit()+ _ensure_grpc_receive_limit()++ keys = [key for key, _ in google_ads_client_module._GRPC_CHANNEL_OPTIONS]+ assert keys.count("grpc.max_receive_message_length") == 1++class TestValidateCredentials:def test_missing_integration_does_not_exist_returns_reconnect_message(self):# `google_ads_client` calls `Integration.objects.get(...)`, which raises the typed