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(warehouse-sources): respect Etsy's per-endpoint window and offset limits (#99945)

posthog[bot]342258d824e7
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/etsy/etsy.py b/products/warehouse_sources/backend/temporal/data_imports/sources/etsy/etsy.py
index f93109bdcc20..85cc00396fe6 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/etsy/etsy.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/etsy/etsy.py
@@ -182,6 +182,10 @@ def _rows_from_results(results: list[Any], config: EtsyEndpointConfig) -> list[d
return rows
+def _max_offset(config: EtsyEndpointConfig) -> int:
+ return MAX_OFFSET if config.max_offset is None else config.max_offset
+
+
def _fetch_page(
client: EtsyClient, path: str, params: dict[str, Any], offset: int, config: EtsyEndpointConfig
) -> tuple[list[dict[str, Any]], int, int]:
@@ -205,9 +209,16 @@ def _windowed_pages(
) -> Iterator[list[dict[str, Any]]]:
"""Walk `start`..`end` in time slices, offset-paging each one.
- Etsy's 12,000 offset ceiling means a slice holding more rows than that can never be read to the
- end, so an oversized slice is halved (down to `MIN_WINDOW_SECONDS`) and retried instead.
+ A slice holding more rows than its offset ceiling can reach can never be read to the end, so an
+ oversized slice is halved (down to `MIN_WINDOW_SECONDS`) and retried instead. Both the slice
+ width and the ceiling vary by endpoint: Etsy rejects a window wider than `max_window_seconds`,
+ and serves no offset above `max_offset`.
"""
+ window_seconds = config.max_window_seconds or DEFAULT_WINDOW_SECONDS
+ max_offset = _max_offset(config)
+ # Offsets step by PAGE_SIZE from zero, so the ceiling itself is still a readable page.
+ readable_rows = max_offset + PAGE_SIZE
+
pending: deque[tuple[int, int]] = deque()
cursor = start
offset = 0
@@ -222,7 +233,7 @@ def _windowed_pages(
window_start, window_end = pending.popleft()
else:
window_start = cursor
- window_end = min(cursor + DEFAULT_WINDOW_SECONDS - 1, end)
+ window_end = min(cursor + window_seconds - 1, end)
cursor = window_end + 1
params = {
@@ -234,7 +245,7 @@ def _windowed_pages(
while True:
rows, page_size, total = _fetch_page(client, path, params, offset, config)
- if offset == 0 and total > MAX_OFFSET and (window_end - window_start) >= MIN_WINDOW_SECONDS:
+ if offset == 0 and total > readable_rows and (window_end - window_start) >= MIN_WINDOW_SECONDS:
midpoint = window_start + (window_end - window_start) // 2
pending.appendleft((midpoint + 1, window_end))
pending.appendleft((window_start, midpoint))
@@ -250,7 +261,7 @@ def _windowed_pages(
if page_size == 0 or page_size < PAGE_SIZE or offset >= total:
offset = 0
break
- if offset > MAX_OFFSET:
+ if offset > max_offset:
logger.warning(
f"Etsy offset ceiling reached for {config.name} between {window_start} and {window_end}; "
f"{total - offset} rows in this window were not read"
@@ -272,6 +283,7 @@ def _offset_pages(
# State written by the windowed walk is meaningless here, so only a windowless checkpoint resumes.
resumable = resume if resume is not None and resume.window_start is None else None
offset = resumable.offset if resumable is not None else 0
+ max_offset = _max_offset(config)
if resumable is not None and resumable.listing_state is not None and resumable.listing_state in states:
states = states[states.index(resumable.listing_state) :]
@@ -292,7 +304,7 @@ def _offset_pages(
if page_size == 0 or page_size < PAGE_SIZE or offset >= total:
break
- if offset > MAX_OFFSET:
+ if offset > max_offset:
logger.warning(
f"Etsy offset ceiling reached for {config.name} (state={state}); "
f"{total - offset} rows were not read"
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/etsy/settings.py b/products/warehouse_sources/backend/temporal/data_imports/sources/etsy/settings.py
index 4554225d1be2..473ec540394a 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/etsy/settings.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/etsy/settings.py
@@ -35,6 +35,10 @@ class EtsyEndpointConfig:
# Filter prefix used to walk history when the schema is NOT synced incrementally. `None` means
# the endpoint has no time filter at all, so it is paginated by offset alone.
default_window_param: str | None = None
+ # Etsy caps the window narrower and the offset lower on some endpoints, rejecting the request
+ # outright above either. `None` takes the source-wide default.
+ max_window_seconds: int | None = None
+ max_offset: int | None = None
# Extra query params sent on every request for this endpoint.
extra_params: dict[str, str] = field(default_factory=dict)
# Endpoint returns a bare object rather than the {count, results} envelope.
@@ -98,6 +102,9 @@ ETSY_ENDPOINTS: dict[str, EtsyEndpointConfig] = {
window_params={"created_timestamp": "created"},
incremental_fields=[_CREATED],
default_window_param="created",
+ # Etsy answers getReviewsByShop anonymously however the request is authenticated, and
+ # caps an anonymous request at the first page.
+ max_offset=0,
),
"ledger_entries": EtsyEndpointConfig(
name="ledger_entries",
@@ -105,8 +112,10 @@ ETSY_ENDPOINTS: dict[str, EtsyEndpointConfig] = {
primary_keys=["entry_id"],
window_params={"created_timestamp": "created"},
incremental_fields=[_CREATED],
- # Etsy rejects this endpoint without min_created/max_created, so it is always windowed.
+ # Etsy rejects this endpoint without min_created/max_created, so it is always windowed,
+ # and rejects a window wider than 31 days.
default_window_param="created",
+ max_window_seconds=31 * 24 * 60 * 60,
),
}
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/etsy/tests/test_etsy.py b/products/warehouse_sources/backend/temporal/data_imports/sources/etsy/tests/test_etsy.py
index c0143d02e8a3..454c74b71a0a 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/etsy/tests/test_etsy.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/etsy/tests/test_etsy.py
@@ -306,7 +306,7 @@ class TestEtsyTransport:
def test_oversized_window_is_halved_instead_of_hitting_the_offset_ceiling(self) -> None:
# First probe reports more rows than the offset ceiling can reach, so the slice splits.
session = _FakeSession(
- [_page(_rows(PAGE_SIZE), MAX_OFFSET + 1), _page(_rows(1), 1), _page(_rows(1, start=1), 1)]
+ [_page(_rows(PAGE_SIZE), MAX_OFFSET + PAGE_SIZE + 1), _page(_rows(1), 1), _page(_rows(1, start=1), 1)]
)
rows, _ = _collect(session, "receipts")
@@ -328,6 +328,30 @@ class TestEtsyTransport:
assert window["max_created"] - window["min_created"] < MIN_WINDOW_SECONDS
assert len(rows) == MAX_OFFSET + PAGE_SIZE
+ @time_machine.travel("2005-03-01", tick=False)
+ def test_ledger_windows_stay_inside_etsys_31_day_maximum(self) -> None:
+ session = _FakeSession([_page([], 0) for _ in range(2)])
+ _collect(session, "ledger_entries")
+
+ windows = [(call[1]["min_created"], call[1]["max_created"]) for call in session.get_calls]
+ assert len(windows) == 2
+ assert max(end - start for start, end in windows) <= 31 * 24 * 60 * 60
+
+ @time_machine.travel("2005-01-15", tick=False)
+ def test_reviews_split_the_window_rather_than_paging_past_the_first_page(self) -> None:
+ # Offset 100 is the request Etsy rejects; the window has to narrow instead.
+ session = _FakeSession(
+ [
+ _page(_rows(PAGE_SIZE, key="transaction_id"), PAGE_SIZE + 1),
+ _page(_rows(1, key="transaction_id"), 1),
+ _page(_rows(1, start=1, key="transaction_id"), 1),
+ ]
+ )
+ rows, _ = _collect(session, "reviews")
+
+ assert [call[1]["offset"] for call in session.get_calls] == [0, 0, 0]
+ assert len(rows) == 2
+
@time_machine.travel("2005-01-15", tick=False)
def test_resume_finishes_the_saved_window_then_continues_after_it(self) -> None:
saved_end = ETSY_HISTORY_START + 1000