: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import time
import dataclasses
from collections.abc import Iterator
from datetime import UTC, date, datetime
from typing import Any, Optional
from urllib.parse import urlencode, urljoin, urlparse
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.honeybadger.settings import HONEYBADGER_ENDPOINTS, HoneybadgerEndpointConfig
from products.warehouse_sources.sources.sdk import (
ResumableSourceManager,
SourceResponse,
make_tracked_session,
retry_with_deadline,
)
HONEYBADGER_BASE_URL = "https://app.honeybadger.io/v2"
_API_ORIGIN = urlparse(HONEYBADGER_BASE_URL)
# Honeybadger caps list page sizes at 25 (default and max).
PAGE_LIMIT = 25
REQUEST_TIMEOUT = 60
# Longest single in-attempt sleep when the hourly quota (360 req/hour) is exhausted. If the
# window resets further out, the retries exhaust and the activity fails; Temporal retries it
# later and the resumable state picks the sync back up where it left off.
MAX_RATE_LIMIT_SLEEP_SECONDS = 60.0
class HoneybadgerRetryableError(Exception):
pass
@dataclasses.dataclass(frozen=True)
class HoneybadgerResumeConfig:
# Next page URL to fetch. None means "start the bookmarked resource at its first page".
next_url: str | None = None
# Stable id bookmarks (not positional indexes) so resources added/removed between a crash
# and the retry can't resume us into the wrong project/fault.
project_id: int | None = None
fault_id: int | None = None
site_id: str | None = None
alarm_id: str | None = None
# Fan-out parent list under a project -> the child row column / resume bookmark holding its id.
_FAN_OUT_PARENT_KEYS = {"faults": "fault_id", "sites": "site_id", "alarms": "alarm_id"}
def _make_session(api_key: str) -> requests.Session:
# Honeybadger authenticates with HTTP Basic auth: personal token as username, blank password.
# Exclude requests from HTTP sample capture: project responses carry a live project token and
# notice responses can contain session cookies and other auth data the name-based sample
# scrubbers don't recognise.
session = make_tracked_session(headers={"Accept": "application/json"}, capture=False)
session.auth = (api_key, "")
return session
def _build_url(base_url: str, params: dict[str, Any]) -> str:
if not params:
return base_url
return f"{base_url}?{urlencode(params)}"
def _to_unix_timestamp(value: Any) -> int:
"""Convert an incremental cursor value to the Unix timestamp Honeybadger's filters expect."""
if isinstance(value, datetime):
aware = value if value.tzinfo is not None else value.replace(tzinfo=UTC)
return int(aware.timestamp())
if isinstance(value, date):
return int(datetime.combine(value, datetime.min.time(), tzinfo=UTC).timestamp())
if isinstance(value, int | float):
return int(value)
return int(datetime.fromisoformat(str(value).replace("Z", "+00:00")).timestamp())
@retry_with_deadline(
retry=retry_if_exception_type(
(
HoneybadgerRetryableError,
requests.ReadTimeout,
requests.ConnectionError,
requests.exceptions.ChunkedEncodingError,
)
),
stop=stop_after_attempt(5),
wait=wait_exponential_jitter(initial=2, max=30),
reraise=True,
)
def _fetch_page(session: requests.Session, url: str, logger: FilteringBoundLogger) -> Any:
# Honeybadger paginates with a root-relative `links.next` (e.g. /v2/projects/1/faults?page=2),
# so resolve against the API base before validating the origin. A foreign absolute URL keeps
# its own netloc through urljoin, so it is still refused below.
resolved_url = urljoin(HONEYBADGER_BASE_URL, url)
# Every fetched URL — including `links.next` from responses and resume URLs replayed from
# Redis — must stay on the Honeybadger API origin: the session carries the user's token as
# Basic auth, so following a foreign URL would hand the credential to that host (and is an
# SSRF primitive).
parsed = urlparse(resolved_url)
if parsed.scheme != _API_ORIGIN.scheme or parsed.netloc != _API_ORIGIN.netloc:
raise ValueError(f"Refusing to fetch non-Honeybadger URL: {url}")
response = session.get(resolved_url, timeout=REQUEST_TIMEOUT)
# Honeybadger signals both auth failure and rate-limit exhaustion with a 403; only the
# rate-limited one carries an exhausted X-RateLimit-Remaining header. Sleep toward the
# window reset (bounded) and retry; a genuine permission 403 falls through to
# raise_for_status so get_non_retryable_errors can stop the sync.
if response.status_code == 403 and response.headers.get("X-RateLimit-Remaining") == "0":
delay = MAX_RATE_LIMIT_SLEEP_SECONDS
reset_header = response.headers.get("X-RateLimit-Reset")
if reset_header:
try:
delay = min(max(float(reset_header) - time.time(), 1.0), MAX_RATE_LIMIT_SLEEP_SECONDS)
except ValueError:
pass
logger.warning(f"Honeybadger: rate limited, sleeping {delay:.0f}s before retrying. url={url}")
time.sleep(delay)
raise HoneybadgerRetryableError(f"Honeybadger API rate limited: url={url}")
if response.status_code == 429 or response.status_code >= 500:
raise HoneybadgerRetryableError(f"Honeybadger API error (retryable): status={response.status_code}, url={url}")
if not response.ok:
logger.error(f"Honeybadger API error: status={response.status_code}, body={response.text}, url={url}")
response.raise_for_status()
return response.json()
def validate_credentials(api_key: str) -> bool:
try:
response = _make_session(api_key).get(f"{HONEYBADGER_BASE_URL}/projects", timeout=10)
return response.status_code == 200
except Exception:
return False
def _parse_page(data: Any, data_selector: str = "results") -> tuple[list[Any], str | None]:
"""Split a response into (results, next_url). Some endpoints return a bare array with no paging."""
if isinstance(data, list):
return data, None
return data.get(data_selector) or [], (data.get("links") or {}).get("next")
def _collect_items(
session: requests.Session,
first_url: str,
logger: FilteringBoundLogger,
resumable_source_manager: ResumableSourceManager[HoneybadgerResumeConfig],
) -> list[dict]:
"""Walk every page of a list endpoint and return the items, following `links.next`.
It collects fan-out parents, not rows, and the caller holds no rows, so each page is a safe point.
"""
items: list[dict] = []
url: str | None = first_url
while url:
results, next_url = _parse_page(_fetch_page(session, url, logger))
items.extend(results)
resumable_source_manager.safe_point()
url = next_url if next_url != url else None
return items
def _collect_ids(
session: requests.Session,
first_url: str,
logger: FilteringBoundLogger,
resumable_source_manager: ResumableSourceManager[HoneybadgerResumeConfig],
) -> list[Any]:
return [item["id"] for item in _collect_items(session, first_url, logger, resumable_source_manager)]
def _build_params(
config: HoneybadgerEndpointConfig,
logger: FilteringBoundLogger,
should_use_incremental_field: bool,
db_incremental_field_last_value: Any,
incremental_field: str | None,
) -> dict[str, Any]:
params: dict[str, Any] = {"limit": PAGE_LIMIT} if config.paginated else {}
params.update(config.query_params)
if not should_use_incremental_field or db_incremental_field_last_value is None:
return params
field_name = incremental_field or config.default_incremental_field
query_param = config.incremental_params.get(field_name) if field_name else None
if query_param is None:
if field_name in config.parent_incremental_params:
return params
# No server-side filter for the chosen cursor — walk the full history and let the
# merge dedupe on the primary key rather than silently filtering on the wrong field.
logger.warning(
f"Honeybadger: no server-side filter for incremental field '{field_name}' on "
f"endpoint '{config.name}', running without a time filter"
)
return params
params[query_param] = _to_unix_timestamp(db_incremental_field_last_value)
return params
def _build_parent_params(
config: HoneybadgerEndpointConfig,
should_use_incremental_field: bool,
db_incremental_field_last_value: Any,
incremental_field: str | None,
) -> dict[str, Any]:
"""Params for the fan-out parent list, bounded by the child's watermark where the parent allows it."""
params: dict[str, Any] = {"limit": PAGE_LIMIT}
if not should_use_incremental_field or db_incremental_field_last_value is None:
return params
field_name = incremental_field or config.default_incremental_field
query_param = config.parent_incremental_params.get(field_name) if field_name else None
if query_param is not None:
params[query_param] = _to_unix_timestamp(db_incremental_field_last_value)
return params
def _time_series_rows(project_id: Any, pairs: list[Any]) -> list[dict]:
return [
{"project_id": project_id, "bucket_start": datetime.fromtimestamp(timestamp, tz=UTC), "count": count}
for timestamp, count in pairs
]
def _iter_pages(
session: requests.Session,
first_url: str,
logger: FilteringBoundLogger,
data_selector: str = "results",
) -> Iterator[tuple[list[dict], str | None]]:
"""Yield (results, next_url) per page. The docs note a `next` link may point at an empty
page, so empty results don't terminate the walk — only a missing `next` link does."""
url: str | None = first_url
while url:
results, next_url = _parse_page(_fetch_page(session, url, logger), data_selector)
if next_url == url:
next_url = None
yield results, next_url
url = next_url
def _get_project_rows(
session: requests.Session,
logger: FilteringBoundLogger,
resumable_source_manager: ResumableSourceManager[HoneybadgerResumeConfig],
resume: HoneybadgerResumeConfig | None,
params: dict[str, Any],
) -> Iterator[list[dict]]:
first_url = (
resume.next_url if resume and resume.next_url else _build_url(f"{HONEYBADGER_BASE_URL}/projects", params)
)
for results, next_url in _iter_pages(session, first_url, logger):
if results:
yield results
# Save AFTER yielding (and only when more pages remain) so a crash re-yields the last
# page rather than skipping it — merge dedupes on the primary key.
if next_url:
resumable_source_manager.save_state(HoneybadgerResumeConfig(next_url=next_url))
resumable_source_manager.safe_point()
def _get_project_child_rows(
session: requests.Session,
logger: FilteringBoundLogger,
resumable_source_manager: ResumableSourceManager[HoneybadgerResumeConfig],
resume: HoneybadgerResumeConfig | None,
config: HoneybadgerEndpointConfig,
params: dict[str, Any],
) -> Iterator[list[dict]]:
"""Fan out over every project and page through the per-project child endpoint."""
project_ids = _collect_ids(
session, _build_url(f"{HONEYBADGER_BASE_URL}/projects", {"limit": PAGE_LIMIT}), logger, resumable_source_manager
)
# Resolve the saved project bookmark to the projects still to process. If the bookmarked
# project no longer exists, start over — merge dedupes re-pulled rows on the primary key.
remaining = project_ids
resume_url: str | None = None
if resume is not None and resume.project_id is not None and resume.project_id in project_ids:
remaining = project_ids[project_ids.index(resume.project_id) :]
resume_url = resume.next_url
for index, project_id in enumerate(remaining):
first_url = resume_url or _build_url(
f"{HONEYBADGER_BASE_URL}{config.path.format(project_id=project_id)}", params
)
resume_url = None # only the resumed-into project uses the saved URL
for results, next_url in _iter_pages(session, first_url, logger, config.data_selector):
if results:
if config.time_series:
yield _time_series_rows(project_id, results)
else:
# The traversal determines the parent, so API rows cannot change their project attribution.
yield [{**item, "project_id": project_id} for item in results]
if next_url:
resumable_source_manager.save_state(
HoneybadgerResumeConfig(next_url=next_url, project_id=project_id)
)
resumable_source_manager.safe_point()
# Advance the bookmark so a crash between projects resumes at the next one.
if index + 1 < len(remaining):
resumable_source_manager.save_state(HoneybadgerResumeConfig(project_id=remaining[index + 1]))
def _get_nested_rows(
session: requests.Session,
logger: FilteringBoundLogger,
resumable_source_manager: ResumableSourceManager[HoneybadgerResumeConfig],
resume: HoneybadgerResumeConfig | None,
config: HoneybadgerEndpointConfig,
fan_out_parent: str,
params: dict[str, Any],
parent_params: dict[str, Any],
) -> Iterator[list[dict]]:
"""Two-level fan-out: projects -> faults, sites, or alarms -> {endpoint}.
On incremental syncs of fault children, the watermark also bounds the fault enumeration
(`occurred_after`): only faults whose last notice is newer than the watermark can have new
rows, which keeps the per-fault request count away from the 360 req/hour quota.
"""
parent_key = _FAN_OUT_PARENT_KEYS[fan_out_parent]
project_ids = _collect_ids(
session, _build_url(f"{HONEYBADGER_BASE_URL}/projects", {"limit": PAGE_LIMIT}), logger, resumable_source_manager
)
remaining_projects = project_ids
resume_parent_id: Any = None
resume_url: str | None = None
if resume is not None and resume.project_id is not None and resume.project_id in project_ids:
remaining_projects = project_ids[project_ids.index(resume.project_id) :]
resume_parent_id = getattr(resume, parent_key)
resume_url = resume.next_url
for project_index, project_id in enumerate(remaining_projects):
parents = _collect_items(
session,
_build_url(f"{HONEYBADGER_BASE_URL}/projects/{project_id}/{fan_out_parent}", parent_params),
logger,
resumable_source_manager,
)
parent_ids = [parent["id"] for parent in parents]
parent_columns = {
parent["id"]: {column: parent.get(field_name) for field_name, column in config.parent_fields.items()}
for parent in parents
}
remaining_parents = parent_ids
if resume_parent_id is not None and resume_parent_id in parent_ids:
remaining_parents = parent_ids[parent_ids.index(resume_parent_id) :]
else:
# No (or unresolvable) parent bookmark: the saved page URL belongs to a parent we're
# not resuming into, so it must not seed another parent's pagination.
resume_url = None
resume_parent_id = None # only the resumed-into project uses the parent bookmark
for parent_index, parent_id in enumerate(remaining_parents):
first_url = resume_url or _build_url(
f"{HONEYBADGER_BASE_URL}{config.path.format(project_id=project_id, **{parent_key: parent_id})}",
params,
)
resume_url = None
for results, next_url in _iter_pages(session, first_url, logger, config.data_selector):
if results:
yield [
{**item, "project_id": project_id, parent_key: parent_id, **parent_columns[parent_id]}
for item in results
]
if next_url:
resumable_source_manager.save_state(
HoneybadgerResumeConfig(next_url=next_url, project_id=project_id, **{parent_key: parent_id})
)
resumable_source_manager.safe_point()
if parent_index + 1 < len(remaining_parents):
resumable_source_manager.save_state(
HoneybadgerResumeConfig(project_id=project_id, **{parent_key: remaining_parents[parent_index + 1]})
)
if project_index + 1 < len(remaining_projects):
resumable_source_manager.save_state(
HoneybadgerResumeConfig(project_id=remaining_projects[project_index + 1])
)
def get_rows(
api_key: str,
endpoint: str,
logger: FilteringBoundLogger,
resumable_source_manager: ResumableSourceManager[HoneybadgerResumeConfig],
should_use_incremental_field: bool = False,
db_incremental_field_last_value: Any = None,
incremental_field: str | None = None,
) -> Iterator[list[dict]]:
config = HONEYBADGER_ENDPOINTS[endpoint]
# One session reused across every page (and every fanned-out project/fault) so urllib3
# keeps the connection alive instead of re-handshaking per request.
session = _make_session(api_key)
params = _build_params(
config, logger, should_use_incremental_field, db_incremental_field_last_value, incremental_field
)
resume = resumable_source_manager.load_state() if resumable_source_manager.can_resume() else None
if config.fan_out_parent is not None:
parent_params = _build_parent_params(
config, should_use_incremental_field, db_incremental_field_last_value, incremental_field
)
yield from _get_nested_rows(
session, logger, resumable_source_manager, resume, config, config.fan_out_parent, params, parent_params
)
elif "{project_id}" in config.path:
yield from _get_project_child_rows(session, logger, resumable_source_manager, resume, config, params)
else:
yield from _get_project_rows(session, logger, resumable_source_manager, resume, params)
def honeybadger_source(
api_key: str,
endpoint: str,
logger: FilteringBoundLogger,
resumable_source_manager: ResumableSourceManager[HoneybadgerResumeConfig],
should_use_incremental_field: bool = False,
db_incremental_field_last_value: Optional[Any] = None,
incremental_field: str | None = None,
) -> SourceResponse:
config = HONEYBADGER_ENDPOINTS[endpoint]
return SourceResponse(
name=endpoint,
items=lambda: get_rows(
api_key=api_key,
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,
incremental_field=incremental_field,
),
primary_keys=config.primary_keys,
# Honeybadger lists return newest-first (deploys/notices document it; faults default to
# creation-time order with no ascending option). desc also fits the fan-out: the
# incremental watermark is only finalized once the whole run completes, so a partial
# run can't advance it past projects/faults it never reached.
sort_mode="desc",
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,
)