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): skip sentry org stats summary when token has no project access (#74993)

Tom Owersece836398c80
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/sentry/sentry.py b/products/warehouse_sources/backend/temporal/data_imports/sources/sentry/sentry.py
index fdb2e42402f6..2c7ee8e8bf7f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/sentry/sentry.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/sentry/sentry.py
@@ -536,6 +536,9 @@ def _iter_issue_tag_values_rows(
_UNAVAILABLE_DATASET_STATUSES = (400, 403, 404)
# Per-project surfaces only ever go missing through permissions or an absent config.
_MISSING_PROJECT_RESOURCE_STATUSES = (403, 404)
+# Sentry's stats-summary endpoint 400s with this detail when the token's user has no
+# project membership in the org, even though the token itself is otherwise valid.
+_NO_PROJECTS_AVAILABLE_DETAIL = "No projects available"
def _iter_rows_tolerating_unavailable(
@@ -672,12 +675,30 @@ def _iter_organization_stats_summary_rows(
organization_slug: str,
) -> Iterator[dict[str, Any]]:
"""Per-project event volume for the retention window, one row per project per category."""
- payload = _fetch_json(
- base_api_url,
- _endpoint_path("organization_stats_summary", organization_slug=organization_slug),
- headers,
- {"field": "sum(quantity)", "statsPeriod": f"{SENTRY_RETENTION_DAYS}d"},
- )
+ try:
+ payload = _fetch_json(
+ base_api_url,
+ _endpoint_path("organization_stats_summary", organization_slug=organization_slug),
+ headers,
+ {"field": "sum(quantity)", "statsPeriod": f"{SENTRY_RETENTION_DAYS}d"},
+ )
+ except HTTPError as exc:
+ response = exc.response
+ if response is not None and response.status_code == 400:
+ try:
+ detail = response.json().get("detail")
+ except JSONDecodeError:
+ detail = None
+ if detail == _NO_PROJECTS_AVAILABLE_DETAIL:
+ # The requesting token's user isn't a member of any project in this
+ # org — Sentry rejects that as a 400 rather than an empty result.
+ # Skip the table rather than failing the whole sync.
+ logger.warning(
+ "sentry_source.organization_stats_summary_no_projects_skipped",
+ organization_slug=organization_slug,
+ )
+ return
+ raise
period_start = payload.get("start")
period_end = payload.get("end")
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/sentry/tests/test_sentry.py b/products/warehouse_sources/backend/temporal/data_imports/sources/sentry/tests/test_sentry.py
index 2fa71efce8b3..3cc95906db24 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/sentry/tests/test_sentry.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/sentry/tests/test_sentry.py
@@ -1379,6 +1379,40 @@ class TestSentryCustomIteratorEndpoints:
assert rows[0]["period_start"] == "2026-01-01T00:00:00Z"
assert rows[0]["period_end"] == "2026-03-01T00:00:00Z"
+ @patch("products.warehouse_sources.backend.temporal.data_imports.sources.sentry.sentry._request_with_retry")
+ def test_stats_summary_skips_when_token_has_no_project_access(self, mock_request) -> None:
+ # The requesting token's user can be a member of the org without being a
+ # member of any project's team — Sentry 400s this specific endpoint rather
+ # than returning an empty result.
+ mock_request.return_value = _response({"detail": "No projects available"}, status_code=400)
+
+ resp = sentry_source(
+ auth_token="token",
+ organization_slug="acme",
+ api_base_url="https://sentry.io",
+ endpoint="organization_stats_summary",
+ team_id=123,
+ job_id="job-id",
+ )
+
+ assert list(cast(Any, resp.items())) == []
+
+ @patch("products.warehouse_sources.backend.temporal.data_imports.sources.sentry.sentry._request_with_retry")
+ def test_stats_summary_propagates_other_400_errors(self, mock_request) -> None:
+ mock_request.return_value = _response({"detail": 'Invalid field: "bogus"'}, status_code=400)
+
+ resp = sentry_source(
+ auth_token="token",
+ organization_slug="acme",
+ api_base_url="https://sentry.io",
+ endpoint="organization_stats_summary",
+ team_id=123,
+ job_id="job-id",
+ )
+
+ with pytest.raises(HTTPError):
+ list(cast(Any, resp.items()))
+
@patch("products.warehouse_sources.backend.temporal.data_imports.sources.sentry.sentry._request_with_retry")
def test_trace_item_attributes_stamps_dataset_and_skips_unavailable_ones(self, mock_request) -> None:
def side_effect(url, headers=None, params=None, timeout=None):