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.

20.0 KB
Raw Blame
import dataclasses
from collections.abc import Iterator
from datetime import UTC, date, datetime, timedelta
from typing import Any, Optional
from urllib.parse import quote, urlencode

import requests
from structlog.types import FilteringBoundLogger
from tenacity import retry_if_exception_type, stop_after_attempt, wait_exponential_jitter

from products.warehouse_sources.sources.pylon.settings import (
    ISSUES_MAX_WINDOW_DAYS,
    PYLON_ENDPOINTS,
    PylonEndpointConfig,
)
from products.warehouse_sources.sources.sdk import (
    ResumableSourceManager,
    SourceResponse,
    make_tracked_session,
    retry_with_deadline,
)

PYLON_US_BASE_URL = "https://api.usepylon.com"
PYLON_EU_BASE_URL = "https://api.eu.usepylon.com"
# Pylon issues region-scoped tokens: an EU-residency workspace's token only authenticates against the
# EU host, and the US host rejects it with a 401 naming the correct region.
PYLON_EU_TOKEN_PREFIX = "pylon_api_eu_"


class PylonRetryableError(Exception):
    pass


@dataclasses.dataclass(frozen=False)
class PylonResumeConfig:
    # Next-page cursor within the current page scroll.
    cursor: str | None = None
    # RFC3339 start of the issues window currently being processed (windowed endpoints only).
    window_start: str | None = None
    # The custom-fields object_type currently being fanned out (fan-out endpoints only).
    object_type: str | None = None
    # Child endpoints: the cursor that fetched the current parent page, the parent being read, and
    # whether every child page of that parent has already been yielded.
    parent_cursor: str | None = None
    parent_id: str | None = None
    parent_completed: bool = False
    # RFC3339 cutoff that every child request of an incremental run is bounded by.
    upper_bound: str | None = None


def base_url_for_token(api_token: str) -> str:
    """Pick Pylon's regional API host from the token's prefix, defaulting to US."""
    if api_token.startswith(PYLON_EU_TOKEN_PREFIX):
        return PYLON_EU_BASE_URL
    return PYLON_US_BASE_URL


def _get_headers(api_token: str) -> dict[str, str]:
    return {
        "Authorization": f"Bearer {api_token}",
        "Accept": "application/json",
    }


def _format_rfc3339(dt: datetime) -> str:
    """Pylon's time-window params expect RFC3339. Use the Z suffix rather than +00:00."""
    utc_dt = dt.replace(tzinfo=UTC) if dt.tzinfo is None else dt.astimezone(UTC)
    return utc_dt.strftime("%Y-%m-%dT%H:%M:%SZ")


def _format_rfc3339_precise(dt: datetime) -> str:
    """Like `_format_rfc3339`, but keeps sub-second precision so a watermark doesn't re-match its own row."""
    utc_dt = dt.replace(tzinfo=UTC) if dt.tzinfo is None else dt.astimezone(UTC)
    if utc_dt.microsecond:
        return utc_dt.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
    return utc_dt.strftime("%Y-%m-%dT%H:%M:%SZ")


def _parse_rfc3339(value: str) -> datetime:
    parsed = datetime.fromisoformat(value.replace("Z", "+00:00"))
    return parsed if parsed.tzinfo is not None else parsed.replace(tzinfo=UTC)


def _to_datetime(value: Any) -> datetime:
    """Coerce an incremental cursor value (datetime / date / RFC3339 string) to an aware datetime."""
    if isinstance(value, datetime):
        return value if value.tzinfo is not None else value.replace(tzinfo=UTC)
    if isinstance(value, date):
        return datetime.combine(value, datetime.min.time(), tzinfo=UTC)
    return _parse_rfc3339(str(value))


def _build_url(base_url: str, params: dict[str, Any]) -> str:
    if not params:
        return base_url
    return f"{base_url}?{urlencode(params)}"


@retry_with_deadline(
    retry=retry_if_exception_type((PylonRetryableError, requests.ReadTimeout, requests.ConnectionError)),
    stop=stop_after_attempt(5),
    wait=wait_exponential_jitter(initial=2, max=60),
    reraise=True,
)
def _fetch_page(
    session: requests.Session, url: str, headers: dict[str, str], logger: FilteringBoundLogger
) -> dict[str, Any]:
    response = session.get(url, headers=headers, timeout=60)

    # Pylon enforces tight per-endpoint rate limits (e.g. 10 req/min on /issues); back off and retry on
    # 429 and transient 5xx. Everything else (notably 401/403) raises immediately so the sync surfaces a
    # non-retryable credential error rather than spinning.
    if response.status_code == 429 or response.status_code >= 500:
        raise PylonRetryableError(f"Pylon API error (retryable): status={response.status_code}, url={url}")

    if not response.ok:
        logger.error(f"Pylon API error: status={response.status_code}, body={response.text}, url={url}")
        response.raise_for_status()

    return response.json()


def validate_credentials(api_token: str) -> bool:
    try:
        # redact_values masks the bearer token in logs/sample capture; allow_redirects=False keeps the
        # credentialed request from following an off-origin redirect that could leak the token.
        session = make_tracked_session(redact_values=(api_token,), allow_redirects=False)
        base_url = base_url_for_token(api_token)
        response = session.get(f"{base_url}/me", headers=_get_headers(api_token), timeout=10)
        return response.status_code == 200
    except Exception:
        return False


def _iter_pages(
    session: requests.Session,
    headers: dict[str, str],
    base_url: str,
    path: str,
    params: dict[str, Any],
    logger: FilteringBoundLogger,
    start_cursor: str | None = None,
) -> Iterator[tuple[list[dict[str, Any]], str | None]]:
    """Page through a Pylon list endpoint, yielding (items, next_cursor) per page.

    `next_cursor` is the cursor to fetch the page *after* the one just yielded, or None when the API
    signals no further pages. Endpoints that return no `pagination` object (e.g. macros) yield exactly
    one page with `next_cursor=None`.
    """
    cursor = start_cursor
    seen_cursors: set[str] = set()
    while True:
        page_params = dict(params)
        if cursor:
            page_params["cursor"] = cursor

        data = _fetch_page(session, _build_url(f"{base_url}{path}", page_params), headers, logger)
        items = data.get("data") or []
        pagination = data.get("pagination") or {}
        next_cursor = pagination.get("cursor") if pagination.get("has_next_page") else None

        yield items, next_cursor

        if not next_cursor:
            break
        # A few list endpoints don't document the cursor param; guard against an endpoint that keeps
        # reporting has_next_page with a cursor it then ignores, which would otherwise loop forever on
        # the same page.
        if next_cursor in seen_cursors:
            logger.warning(f"Pylon: cursor did not advance for {path}, stopping pagination")
            break
        seen_cursors.add(next_cursor)
        cursor = next_cursor


def _get_simple_rows(
    session: requests.Session,
    headers: dict[str, str],
    base_url: str,
    config: PylonEndpointConfig,
    logger: FilteringBoundLogger,
    manager: ResumableSourceManager[PylonResumeConfig],
    resume: PylonResumeConfig | None,
) -> Iterator[list[dict[str, Any]]]:
    params: dict[str, Any] = {}
    if config.limit:
        params["limit"] = config.limit

    start_cursor = resume.cursor if resume else None
    for items, next_cursor in _iter_pages(session, headers, base_url, config.path, params, logger, start_cursor):
        if next_cursor:
            manager.save_state(PylonResumeConfig(cursor=next_cursor))
        if items:
            yield items
        else:
            manager.safe_point()


def _get_fan_out_rows(
    session: requests.Session,
    headers: dict[str, str],
    base_url: str,
    config: PylonEndpointConfig,
    logger: FilteringBoundLogger,
    manager: ResumableSourceManager[PylonResumeConfig],
    resume: PylonResumeConfig | None,
) -> Iterator[list[dict[str, Any]]]:
    object_types = config.fan_out_object_types or []

    remaining = object_types
    resume_cursor: str | None = None
    if resume is not None and resume.object_type is not None and resume.object_type in object_types:
        remaining = object_types[object_types.index(resume.object_type) :]
        resume_cursor = resume.cursor

    for index, object_type in enumerate(remaining):
        params: dict[str, Any] = {"object_type": object_type}
        if config.limit:
            params["limit"] = config.limit

        start_cursor = resume_cursor if index == 0 else None
        resume_cursor = None
        next_object_type = remaining[index + 1] if index + 1 < len(remaining) else None

        for items, next_cursor in _iter_pages(session, headers, base_url, config.path, params, logger, start_cursor):
            # Stamp the queried object_type so a single table covers every type and the composite
            # [object_type, id] primary key stays unique even if two types reuse an id.
            stamped = [{**item, "object_type": item.get("object_type") or object_type} for item in items]
            if next_cursor:
                manager.save_state(PylonResumeConfig(cursor=next_cursor, object_type=object_type))
            elif next_object_type is not None:
                # The last page of this type: advance the bookmark so a crash between types resumes correctly.
                manager.save_state(PylonResumeConfig(cursor=None, object_type=next_object_type))
            if stamped:
                yield stamped
            else:
                manager.safe_point()


def _get_windowed_rows(
    session: requests.Session,
    headers: dict[str, str],
    base_url: str,
    config: PylonEndpointConfig,
    logger: FilteringBoundLogger,
    manager: ResumableSourceManager[PylonResumeConfig],
    resume: PylonResumeConfig | None,
    should_use_incremental_field: bool,
    db_incremental_field_last_value: Any,
) -> Iterator[list[dict[str, Any]]]:
    """Walk `/issues` forward one <=30-day window at a time.

    The endpoint requires a start_time/end_time window no longer than 30 days, so a backfill is a
    sequence of ascending windows ending at "now". On an incremental run we start from the saved
    watermark; on a first sync we look back `default_lookback_days`. Windows are processed strictly
    ascending and always extended up to "now", so the resumable state (exact window + cursor) and the
    incremental watermark both pick back up without gaps regardless of intra-window ordering.
    """
    now = datetime.now(UTC)

    if should_use_incremental_field and db_incremental_field_last_value:
        overall_start = _to_datetime(db_incremental_field_last_value)
    else:
        lookback_days = config.default_lookback_days or ISSUES_MAX_WINDOW_DAYS
        overall_start = now - timedelta(days=lookback_days)

    # A future-dated watermark would make every window empty; cap it so the sync stays a no-op rather
    # than building an invalid window.
    overall_start = min(overall_start, now)

    if resume is not None and resume.window_start:
        window_start = _parse_rfc3339(resume.window_start)
        resume_cursor = resume.cursor
    else:
        window_start = overall_start
        resume_cursor = None

    while window_start < now:
        window_end = min(window_start + timedelta(days=ISSUES_MAX_WINDOW_DAYS), now)
        params: dict[str, Any] = {
            "start_time": _format_rfc3339(window_start),
            "end_time": _format_rfc3339(window_end),
        }
        if config.limit:
            params["limit"] = config.limit

        start_cursor = resume_cursor
        resume_cursor = None

        for items, next_cursor in _iter_pages(session, headers, base_url, config.path, params, logger, start_cursor):
            if next_cursor:
                manager.save_state(PylonResumeConfig(cursor=next_cursor, window_start=params["start_time"]))
            elif window_end < now:
                # The last page of this window: point the bookmark at the next window.
                manager.save_state(PylonResumeConfig(cursor=None, window_start=_format_rfc3339(window_end)))
            if items:
                yield items
            else:
                manager.safe_point()

        window_start = window_end


def _iter_parent_pages(
    session: requests.Session,
    headers: dict[str, str],
    base_url: str,
    config: PylonEndpointConfig,
    logger: FilteringBoundLogger,
    resume: PylonResumeConfig | None,
) -> Iterator[tuple[list[str], PylonResumeConfig]]:
    """Yield (parent ids, position) per parent page, where position locates that page for resume."""
    assert config.parent is not None
    parent = config.parent

    windows: list[dict[str, Any]] = [{}]
    if parent.windowed:
        now = datetime.now(UTC)
        if resume is not None and resume.window_start:
            window_start = _parse_rfc3339(resume.window_start)
        else:
            window_start = now - timedelta(days=config.default_lookback_days or ISSUES_MAX_WINDOW_DAYS)
        windows = []
        while window_start < now:
            window_end = min(window_start + timedelta(days=ISSUES_MAX_WINDOW_DAYS), now)
            windows.append({"start_time": _format_rfc3339(window_start), "end_time": _format_rfc3339(window_end)})
            window_start = window_end

    page_cursor = resume.parent_cursor if resume else None
    for window in windows:
        params = dict(window)
        if parent.limit:
            params["limit"] = parent.limit
        for items, next_cursor in _iter_pages(session, headers, base_url, parent.path, params, logger, page_cursor):
            parent_ids = [str(item["id"]) for item in items if item.get("id")]
            yield parent_ids, PylonResumeConfig(window_start=window.get("start_time"), parent_cursor=page_cursor)
            page_cursor = next_cursor
        page_cursor = None


def _get_child_rows(
    session: requests.Session,
    headers: dict[str, str],
    base_url: str,
    config: PylonEndpointConfig,
    logger: FilteringBoundLogger,
    manager: ResumableSourceManager[PylonResumeConfig],
    resume: PylonResumeConfig | None,
    should_use_incremental_field: bool,
    db_incremental_field_last_value: Any,
) -> Iterator[list[dict[str, Any]]]:
    assert config.parent is not None
    id_field = config.parent.id_field

    params: dict[str, Any] = {}
    if config.limit:
        params["limit"] = config.limit
    if config.incremental_param and should_use_incremental_field and db_incremental_field_last_value:
        params[config.incremental_param] = _format_rfc3339_precise(_to_datetime(db_incremental_field_last_value))
    upper_bound: str | None = None
    if config.incremental_upper_param and should_use_incremental_field:
        # Every parent is read up to the same instant. Otherwise a row added to an already-read parent
        # falls below the newer watermark a later parent sets, and the next run never reads it.
        upper_bound = resume.upper_bound if resume and resume.upper_bound else _format_rfc3339(datetime.now(UTC))
        params[config.incremental_upper_param] = upper_bound

    resume_parent_id = resume.parent_id if resume else None
    for parent_ids, position in _iter_parent_pages(session, headers, base_url, config, logger, resume):
        resume_cursor: str | None = None
        # The saved parent is only looked for on the resumed parent page. If it has since been deleted,
        # the whole page is re-read and merge dedupes on the primary key.
        if resume is not None and resume_parent_id is not None and resume_parent_id in parent_ids:
            index = parent_ids.index(resume_parent_id)
            if resume.parent_completed:
                parent_ids = parent_ids[index + 1 :]
            else:
                parent_ids = parent_ids[index:]
                resume_cursor = resume.cursor
        resume_parent_id = None

        for parent_id in parent_ids:
            path = config.path.format(parent_id=quote(parent_id, safe=""))
            yielded = False
            try:
                for items, next_cursor in _iter_pages(session, headers, base_url, path, params, logger, resume_cursor):
                    manager.save_state(
                        dataclasses.replace(
                            position,
                            parent_id=parent_id,
                            cursor=next_cursor,
                            parent_completed=next_cursor is None,
                            upper_bound=upper_bound,
                        )
                    )
                    if items:
                        yield [{**item, id_field: item.get(id_field) or parent_id} for item in items]
                        yielded = True
            except requests.HTTPError as e:
                # The parent was deleted after the parent list was read.
                if e.response is None or e.response.status_code != 404:
                    raise
                logger.warning(f"Pylon: parent {parent_id} of {config.name} no longer exists, skipping")
                manager.save_state(
                    dataclasses.replace(
                        position, parent_id=parent_id, cursor=None, parent_completed=True, upper_bound=upper_bound
                    )
                )
            resume_cursor = None
            if not yielded:
                manager.safe_point()

        if not parent_ids:
            manager.safe_point()


def get_rows(
    api_token: str,
    endpoint: str,
    logger: FilteringBoundLogger,
    resumable_source_manager: ResumableSourceManager[PylonResumeConfig],
    should_use_incremental_field: bool = False,
    db_incremental_field_last_value: Any = None,
) -> Iterator[list[dict[str, Any]]]:
    config = PYLON_ENDPOINTS[endpoint]
    headers = _get_headers(api_token)
    base_url = base_url_for_token(api_token)
    # redact_values masks the bearer token in logs/sample capture; allow_redirects=False keeps the
    # credentialed request from following an off-origin redirect that could leak the token.
    session = make_tracked_session(redact_values=(api_token,), allow_redirects=False)

    resume = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None

    if config.windowed:
        yield from _get_windowed_rows(
            session,
            headers,
            base_url,
            config,
            logger,
            resumable_source_manager,
            resume,
            should_use_incremental_field,
            db_incremental_field_last_value,
        )
    elif config.parent:
        yield from _get_child_rows(
            session,
            headers,
            base_url,
            config,
            logger,
            resumable_source_manager,
            resume,
            should_use_incremental_field,
            db_incremental_field_last_value,
        )
    elif config.fan_out_object_types:
        yield from _get_fan_out_rows(session, headers, base_url, config, logger, resumable_source_manager, resume)
    else:
        yield from _get_simple_rows(session, headers, base_url, config, logger, resumable_source_manager, resume)


def pylon_source(
    api_token: str,
    endpoint: str,
    logger: FilteringBoundLogger,
    resumable_source_manager: ResumableSourceManager[PylonResumeConfig],
    should_use_incremental_field: bool = False,
    db_incremental_field_last_value: Optional[Any] = None,
) -> SourceResponse:
    config = PYLON_ENDPOINTS[endpoint]

    return SourceResponse(
        name=endpoint,
        items=lambda: get_rows(
            api_token=api_token,
            endpoint=endpoint,
            logger=logger,
            resumable_source_manager=resumable_source_manager,
            should_use_incremental_field=should_use_incremental_field,
            db_incremental_field_last_value=db_incremental_field_last_value,
        ),
        primary_keys=config.primary_keys,
        partition_count=1,
        partition_size=1,
        partition_mode="datetime" if config.partition_key else None,
        partition_format="month" if config.partition_key else None,
        partition_keys=[config.partition_key] if config.partition_key else None,
        # Issues windows are processed strictly ascending; the other top-level endpoints are full-refresh
        # cursor scrolls where sort_mode is not used to checkpoint a watermark. Child endpoints read one
        # series per parent, so their rows are not ordered across the table.
        sort_mode=None if config.parent else "asc",
    )