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(rollbar): tell rejected tokens apart from an unreachable api at setup (#112385)

Tom Owers2c8286faf87a
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/rollbar/rollbar.py b/products/warehouse_sources/backend/temporal/data_imports/sources/rollbar/rollbar.py
index b7cabecd3b07..71ca920e27f9 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/rollbar/rollbar.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/rollbar/rollbar.py
@@ -62,16 +62,28 @@ def _build_url(path: str, params: dict[str, Any]) -> str:
return f"{ROLLBAR_BASE_URL}{path}?{urlencode(params)}"
-def validate_credentials(access_token: str) -> bool:
+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,
)
- return response.status_code == 200
except Exception:
- return False
+ 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(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/rollbar/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/rollbar/source.py
index d016ee6fa8d8..ca67711e0c85 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/rollbar/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/rollbar/source.py
@@ -112,10 +112,7 @@ You can find or create a project access token in your Rollbar project under Sett
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
- if validate_rollbar_credentials(config.access_token):
- return True, None
-
- return False, "Invalid Rollbar project access token"
+ return validate_rollbar_credentials(config.access_token)
def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[RollbarResumeConfig]:
return ResumableSourceManager[RollbarResumeConfig](inputs, RollbarResumeConfig)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/rollbar/tests/test_rollbar.py b/products/warehouse_sources/backend/temporal/data_imports/sources/rollbar/tests/test_rollbar.py
index 795afc9d775b..8810ec477a78 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/rollbar/tests/test_rollbar.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/rollbar/tests/test_rollbar.py
@@ -6,6 +6,8 @@ from unittest import mock
from products.warehouse_sources.backend.temporal.data_imports.sources.rollbar.rollbar import (
KEYSET_PAGE_SIZE,
+ TOKEN_REJECTED_MESSAGE,
+ UNREACHABLE_MESSAGE,
RollbarResumeConfig,
_extract_items,
_to_int,
@@ -77,10 +79,11 @@ class TestValidateCredentials:
@pytest.mark.parametrize(
"status_code, expected",
[
- (200, True),
- (401, False),
- (403, False),
- (500, False),
+ (200, (True, None)),
+ (401, (False, TOKEN_REJECTED_MESSAGE)),
+ (403, (False, TOKEN_REJECTED_MESSAGE)),
+ (429, (False, UNREACHABLE_MESSAGE)),
+ (500, (False, UNREACHABLE_MESSAGE)),
],
)
@mock.patch("products.warehouse_sources.backend.temporal.data_imports.sources.rollbar.rollbar.make_tracked_session")
@@ -89,12 +92,12 @@ class TestValidateCredentials:
response.status_code = status_code
mock_session.return_value.get.return_value = response
- assert validate_credentials("token") is expected
+ assert validate_credentials("token") == expected
@mock.patch("products.warehouse_sources.backend.temporal.data_imports.sources.rollbar.rollbar.make_tracked_session")
def test_validate_credentials_swallows_exceptions(self, mock_session):
mock_session.return_value.get.side_effect = Exception("boom")
- assert validate_credentials("token") is False
+ assert validate_credentials("token") == (False, UNREACHABLE_MESSAGE)
class TestGetRowsPagePagination:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/rollbar/tests/test_rollbar_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/rollbar/tests/test_rollbar_source.py
index 5998915c7500..2c55f6ecdaa8 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/rollbar/tests/test_rollbar_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/rollbar/tests/test_rollbar_source.py
@@ -61,8 +61,8 @@ class TestRollbarSource:
@pytest.mark.parametrize(
"mock_return, expected_valid, expected_message",
[
- (True, True, None),
- (False, False, "Invalid Rollbar project access token"),
+ ((True, None), True, None),
+ ((False, "Rollbar rejected your access token."), False, "Rollbar rejected your access token."),
],
)
@mock.patch(