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(sentry): name the required scopes in credential errors (#65052)

Tom Owersdc40b8a95aaf
diff --git a/posthog/temporal/data_imports/sources/sentry/sentry.py b/posthog/temporal/data_imports/sources/sentry/sentry.py
index 7addbec018d6..1eb76981ebfa 100644
--- a/posthog/temporal/data_imports/sources/sentry/sentry.py
+++ b/posthog/temporal/data_imports/sources/sentry/sentry.py
@@ -26,6 +26,7 @@ from posthog.temporal.data_imports.sources.common.resumable import ResumableSour
from posthog.temporal.data_imports.sources.sentry.settings import (
ALLOWED_SENTRY_API_BASE_URLS,
DEFAULT_SENTRY_API_BASE_URL,
+ REQUIRED_SENTRY_SCOPES,
SENTRY_ENDPOINTS,
SentryEndpointConfig,
)
@@ -487,7 +488,12 @@ def validate_credentials(
if response.status_code == 401:
return False, "Invalid Sentry auth token"
if response.status_code == 403:
- return False, "Sentry token is missing required scopes"
+ return (
+ False,
+ "Sentry token is missing required scopes. Create a token with these scopes and reconnect: "
+ + ", ".join(REQUIRED_SENTRY_SCOPES)
+ + ".",
+ )
if response.status_code == 404:
return False, f"Sentry organization '{organization_slug}' not found"
diff --git a/posthog/temporal/data_imports/sources/sentry/settings.py b/posthog/temporal/data_imports/sources/sentry/settings.py
index 16052ab86302..5d1bbbfe46d0 100644
--- a/posthog/temporal/data_imports/sources/sentry/settings.py
+++ b/posthog/temporal/data_imports/sources/sentry/settings.py
@@ -12,6 +12,18 @@ ALLOWED_SENTRY_API_BASE_URLS = (
"https://de.sentry.io",
)
+# Scopes a Sentry auth token needs to sync every dataset. Single source of truth for
+# the setup caption and the "missing scopes" credential errors, so the list we tell
+# users to grant can't drift from the list we check against.
+REQUIRED_SENTRY_SCOPES = (
+ "alerts:read",
+ "event:read",
+ "member:read",
+ "org:read",
+ "project:read",
+ "team:read",
+)
+
LAST_SEEN_INCREMENTAL: IncrementalField = {
"label": "lastSeen",
"type": IncrementalFieldType.DateTime,
diff --git a/posthog/temporal/data_imports/sources/sentry/source.py b/posthog/temporal/data_imports/sources/sentry/source.py
index 5f0c56b91386..1c33be9f6568 100644
--- a/posthog/temporal/data_imports/sources/sentry/source.py
+++ b/posthog/temporal/data_imports/sources/sentry/source.py
@@ -27,6 +27,7 @@ from posthog.temporal.data_imports.sources.sentry.settings import (
DEFAULT_SENTRY_API_BASE_URL,
ENDPOINTS,
INCREMENTAL_FIELDS,
+ REQUIRED_SENTRY_SCOPES,
)
from products.data_warehouse.backend.types import ExternalDataSourceType
@@ -45,16 +46,12 @@ class SentrySource(ResumableSource[SentrySourceConfig, SentryResumeConfig]):
category=DataWarehouseSourceCategory.ENGINEERING___MONITORING,
label="Sentry",
iconPath="/static/services/sentry.png",
- caption="""Enter a Sentry auth token and your organization slug to sync Sentry organization, project, issue, and monitor datasets.
-
-Create a token in Sentry and make sure it includes the scopes below if you want to sync all datasets:
-- `alerts:read`
-- `event:read`
-- `member:read`
-- `org:read`
-- `project:read`
-- `team:read`
-""",
+ caption=(
+ "Enter a Sentry auth token and your organization slug to sync Sentry organization, "
+ "project, issue, and monitor datasets.\n\n"
+ "Create a token in Sentry and make sure it includes the scopes below if you want to "
+ "sync all datasets:\n" + "\n".join(f"- `{scope}`" for scope in REQUIRED_SENTRY_SCOPES) + "\n"
+ ),
docsUrl="https://posthog.com/docs/cdp/sources/sentry",
fields=cast(
list[FieldType],
@@ -96,7 +93,9 @@ Create a token in Sentry and make sure it includes the scopes below if you want
def get_non_retryable_errors(self) -> dict[str, str | None]:
return {
"401 Client Error": "Invalid Sentry auth token. Please update your token and reconnect.",
- "403 Client Error": "Sentry token is missing required scopes. Please make sure it includes all scopes required for your schemas.",
+ "403 Client Error": "Sentry token is missing required scopes. Make sure it includes the scopes required for your schemas — the full set is: "
+ + ", ".join(REQUIRED_SENTRY_SCOPES)
+ + ".",
"404 Client Error": "Sentry organization not found. Verify your organization slug.",
}
diff --git a/posthog/temporal/data_imports/sources/sentry/tests/test_sentry.py b/posthog/temporal/data_imports/sources/sentry/tests/test_sentry.py
index 16fd4d4e8b2b..84ce73874a88 100644
--- a/posthog/temporal/data_imports/sources/sentry/tests/test_sentry.py
+++ b/posthog/temporal/data_imports/sources/sentry/tests/test_sentry.py
@@ -20,6 +20,7 @@ from posthog.temporal.data_imports.sources.sentry.sentry import (
sentry_source,
validate_credentials,
)
+from posthog.temporal.data_imports.sources.sentry.settings import REQUIRED_SENTRY_SCOPES
from posthog.temporal.data_imports.sources.sentry.source import SentrySource
@@ -242,6 +243,18 @@ class TestSentryTransport:
"API base URL must be one of https://sentry.io, https://us.sentry.io, or https://de.sentry.io.",
)
+ @patch("posthog.temporal.data_imports.sources.sentry.sentry.make_tracked_session")
+ def test_validate_credentials_403_names_required_scopes(self, mock_session) -> None:
+ mock_session.return_value.get.return_value = _response(None, status_code=403)
+
+ valid, error = validate_credentials(auth_token="token", organization_slug="acme")
+
+ assert not valid
+ assert error is not None
+ assert error.startswith("Sentry token is missing required scopes")
+ for scope in REQUIRED_SENTRY_SCOPES:
+ assert scope in error
+
def test_sentry_source_rejects_unknown_api_base_url_at_runtime(self) -> None:
with pytest.raises(
ValueError,