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(amplitude): treat a corrupt export archive as retryable (#120039)

Tom Owerse8b6ccc32d31
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/amplitude/amplitude.py b/products/warehouse_sources/backend/temporal/data_imports/sources/amplitude/amplitude.py
index eef82e2802f9..6ca8594b1725 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/amplitude/amplitude.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/amplitude/amplitude.py
@@ -52,6 +52,12 @@ class AmplitudeRetryableError(Exception):
pass
+# Amplitude has occasionally served a 2xx export response whose body isn't a valid zip
+# archive (e.g. truncated by a network blip). Raised as retryable so Temporal retries the
+# activity and re-downloads the window, rather than failing the sync outright.
+AMPLITUDE_INVALID_ARCHIVE_ERROR = "Amplitude export response was not a valid zip archive"
+
+
@dataclasses.dataclass
class AmplitudeResumeConfig:
# ISO 8601 UTC timestamp marking the start of the next export window to fetch.
@@ -169,13 +175,24 @@ def _iter_export_window(
for chunk in response.iter_content(chunk_size=EXPORT_DOWNLOAD_CHUNK_BYTES):
buffer.write(chunk)
buffer.seek(0)
- with zipfile.ZipFile(buffer) as archive:
- for entry in archive.namelist():
- with archive.open(entry) as raw, gzip.open(raw) as decompressed:
- for line in decompressed:
- if not line.strip():
- continue
- yield _normalize_event(json.loads(line))
+ try:
+ archive = zipfile.ZipFile(buffer)
+ except zipfile.BadZipFile as e:
+ raise AmplitudeRetryableError(f"{AMPLITUDE_INVALID_ARCHIVE_ERROR}: url={url}") from e
+
+ # A central directory can parse fine while an individual entry's payload is corrupt
+ # (truncated gzip member, bad CRC) — that only surfaces once the entry is read, so it
+ # needs its own retryable classification alongside the archive-open check above.
+ try:
+ with archive:
+ for entry in archive.namelist():
+ with archive.open(entry) as raw, gzip.open(raw) as decompressed:
+ for line in decompressed:
+ if not line.strip():
+ continue
+ yield _normalize_event(json.loads(line))
+ except (zipfile.BadZipFile, gzip.BadGzipFile, EOFError) as e:
+ raise AmplitudeRetryableError(f"{AMPLITUDE_INVALID_ARCHIVE_ERROR}: url={url}") from e
def _get_events_rows(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/amplitude/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/amplitude/source.py
index af9f51aa4406..3d9ec79e5739 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/amplitude/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/amplitude/source.py
@@ -13,6 +13,7 @@ from products.warehouse_sources.backend.facade.source_config import (
SourceFieldSelectConfigOption,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.amplitude.amplitude import (
+ AMPLITUDE_INVALID_ARCHIVE_ERROR,
AmplitudeResumeConfig,
amplitude_source,
validate_credentials as validate_amplitude_credentials,
@@ -162,6 +163,11 @@ class AmplitudeSource(ResumableSource[AmplitudeSourceConfig, AmplitudeResumeConf
"Invalid API Key": message,
}
+ def get_retryable_errors(self) -> set[str]:
+ # A corrupt export body is a transient download problem, not a configuration error —
+ # Temporal retries the activity and re-downloads the same window from scratch.
+ return {AMPLITUDE_INVALID_ARCHIVE_ERROR}
+
def get_canonical_descriptions(self) -> CanonicalDescriptions:
from products.warehouse_sources.backend.temporal.data_imports.sources.amplitude.canonical_descriptions import (
CANONICAL_DESCRIPTIONS,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/amplitude/tests/test_amplitude.py b/products/warehouse_sources/backend/temporal/data_imports/sources/amplitude/tests/test_amplitude.py
index df3b7fdc5eec..af6a2b6fb525 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/amplitude/tests/test_amplitude.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/amplitude/tests/test_amplitude.py
@@ -15,6 +15,7 @@ from parameterized import parameterized
from products.warehouse_sources.backend.temporal.data_imports.sources.amplitude.amplitude import (
AmplitudeResumeConfig,
+ AmplitudeRetryableError,
_auth_headers,
_iter_export_window,
_normalize_event,
@@ -197,6 +198,34 @@ class TestIterExportWindow:
)
assert [row["uuid"] for row in rows] == ["a", "b"]
+ def test_invalid_zip_body_raises_retryable_error(self) -> None:
+ # Amplitude has occasionally served a 2xx response whose body isn't a valid archive
+ # (e.g. truncated by a network blip). That must surface as a retryable failure so
+ # Temporal retries the activity, not as an unhandled BadZipFile crash.
+ session = self._session_returning(_response(status=200, content=b"not a zip file"))
+
+ with pytest.raises(AmplitudeRetryableError):
+ list(
+ _iter_export_window(
+ session, "https://amplitude.com", {}, "20260101T00", "20260101T23", mock.MagicMock()
+ )
+ )
+
+ def test_corrupt_entry_payload_raises_retryable_error(self) -> None:
+ # The central directory parses fine, but an entry's payload isn't valid gzip data.
+ # That only surfaces while the entry is being read, not when the archive is opened.
+ buf = io.BytesIO()
+ with zipfile.ZipFile(buf, "w") as archive:
+ archive.writestr("part_0.json.gz", b"not actually gzip data")
+ session = self._session_returning(_response(status=200, content=buf.getvalue()))
+
+ with pytest.raises(AmplitudeRetryableError):
+ list(
+ _iter_export_window(
+ session, "https://amplitude.com", {}, "20260101T00", "20260101T23", mock.MagicMock()
+ )
+ )
+
def test_non_ok_status_raises(self) -> None:
response = _response(status=400, text="bad request")
# Assign the class (not an instance) so the requests-stubs `response` arg isn't required;
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/amplitude/tests/test_amplitude_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/amplitude/tests/test_amplitude_source.py
index a210aa4b8229..7d05575641e1 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/amplitude/tests/test_amplitude_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/amplitude/tests/test_amplitude_source.py
@@ -1,5 +1,8 @@
import time_machine
+from products.warehouse_sources.backend.temporal.data_imports.sources.amplitude.amplitude import (
+ AMPLITUDE_INVALID_ARCHIVE_ERROR,
+)
from products.warehouse_sources.backend.temporal.data_imports.sources.amplitude.settings import EVENTS_ENDPOINT
from products.warehouse_sources.backend.temporal.data_imports.sources.amplitude.source import AmplitudeSource
from products.warehouse_sources.backend.temporal.data_imports.sources.common.testing import (
@@ -30,3 +33,7 @@ class TestAmplitudeSource:
# When the user isn't running incrementally, no watermark should leak through.
assert result.requests[0].param("start") == "20260505T10"
assert result.raised is None
+
+ def test_invalid_archive_error_is_classified_retryable(self) -> None:
+ error_message = f"{AMPLITUDE_INVALID_ARCHIVE_ERROR}: url=https://amplitude.com/api/2/export?start=20260101T00&end=20260101T23"
+ assert any(pattern in error_message for pattern in self.source.get_retryable_errors())