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(mixpanel): re-fetch the day when an export stream is cut short (#94687)

Tom Owers85228eb7c3d6
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mixpanel/mixpanel.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mixpanel/mixpanel.py
index 3665c74914d1..ddeab8957db9 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mixpanel/mixpanel.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mixpanel/mixpanel.py
@@ -258,6 +258,15 @@ def _request(
return _check_response(response, url, logger)
+class MixpanelExportTruncatedError(Exception):
+ """The export stream ended in a line that isn't an event.
+
+ Mixpanel commits a 200 before it starts streaming, so a server-side abort can only be
+ signalled inside the body: it appends a plain-text marker (`terminated early`) after the
+ JSONL rows instead of failing the request. That leaves a partial day, which is the same
+ situation as a dropped connection and is re-fetched the same way."""
+
+
# The export body is read lazily while iterating `iter_lines`, i.e. outside `_request`'s
# retry. A connection dropped mid-day surfaces there (`requests` wraps the underlying
# `IncompleteRead` as `ChunkedEncodingError`), so it must be retried separately or a single
@@ -267,6 +276,7 @@ _STREAM_RETRYABLE_ERRORS = (
requests.exceptions.ChunkedEncodingError,
requests.ConnectionError,
requests.ReadTimeout,
+ MixpanelExportTruncatedError,
)
@@ -294,7 +304,13 @@ def _stream_export_day(
for line in response.iter_lines():
if not line:
continue
- batch.append(_flatten_event(orjson.loads(line)))
+ try:
+ event = orjson.loads(line)
+ except orjson.JSONDecodeError as e:
+ raise MixpanelExportTruncatedError(
+ f"Mixpanel export: stream for {from_date} ended with a line that is not an event"
+ ) from e
+ batch.append(_flatten_event(event))
if len(batch) >= CHUNK_SIZE:
yield batch
batch = []
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mixpanel/test_mixpanel.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mixpanel/test_mixpanel.py
index 21ae27177384..25265494784b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mixpanel/test_mixpanel.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mixpanel/test_mixpanel.py
@@ -411,6 +411,35 @@ class TestExportStreamRetry:
# Cursor only advances once the day finally completes.
assert [s.from_date for s in manager.saved] == ["2024-01-02"]
+ def test_retries_day_when_stream_ends_in_a_non_event_line(self) -> None:
+ manager = FakeManager()
+ day = date(2024, 1, 1)
+ truncated = FakeResponse(lines=[self._line("i1"), b"terminated early"])
+ succeeding = FakeResponse(lines=[self._line("i1"), self._line("i2")])
+ with (
+ patch.object(mp, "_request", side_effect=[truncated, succeeding]) as mock_request,
+ patch.object(mp.time, "sleep") as mock_sleep,
+ ):
+ batches = list(
+ mp._iter_export(
+ "us",
+ "u",
+ "s",
+ "123",
+ LOGGER,
+ manager, # type: ignore[arg-type]
+ start_date=day,
+ end_date=day,
+ api_version=MIXPANEL_API_VERSION_V1,
+ )
+ )
+
+ rows = [row for batch in batches for row in batch]
+ assert [r["$insert_id"] for r in rows] == ["i1", "i2"]
+ assert mock_request.call_count == 2
+ mock_sleep.assert_called_once()
+ assert [s.from_date for s in manager.saved] == ["2024-01-02"]
+
def test_gives_up_after_max_attempts(self) -> None:
manager = FakeManager()
day = date(2024, 1, 1)