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(customer_io): retry transient 5xx/429 on list-endpoint syncs (#75994)

Tom Owers69f0d581cf32
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/api_client.py b/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/api_client.py
index 1488623a7dce..7c02a9b08803 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/api_client.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/api_client.py
@@ -10,6 +10,7 @@ from typing import Any
import requests
import structlog
+from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential_jitter
from products.warehouse_sources.backend.temporal.data_imports.sources.common.base import (
ExternalWebhookInfo,
@@ -32,6 +33,14 @@ REPORTING_WEBHOOKS_PATH = "/v1/reporting_webhooks"
WORKSPACES_PATH = "/v1/workspaces"
REQUEST_TIMEOUT_SECONDS = 30
+# Stable sentinel prefix — matched by `CustomerIOSource.get_retryable_errors` so an exhausted
+# retry budget doesn't get tracked as a bug.
+LIST_ENDPOINT_RETRYABLE_ERROR_PREFIX = "Customer.io API error (retryable)"
+
+
+class ListEndpointRetryableError(Exception):
+ pass
+
def _base_url(region: str | None) -> str:
return CIO_EU_BASE_URL if (region or "").lower() == "eu" else CIO_US_BASE_URL
@@ -106,6 +115,23 @@ def validate_credentials(api_key: str, region: str | None) -> tuple[bool, str |
return True, None
+@retry(
+ retry=retry_if_exception_type((ListEndpointRetryableError, requests.ReadTimeout, requests.ConnectionError)),
+ stop=stop_after_attempt(5),
+ wait=wait_exponential_jitter(initial=1, max=30),
+ reraise=True,
+)
+def _get_list_page(session: requests.Session, url: str, params: dict[str, Any]) -> dict[str, Any]:
+ response = session.get(url, params=params, timeout=REQUEST_TIMEOUT_SECONDS)
+ # Customer.io's list endpoints occasionally return a transient 429/5xx (e.g. a brief
+ # `503 Service Unavailable`); back off and retry in-process rather than failing the whole
+ # sync on a blip Temporal would just retry anyway.
+ if response.status_code == 429 or response.status_code >= 500:
+ raise ListEndpointRetryableError(f"{LIST_ENDPOINT_RETRYABLE_ERROR_PREFIX}: status={response.status_code}")
+ response.raise_for_status()
+ return response.json() or {}
+
+
def iterate_list_endpoint(
api_key: str,
region: str | None,
@@ -126,9 +152,7 @@ def iterate_list_endpoint(
params["limit"] = endpoint.page_size
while True:
- response = session.get(base, params=params, timeout=REQUEST_TIMEOUT_SECONDS)
- response.raise_for_status()
- payload = response.json() or {}
+ payload = _get_list_page(session, base, params)
rows = payload.get(endpoint.response_key) or []
if isinstance(rows, list):
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/source.py
index 21c6c9091ea4..249c38f46c7a 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/source.py
@@ -198,6 +198,17 @@ class CustomerIOSource(
),
}
+ def get_retryable_errors(self) -> set[str]:
+ # `_get_list_page` already retries 429/5xx and connection/read-timeout errors with
+ # backoff (see api_client.py); if that budget still exhausts, Temporal retries the
+ # whole activity and the failure is transient and self-recovering, so don't surface it
+ # as tracked exception noise.
+ return {
+ api_client.LIST_ENDPOINT_RETRYABLE_ERROR_PREFIX,
+ "HTTPSConnectionPool(host='api.customer.io', port=443)",
+ "HTTPSConnectionPool(host='api-eu.customer.io', port=443)",
+ }
+
def get_canonical_descriptions(self) -> CanonicalDescriptions:
from products.warehouse_sources.backend.temporal.data_imports.sources.customer_io.canonical_descriptions import (
CANONICAL_DESCRIPTIONS,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/tests/test_api_client.py b/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/tests/test_api_client.py
index da06483e4d9b..cba0e66459fc 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/tests/test_api_client.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/tests/test_api_client.py
@@ -1,3 +1,4 @@
+import pytest
from unittest.mock import MagicMock, patch
import requests
@@ -404,6 +405,35 @@ class TestGetExternalWebhookInfo:
assert "App API Key" in info.error
+class TestGetListPage:
+ @parameterized.expand(
+ [
+ ("rate_limited", 429),
+ ("internal_server_error", 500),
+ ("service_unavailable", 503),
+ ]
+ )
+ def test_transient_statuses_raise_retryable(self, _name, status_code):
+ response = MagicMock()
+ response.status_code = status_code
+ session = MagicMock()
+ session.get.return_value = response
+
+ # Call the undecorated function so tenacity's exponential backoff doesn't sleep in tests.
+ with pytest.raises(api_client.ListEndpointRetryableError):
+ api_client._get_list_page.__wrapped__(session, f"{CIO_US_BASE_URL}/v1/collections", {}) # type: ignore[attr-defined]
+
+ def test_non_transient_error_status_raises_http_error(self):
+ response = MagicMock()
+ response.status_code = 401
+ response.raise_for_status.side_effect = requests.HTTPError(response=response)
+ session = MagicMock()
+ session.get.return_value = response
+
+ with pytest.raises(requests.HTTPError):
+ api_client._get_list_page.__wrapped__(session, f"{CIO_US_BASE_URL}/v1/collections", {}) # type: ignore[attr-defined]
+
+
class TestIterateListEndpoint:
@patch("products.warehouse_sources.backend.temporal.data_imports.sources.customer_io.api_client._session")
def test_yields_rows_from_single_page_endpoint(self, mock_session):
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/tests/test_customer_io_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/tests/test_customer_io_source.py
index 7c3a08a37e08..caa7a9d10260 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/tests/test_customer_io_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/tests/test_customer_io_source.py
@@ -14,6 +14,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common.bas
WebhookDeletionResult,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.common.typings import SourceResponse
+from products.warehouse_sources.backend.temporal.data_imports.sources.customer_io import api_client
from products.warehouse_sources.backend.temporal.data_imports.sources.customer_io.constants import (
CIO_API_SCHEMA_NAMES,
CIO_WEBHOOK_SCHEMA_NAMES,
@@ -97,6 +98,16 @@ class TestCustomerIOSourceGetSchemas:
assert webhook_only == set(CIO_WEBHOOK_SCHEMA_NAMES)
+class TestCustomerIOSourceGetRetryableErrors:
+ def test_matches_list_endpoint_retryable_sentinel(self):
+ source = CustomerIOSource()
+ retryable_errors = source.get_retryable_errors()
+
+ error_message = f"{api_client.LIST_ENDPOINT_RETRYABLE_ERROR_PREFIX}: status=503"
+
+ assert any(pattern in error_message for pattern in retryable_errors)
+
+
class TestCustomerIOSourceCreateWebhook:
@patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.customer_io.source.api_client.create_webhook"