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(data-warehouse): reduce linkedin ads api calls and handle daily rate limits (#59608)

Tom Owers0aaf55a5897a
diff --git a/posthog/temporal/data_imports/sources/linkedin_ads/client.py b/posthog/temporal/data_imports/sources/linkedin_ads/client.py
index 952f913dbce9..a8b44cbac1b6 100644
--- a/posthog/temporal/data_imports/sources/linkedin_ads/client.py
+++ b/posthog/temporal/data_imports/sources/linkedin_ads/client.py
@@ -1,3 +1,4 @@
+import re
import json
import datetime as dt
from collections.abc import Generator
@@ -6,7 +7,7 @@ from typing import Any, Optional
import requests
import structlog
from linkedin_api.clients.restli.client import RestliClient
-from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential_jitter
+from tenacity import RetryCallState, retry, retry_if_exception_type, stop_after_attempt, wait_exponential_jitter
from .schemas import (
LINKEDIN_ADS_ENDPOINTS,
@@ -22,13 +23,84 @@ LINKEDIN_SPONSORED_URN_PREFIX = "urn:li:sponsored"
MAX_PAGE_SIZE = 1000
CREATIVES_PAGE_SIZE = 100 # creatives backend returns transient 500s on heavier requests
API_VERSION = "202508"
-# `q=analytics` truncates at 15k rows; we slice the date range to stay under it.
+
+# `q=analytics` silently truncates a response at 15k elements. With DAILY granularity an element
+# is one (day × active pivot value), so a window of N days over an account with P active pivot
+# values returns ~N*P elements. Rather than a fixed weekly window (which makes ~52 calls/year for
+# a 1-campaign account just like a 1000-campaign one), we start wide and shrink the window only
+# when a response comes back truncated. Small accounts then sync a long range in a single call,
+# which is what keeps us under LinkedIn's per-member daily call budget. The window size is carried
+# forward across chunks so we discover the right size once instead of per chunk.
ANALYTICS_RESPONSE_CAP = 15000
-ANALYTICS_CHUNK_DAYS = 7
+ANALYTICS_INITIAL_CHUNK_DAYS = 365
+ANALYTICS_MIN_CHUNK_DAYS = 1
+# Below this fill level a window has enough headroom to widen for the next chunk, so a single dense
+# spike that forced a shrink doesn't keep the window small across an otherwise sparse date range.
+ANALYTICS_GROW_THRESHOLD = ANALYTICS_RESPONSE_CAP // 4
+
+# Upper bound on how long we'll honour a Retry-After header before giving up, so a hostile or
+# misconfigured value can't hang the activity past its heartbeat.
+MAX_RETRY_AFTER_SECONDS = 60.0
+
+# LinkedIn 429 throttle bodies name the window that was exceeded (SECOND / MINUTE / DAY). A DAY
+# throttle is the per-member/app daily call budget and only resets at midnight UTC, so it must not
+# be retried within the run.
+_DAY_THROTTLE_PATTERN = re.compile(r"\bDAY\b")
class LinkedinAdsRetryableError(Exception):
- """Transient LinkedIn API error (5xx / 429) that should be retried."""
+ """Transient LinkedIn API error (5xx / short-window 429) that should be retried.
+
+ `retry_after` carries the parsed Retry-After header (seconds) when LinkedIn provides one,
+ so the retry wait can honour it instead of the default exponential backoff.
+ """
+
+ def __init__(self, message: str, retry_after: float | None = None) -> None:
+ super().__init__(message)
+ self.retry_after = retry_after
+
+
+class LinkedinAdsDailyRateLimitError(Exception):
+ """LinkedIn per-day rate limit reached (429 with a DAY throttle window).
+
+ Not retried within the run: the member/app daily call budget only resets at midnight UTC, so
+ retrying just burns the remaining budget. We fail fast and let the next scheduled sync resume.
+ """
+
+
+def _parse_retry_after(response: Any) -> float | None:
+ """Pull a numeric Retry-After (seconds) off the underlying requests.Response, if present.
+ LinkedIn sends integer seconds; HTTP-date forms and negative/garbage values are ignored
+ (treated as absent), so a malformed header falls back to exponential backoff rather than a
+ zero/negative sleep that would either hammer the API or crash tenacity's `time.sleep`."""
+ headers = getattr(getattr(response, "response", None), "headers", None)
+ if not headers:
+ return None
+ raw = headers.get("Retry-After")
+ if raw is None:
+ return None
+ try:
+ seconds = float(raw)
+ except (TypeError, ValueError):
+ return None
+ return seconds if seconds >= 0 else None
+
+
+def _is_daily_throttle(body: str | None) -> bool:
+ return bool(body and _DAY_THROTTLE_PATTERN.search(body))
+
+
+_exponential_wait = wait_exponential_jitter(initial=1, max=30)
+
+
+def _retry_wait(retry_state: RetryCallState) -> float:
+ """Honour a Retry-After (capped) when the failing call surfaced one, else fall back to
+ exponential jitter for transient transport / 5xx errors."""
+ exc = retry_state.outcome.exception() if retry_state.outcome else None
+ retry_after = getattr(exc, "retry_after", None)
+ if retry_after is not None:
+ return min(float(retry_after), MAX_RETRY_AFTER_SECONDS)
+ return _exponential_wait(retry_state)
class LinkedinAdsClient:
@@ -91,10 +163,12 @@ class LinkedinAdsClient:
date_start: Optional[str] = None,
date_end: Optional[str] = None,
) -> Generator[tuple[list[dict[str, Any]], None], None, None]:
- """Fetch analytics in weekly chunks to stay under the 15k-row response
- cap. Capped chunks yield partial data and log a warning — we don't
- retry/split because extra calls under rate-limit pressure cost more
- than the missing rows recover."""
+ """Fetch analytics in adaptive date-range chunks sized to stay under the 15k-element
+ response cap. The window starts wide and shrinks (halving) only when a response comes
+ back truncated, then carries the learned size forward. A truncated response is silently
+ capped by LinkedIn — we can't tell which rows were dropped — so we discard it and re-fetch
+ the same start with a smaller window rather than yielding partial data. Only a single-day
+ window that still caps is surfaced (with a warning), since it can't be split further."""
resource_by_pivot = {
LinkedinAdsPivot.CAMPAIGN: LinkedinAdsResource.CampaignStats,
LinkedinAdsPivot.CAMPAIGN_GROUP: LinkedinAdsResource.CampaignGroupStats,
@@ -123,16 +197,26 @@ class LinkedinAdsClient:
)
return
+ chunk_days = ANALYTICS_INITIAL_CHUNK_DAYS
chunk_start = dt.date.fromisoformat(date_start)
end_date = dt.date.fromisoformat(date_end)
while chunk_start <= end_date:
- chunk_end = min(chunk_start + dt.timedelta(days=ANALYTICS_CHUNK_DAYS - 1), end_date)
+ chunk_end = min(chunk_start + dt.timedelta(days=chunk_days - 1), end_date)
elements = self._make_request(
endpoint=resource,
finder="analytics",
extra_params=_build_params(chunk_start.isoformat(), chunk_end.isoformat()),
)
- if len(elements) >= ANALYTICS_RESPONSE_CAP:
+ capped = len(elements) >= ANALYTICS_RESPONSE_CAP
+ window_days = (chunk_end - chunk_start).days + 1
+
+ # Truncated response and the window spans more than a day: halve it (by its real span,
+ # so a window already clamped by end_date converges fast) and re-fetch the same start.
+ if capped and window_days > ANALYTICS_MIN_CHUNK_DAYS:
+ chunk_days = max(ANALYTICS_MIN_CHUNK_DAYS, window_days // 2)
+ continue
+
+ if capped:
logger.warning(
"linkedin_ads.analytics_chunk_capped",
pivot=pivot.value,
@@ -140,9 +224,15 @@ class LinkedinAdsClient:
chunk_end=chunk_end.isoformat(),
received=len(elements),
)
+
yield elements, None
chunk_start = chunk_end + dt.timedelta(days=1)
+ # Plenty of headroom left: widen the next window so a sparse range doesn't keep paying
+ # for a window we shrank to fit an earlier dense spike.
+ if not capped and len(elements) < ANALYTICS_GROW_THRESHOLD and chunk_days < ANALYTICS_INITIAL_CHUNK_DAYS:
+ chunk_days = min(ANALYTICS_INITIAL_CHUNK_DAYS, chunk_days * 2)
+
def get_data_by_resource(
self,
resource: LinkedinAdsResource,
@@ -179,7 +269,7 @@ class LinkedinAdsClient:
@retry(
retry=retry_if_exception_type((LinkedinAdsRetryableError, requests.ConnectionError, requests.Timeout)),
stop=stop_after_attempt(5),
- wait=wait_exponential_jitter(initial=1, max=30),
+ wait=_retry_wait,
reraise=True,
)
def _call_finder(self, resource_path: str, finder: str, params: dict[str, Any]) -> Any:
@@ -189,6 +279,11 @@ class LinkedinAdsClient:
returns 504s; those surface here as `requests.ConnectionError` / `requests.Timeout`
from the underlying requests.Session, or as a non-2xx response we convert to
`LinkedinAdsRetryableError`. Non-retryable 4xx responses still raise immediately.
+
+ 429s are split by throttle window: a DAY throttle is the per-member/app daily call budget
+ (resets only at midnight UTC) and raises a non-retryable `LinkedinAdsDailyRateLimitError`
+ so we fail fast instead of burning the remaining budget on doomed retries. Short-window
+ 429s stay retryable and honour the Retry-After header when present.
"""
response = self.client.finder(
resource_path=resource_path,
@@ -198,7 +293,15 @@ class LinkedinAdsClient:
version_string=self.api_version,
)
- if response.status_code == 429 or response.status_code >= 500:
+ if response.status_code == 429:
+ body = response.response.text
+ if _is_daily_throttle(body):
+ raise LinkedinAdsDailyRateLimitError(f"LinkedIn daily rate limit reached (429): {body}")
+ raise LinkedinAdsRetryableError(
+ f"LinkedIn API error (retryable, 429): {body}",
+ retry_after=_parse_retry_after(response),
+ )
+ if response.status_code >= 500:
raise LinkedinAdsRetryableError(
f"LinkedIn API error (retryable, {response.status_code}): {response.response.text}"
)
diff --git a/posthog/temporal/data_imports/sources/linkedin_ads/tests/test_linkedin_client.py b/posthog/temporal/data_imports/sources/linkedin_ads/tests/test_linkedin_client.py
index 6695516fefce..0e63ced7cf07 100644
--- a/posthog/temporal/data_imports/sources/linkedin_ads/tests/test_linkedin_client.py
+++ b/posthog/temporal/data_imports/sources/linkedin_ads/tests/test_linkedin_client.py
@@ -1,4 +1,5 @@
import json
+import datetime as dt
import pytest
from unittest import mock
@@ -8,6 +9,7 @@ from structlog.testing import capture_logs
from posthog.temporal.data_imports.sources.linkedin_ads.client import (
LinkedinAdsClient,
+ LinkedinAdsDailyRateLimitError,
LinkedinAdsPivot,
LinkedinAdsRetryableError,
)
@@ -165,8 +167,9 @@ class TestLinkedinAdsClient:
assert mock_client_instance.finder.call_count == 1
@mock.patch("posthog.temporal.data_imports.sources.linkedin_ads.client.RestliClient")
- def test_get_analytics_chunks_long_date_range_weekly(self, mock_restli_client):
- """Date ranges > 7 days slice into consecutive weekly chunks (no overlap, no gaps)."""
+ def test_get_analytics_uncapped_range_fits_in_single_wide_window(self, mock_restli_client):
+ """When responses never cap, even a long range is fetched in a single wide window —
+ a small account no longer pays one call per week."""
mock_response = mock.MagicMock()
mock_response.status_code = 200
mock_response.elements = [{"impressions": 100}]
@@ -175,33 +178,75 @@ class TestLinkedinAdsClient:
mock_client_instance.finder.return_value = mock_response
client = LinkedinAdsClient(self.access_token)
- # 22-day range → ceil(22/7) = 4 weekly chunks: Jan 1–7, 8–14, 15–21, 22–22.
+ # 200-day range, well under the 365-day initial window → one call.
pages = list(
client.get_analytics(
account_id=self.account_id,
pivot=LinkedinAdsPivot.CREATIVE,
date_start="2024-01-01",
- date_end="2024-01-22",
+ date_end="2024-07-19",
)
)
- assert len(pages) == 4
- assert mock_client_instance.finder.call_count == 4
+ assert len(pages) == 1
+ assert mock_client_instance.finder.call_count == 1
+ assert mock_client_instance.finder.call_args[1]["query_params"]["dateRange"] == {
+ "start": {"year": 2024, "month": 1, "day": 1},
+ "end": {"year": 2024, "month": 7, "day": 19},
+ }
- expected_ranges = [
- {"start": {"year": 2024, "month": 1, "day": 1}, "end": {"year": 2024, "month": 1, "day": 7}},
- {"start": {"year": 2024, "month": 1, "day": 8}, "end": {"year": 2024, "month": 1, "day": 14}},
- {"start": {"year": 2024, "month": 1, "day": 15}, "end": {"year": 2024, "month": 1, "day": 21}},
- {"start": {"year": 2024, "month": 1, "day": 22}, "end": {"year": 2024, "month": 1, "day": 22}},
- ]
- actual_ranges = [
- call_args[1]["query_params"]["dateRange"] for call_args in mock_client_instance.finder.call_args_list
- ]
- assert actual_ranges == expected_ranges
+ @mock.patch("posthog.temporal.data_imports.sources.linkedin_ads.client.RestliClient")
+ def test_get_analytics_shrinks_window_on_capped_response(self, mock_restli_client):
+ """A capped window is discarded and re-fetched at a halved size from the same start;
+ the truncated data is never yielded."""
+ from posthog.temporal.data_imports.sources.linkedin_ads.client import (
+ ANALYTICS_INITIAL_CHUNK_DAYS,
+ ANALYTICS_RESPONSE_CAP,
+ )
+
+ calls = 0
+
+ def respond(*args, **kwargs):
+ # Only the very first (widest) window caps; every narrower window thereafter fits.
+ nonlocal calls
+ response = mock.MagicMock()
+ response.status_code = 200
+ if calls == 0:
+ response.elements = [{"impressions": i} for i in range(ANALYTICS_RESPONSE_CAP)]
+ else:
+ response.elements = [{"impressions": 1}]
+ calls += 1
+ return response
+
+ mock_client_instance = mock_restli_client.return_value
+ mock_client_instance.finder.side_effect = respond
+
+ client = LinkedinAdsClient(self.access_token)
+ pages = list(
+ client.get_analytics(
+ account_id=self.account_id,
+ pivot=LinkedinAdsPivot.CAMPAIGN,
+ date_start="2024-01-01",
+ date_end="2025-12-31",
+ )
+ )
+
+ # The truncated window is never yielded — every yielded page is the fitting (len-1) response.
+ assert all(page[0] == [{"impressions": 1}] for page in pages)
+ first_window, second_window = (
+ call[1]["query_params"]["dateRange"] for call in mock_client_instance.finder.call_args_list[:2]
+ )
+ # The re-fetch after a cap keeps the same start and halves the span.
+ assert first_window["start"] == second_window["start"] == {"year": 2024, "month": 1, "day": 1}
+ first_end = dt.date(**{k: first_window["end"][k] for k in ("year", "month", "day")})
+ second_end = dt.date(**{k: second_window["end"][k] for k in ("year", "month", "day")})
+ assert (first_end - dt.date(2024, 1, 1)).days + 1 == ANALYTICS_INITIAL_CHUNK_DAYS
+ assert (second_end - dt.date(2024, 1, 1)).days + 1 == ANALYTICS_INITIAL_CHUNK_DAYS // 2
@mock.patch("posthog.temporal.data_imports.sources.linkedin_ads.client.RestliClient")
- def test_get_analytics_logs_warning_when_chunk_hits_response_cap(self, mock_restli_client):
- """A capped chunk yields its partial data and logs a warning."""
+ def test_get_analytics_warns_only_when_single_day_still_caps(self, mock_restli_client):
+ """A single-day window that still caps can't be split further, so it yields its partial
+ data and logs a warning (the only path that surfaces capped data)."""
from posthog.temporal.data_imports.sources.linkedin_ads.client import ANALYTICS_RESPONSE_CAP
capped_response = mock.MagicMock()
@@ -212,6 +257,7 @@ class TestLinkedinAdsClient:
mock_client_instance.finder.return_value = capped_response
client = LinkedinAdsClient(self.access_token)
+ # Single-day range → can't shrink below one day.
# Use structlog's own capture_logs — caplog flattens the event_dict on the
# way through stdlib so substring matches against `record.message` miss.
with capture_logs() as logs:
@@ -220,7 +266,7 @@ class TestLinkedinAdsClient:
account_id=self.account_id,
pivot=LinkedinAdsPivot.CREATIVE,
date_start="2024-01-01",
- date_end="2024-01-05",
+ date_end="2024-01-01",
)
)
@@ -351,8 +397,104 @@ class TestLinkedinAdsClient:
assert mock_client_instance.finder.call_count == 1
+ @mock.patch("tenacity.nap.time.sleep", return_value=None)
+ @mock.patch("posthog.temporal.data_imports.sources.linkedin_ads.client.RestliClient")
+ def test_daily_throttle_429_fails_fast_without_retry(self, mock_restli_client, _mock_sleep):
+ """A DAY-window 429 won't reset until midnight UTC, so it raises immediately rather than
+ burning the remaining daily call budget on retries."""
+ mock_response = mock.MagicMock()
+ mock_response.status_code = 429
+ mock_response.response.text = (
+ "Resource level throttle APPLICATION_AND_MEMBER DAY limit for calls to this resource is reached."
+ )
+
+ mock_client_instance = mock_restli_client.return_value
+ mock_client_instance.finder.return_value = mock_response
+
+ client = LinkedinAdsClient(self.access_token)
+
+ with pytest.raises(LinkedinAdsDailyRateLimitError, match="daily rate limit reached"):
+ client.get_accounts()
+
+ assert mock_client_instance.finder.call_count == 1
+
+ @mock.patch("tenacity.nap.time.sleep", return_value=None)
+ @mock.patch("posthog.temporal.data_imports.sources.linkedin_ads.client.RestliClient")
+ def test_short_window_429_is_retried(self, mock_restli_client, _mock_sleep):
+ """A non-DAY (short-window) 429 stays retryable and eventually succeeds."""
+ throttled = mock.MagicMock()
+ throttled.status_code = 429
+ throttled.response.text = "Resource level throttle APPLICATION_AND_MEMBER MINUTE limit reached."
+ throttled.response.headers = {}
+
+ success = mock.MagicMock()
+ success.status_code = 200
+ success.elements = [{"id": "ok"}]
+
+ mock_client_instance = mock_restli_client.return_value
+ mock_client_instance.finder.side_effect = [throttled, success]
+
+ client = LinkedinAdsClient(self.access_token)
+ result = client.get_accounts()
+
+ assert result == [{"id": "ok"}]
+ assert mock_client_instance.finder.call_count == 2
+
+ @mock.patch("tenacity.nap.time.sleep", return_value=None)
+ @mock.patch("posthog.temporal.data_imports.sources.linkedin_ads.client.RestliClient")
+ def test_retry_after_header_is_honoured_and_capped(self, mock_restli_client, mock_tenacity_sleep):
+ """A Retry-After header drives the wait (capped at MAX_RETRY_AFTER_SECONDS) instead of the
+ default exponential backoff."""
+ from posthog.temporal.data_imports.sources.linkedin_ads.client import MAX_RETRY_AFTER_SECONDS
+
+ throttled = mock.MagicMock()
+ throttled.status_code = 429
+ throttled.response.text = "Resource level throttle MINUTE limit reached."
+ throttled.response.headers = {"Retry-After": "9999"}
+
+ success = mock.MagicMock()
+ success.status_code = 200
+ success.elements = [{"id": "ok"}]
+
+ mock_client_instance = mock_restli_client.return_value
+ mock_client_instance.finder.side_effect = [throttled, success]
+
+ client = LinkedinAdsClient(self.access_token)
+ result = client.get_accounts()
+
+ assert result == [{"id": "ok"}]
+ # tenacity sleeps for the wait our strategy returns — the oversized Retry-After is clamped.
+ mock_tenacity_sleep.assert_called_once_with(MAX_RETRY_AFTER_SECONDS)
+
+ @pytest.mark.parametrize("bad_retry_after", ["-1", "-30", "not-a-number"])
+ @mock.patch("tenacity.nap.time.sleep", return_value=None)
+ @mock.patch("posthog.temporal.data_imports.sources.linkedin_ads.client.RestliClient")
+ def test_invalid_retry_after_falls_back_to_backoff(self, mock_restli_client, mock_sleep, bad_retry_after):
+ """A negative or non-numeric Retry-After is ignored (never produces a negative sleep that
+ would crash tenacity) — we fall back to exponential backoff, which sleeps a positive time."""
+ throttled = mock.MagicMock()
+ throttled.status_code = 429
+ throttled.response.text = "Resource level throttle MINUTE limit reached."
+ throttled.response.headers = {"Retry-After": bad_retry_after}
+
+ success = mock.MagicMock()
+ success.status_code = 200
+ success.elements = [{"id": "ok"}]
+
+ mock_client_instance = mock_restli_client.return_value
+ mock_client_instance.finder.side_effect = [throttled, success]
+
+ client = LinkedinAdsClient(self.access_token)
+ result = client.get_accounts()
+
+ assert result == [{"id": "ok"}]
+ # Whatever wait we chose, it must be non-negative (tenacity would raise otherwise).
+ assert mock_sleep.call_count == 1
+ assert mock_sleep.call_args[0][0] >= 0
+
def test_retryable_error_is_exported(self):
assert issubclass(LinkedinAdsRetryableError, Exception)
+ assert issubclass(LinkedinAdsDailyRateLimitError, Exception)
def _status_response(status_code: int, text: str) -> mock.MagicMock: