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(bigquery): Retry credential refreshes up to 5 times (#73463)

Tomás Farías Santanaac5b17ef134b
diff --git a/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.py b/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.py
index 172d36d0bcfc..50d8ba959a99 100644
--- a/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.py
+++ b/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.py
@@ -433,14 +433,36 @@ class GoogleCloudCredentialsError(Exception):
async def ensure_our_google_cloud_credentials_are_valid():
- """Raise `InvalidCredentialsError` if we cannot refresh our credentials."""
+ """Raise `GoogleCloudCredentialsError` if we cannot refresh our credentials."""
+
our_credentials = get_our_google_cloud_credentials()
+ session = _make_requests_session()
try:
- await asyncio.to_thread(our_credentials.refresh, google.auth.transport.requests.Request())
+ await asyncio.to_thread(our_credentials.refresh, google.auth.transport.requests.Request(session=session))
except Exception as e:
raise GoogleCloudCredentialsError from e
+def _make_requests_session() -> "requests.Session":
+ """Make a requests.Session for Google credentials refresh requests."""
+ import requests
+ from requests.adapters import HTTPAdapter
+ from urllib3.util.retry import Retry
+
+ retry = Retry(
+ total=5,
+ connect=5, # Redundant to set this + total, but just being explicit
+ backoff_factor=1.0, # 0s, 2s, 4s, 6s, ...
+ )
+
+ session = requests.Session()
+ adapter = HTTPAdapter(max_retries=retry)
+ session.mount("https://", adapter)
+ session.mount("http://", adapter)
+
+ return session
+
+
async def get_service_account_description(
service_account_email: str,
) -> str: