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.

9.0 KB
Raw Blame
import dataclasses
from collections.abc import Iterator
from typing import Any, Optional
from urllib.parse import 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.rollbar.settings import ROLLBAR_ENDPOINTS
from products.warehouse_sources.sources.sdk import (
    ResumableSourceManager,
    SourceResponse,
    make_tracked_session,
    retry_with_deadline,
)

ROLLBAR_BASE_URL = "https://api.rollbar.com/api/1"
# Instances support a keyset `limit` of up to 5000; keep it moderate so pages
# stay well under the pipeline's buffering thresholds.
KEYSET_PAGE_SIZE = 1000
REQUEST_TIMEOUT_SECONDS = 60
# Rate limits are per-token and user-configurable; 429 on breach.
MAX_RETRY_ATTEMPTS = 5


class RollbarRetryableError(Exception):
    pass


@dataclasses.dataclass(frozen=False)
class RollbarResumeConfig:
    # Page-paginated endpoints persist the 1-indexed page; the occurrences
    # keyset walk persists the last (lowest) occurrence id seen instead.
    page: Optional[int] = None
    last_id: Optional[int] = None


def _get_session(access_token: str) -> requests.Session:
    return make_tracked_session(headers={"X-Rollbar-Access-Token": access_token}, redact_values=(access_token,))


def _to_int(value: Any) -> Optional[int]:
    if value is None or isinstance(value, bool):
        return None
    try:
        return int(value)
    except (TypeError, ValueError):
        return None


def _extract_items(body: Any, data_key: Optional[str]) -> list[dict[str, Any]]:
    result = body.get("result") if isinstance(body, dict) else None
    if isinstance(result, list):
        return result
    if isinstance(result, dict) and data_key is not None:
        items = result.get(data_key)
        return items if isinstance(items, list) else []
    return []


def _extract_count_rows(body: Any) -> list[dict[str, Any]]:
    """Turn the `[timestamp, count]` pairs of a count report into rows, oldest bucket first."""
    result = body.get("result") if isinstance(body, dict) else None
    rows: list[dict[str, Any]] = []
    for pair in result if isinstance(result, list) else []:
        if not isinstance(pair, list) or len(pair) < 2:
            continue
        timestamp = _to_int(pair[0])
        if timestamp is None:
            continue
        rows.append({"timestamp": timestamp, "count": _to_int(pair[1])})
    return sorted(rows, key=lambda row: row["timestamp"])


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


TOKEN_REJECTED_MESSAGE = (
    "Rollbar rejected your access token. Use a project access token with read scope from your "
    "Rollbar project settings, then reconnect."
)
UNREACHABLE_MESSAGE = "Couldn't reach Rollbar to check your access token. Try again in a few minutes."


def validate_credentials(access_token: str) -> tuple[bool, str | None]:
    """Confirm the project access token is valid with a cheap environments probe."""
    try:
        response = _get_session(access_token).get(
            _build_url("/environments", {}),
            timeout=10,
        )
    except Exception:
        return False, UNREACHABLE_MESSAGE
    if response.status_code == 200:
        return True, None
    # A write-only token (the post_server_item token Rollbar's SDKs use) is refused here just like a bad one.
    if response.status_code in (401, 403):
        return False, TOKEN_REJECTED_MESSAGE
    return False, UNREACHABLE_MESSAGE


def get_rows(
    access_token: str,
    endpoint: str,
    logger: FilteringBoundLogger,
    resumable_source_manager: ResumableSourceManager[RollbarResumeConfig],
    should_use_incremental_field: bool = False,
    db_incremental_field_last_value: Any = None,
) -> Iterator[list[dict[str, Any]]]:
    config = ROLLBAR_ENDPOINTS[endpoint]
    session = _get_session(access_token)

    @retry_with_deadline(
        retry=retry_if_exception_type((RollbarRetryableError, requests.ReadTimeout, requests.ConnectionError)),
        stop=stop_after_attempt(MAX_RETRY_ATTEMPTS),
        wait=wait_exponential_jitter(initial=1, max=60),
        reraise=True,
    )
    def fetch(url: str) -> Any:
        response = session.get(url, timeout=REQUEST_TIMEOUT_SECONDS)

        if response.status_code == 429 or response.status_code >= 500:
            raise RollbarRetryableError(f"Rollbar API error (retryable): status={response.status_code}, url={url}")

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

        return response.json()

    if config.pagination == "time_series":
        rows = _extract_count_rows(fetch(_build_url(config.path, config.params)))
        if rows:
            yield rows
        return

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

    if config.pagination == "keyset":
        # Occurrences are strictly descending by id. Walk from the head (or the
        # resumed keyset position) and stop once rows cross the persisted
        # incremental watermark.
        watermark = _to_int(db_incremental_field_last_value) if should_use_incremental_field else None
        last_id: Optional[int] = resume_config.last_id if resume_config is not None else None
        if last_id is not None:
            logger.debug(f"Rollbar: resuming {endpoint} from lastId {last_id}")

        while True:
            params: dict[str, Any] = {"limit": KEYSET_PAGE_SIZE}
            if last_id is not None:
                params["lastId"] = last_id
            body = fetch(_build_url(config.path, params))
            items = _extract_items(body, config.data_key)

            if watermark is not None:
                items = [item for item in items if (_to_int(item["id"]) or 0) > watermark]

            if items:
                yield items

            if len(items) < KEYSET_PAGE_SIZE:
                # Short page: either the stream is exhausted or we crossed the
                # watermark (filtered rows out).
                break

            last_id = min((_to_int(item["id"]) or 0) for item in items)
            # Save state AFTER yielding the page so a crash re-yields the last
            # page (merge dedupes on primary key) rather than skipping it.
            resumable_source_manager.save_state(RollbarResumeConfig(last_id=last_id))
        return

    page = resume_config.page if resume_config is not None and resume_config.page is not None else 1
    if resume_config is not None:
        logger.debug(f"Rollbar: resuming {endpoint} from page {page}")

    is_first_page = True
    while True:
        try:
            body = fetch(_build_url(config.path, {"page": page}))
        except requests.HTTPError as e:
            # Rollbar's page-paginated list endpoints (items, deploys, environments) reject a page
            # past an undocumented depth ceiling with a 422 instead of an empty page. That's
            # deterministic — retrying never clears it — so once this run has already pulled at
            # least one page, we treat the 422 as end-of-data rather than failing the whole sync. A
            # 422 on the first fetch is left to propagate: Rollbar hasn't told us any page works.
            if not is_first_page and e.response is not None and e.response.status_code == 422:
                logger.warning(f"Rollbar: pagination ceiling reached on {endpoint}; stopping this sync early")
                break
            raise
        is_first_page = False
        items = _extract_items(body, config.data_key)

        if not items:
            break

        yield items

        page += 1
        resumable_source_manager.save_state(RollbarResumeConfig(page=page))


def rollbar_source(
    access_token: str,
    endpoint: str,
    logger: FilteringBoundLogger,
    resumable_source_manager: ResumableSourceManager[RollbarResumeConfig],
    should_use_incremental_field: bool = False,
    db_incremental_field_last_value: Optional[Any] = None,
) -> SourceResponse:
    config = ROLLBAR_ENDPOINTS[endpoint]

    return SourceResponse(
        name=endpoint,
        items=lambda: get_rows(
            access_token=access_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_key],
        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,
        # Occurrences arrive newest-first; the pipeline only commits the
        # incremental watermark for desc sources once the run completes.
        sort_mode="desc" if config.pagination == "keyset" else "asc",
    )