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): Handle retrying on getting SA description (#111737)

Tomás Farías Santana61cdf3ae4eac
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 3f00c6eb3b7f..a129991278a6 100644
--- a/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.py
+++ b/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.py
@@ -20,6 +20,7 @@ import google.auth.transport.requests
import google.auth.impersonated_credentials
from google.api_core.exceptions import (
BadRequest,
+ DeadlineExceeded,
Forbidden,
GatewayTimeout,
GoogleAPICallError,
@@ -480,12 +481,16 @@ async def get_service_account_description(
client = iam_admin_v1.IAMAsyncClient(credentials=our_credentials)
retryable_get_service_account = make_retryable_with_exponential_backoff(
- client.get_service_account, retryable_exceptions=(InternalServerError,), max_attempts=max_attempts
+ client.get_service_account,
+ retryable_exceptions=(InternalServerError, DeadlineExceeded, ServiceUnavailable),
+ max_attempts=max_attempts,
)
try:
sa = await retryable_get_service_account(
- request=iam_admin_v1.GetServiceAccountRequest(name=f"projects/-/serviceAccounts/{service_account_email}")
+ request=iam_admin_v1.GetServiceAccountRequest(name=f"projects/-/serviceAccounts/{service_account_email}"),
+ # Retries are handled by our wrapper, have the client just raise
+ retry=None,
)
except PermissionDenied:
EXTERNAL_LOGGER.exception(
diff --git a/products/batch_exports/backend/tests/temporal/destinations/bigquery/test_bigquery_utils.py b/products/batch_exports/backend/tests/temporal/destinations/bigquery/test_bigquery_utils.py
index 080be064ab9c..ca91e10ed74a 100644
--- a/products/batch_exports/backend/tests/temporal/destinations/bigquery/test_bigquery_utils.py
+++ b/products/batch_exports/backend/tests/temporal/destinations/bigquery/test_bigquery_utils.py
@@ -4,13 +4,14 @@ import os
import datetime as dt
import pytest
-from unittest.mock import MagicMock, patch
+from unittest.mock import DEFAULT, AsyncMock, MagicMock, patch
from django.test import override_settings
import pyarrow as pa
+from google.api_core.exceptions import DeadlineExceeded, ServiceUnavailable
from google.auth.exceptions import RefreshError
-from google.cloud import bigquery
+from google.cloud import bigquery, iam_admin_v1
from posthog.models.integration import GoogleCloudServiceAccountIntegration
@@ -20,6 +21,7 @@ from products.batch_exports.backend.temporal.destinations.bigquery_batch_export
GoogleCloudCredentialsError,
ServiceAccountOwnershipError,
ensure_our_google_cloud_credentials_are_valid,
+ get_our_google_cloud_credentials,
get_service_account_description,
verify_impersonated_service_account_ownership,
)
@@ -133,6 +135,32 @@ async def test_get_service_account_description(
assert description == service_account_description
+@SKIP_IF_MISSING_GOOGLE_APPLICATION_CREDENTIALS
[email protected]("integration", ["impersonated"], indirect=True)
[email protected]("service_account_description", ["any"], indirect=True)
+async def test_get_service_account_description_retries(
+ aorganization,
+ integration,
+ service_account_description,
+) -> None:
+ service_account_integration = GoogleCloudServiceAccountIntegration(integration)
+ async with iam_admin_v1.IAMAsyncClient(credentials=get_our_google_cloud_credentials()) as client:
+ with (
+ patch.object(
+ iam_admin_v1.IAMAsyncClient,
+ "get_service_account",
+ wraps=client.get_service_account,
+ side_effect=[DeadlineExceeded("Deadline exceeded"), ServiceUnavailable("Unavailable"), DEFAULT],
+ ) as get_service_account,
+ patch("products.batch_exports.backend.temporal.utils.asyncio.sleep", new_callable=AsyncMock),
+ ):
+ description = await get_service_account_description(service_account_integration.service_account_email)
+
+ assert description == service_account_description
+ assert get_service_account.await_count == 3
+
+
@SKIP_IF_MISSING_GOOGLE_APPLICATION_CREDENTIALS
@pytest.mark.asyncio
@pytest.mark.parametrize("integration", ["impersonated"], indirect=True)