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(microsoft-clarity): answer the credential check before the gateway times out (#120076)

Tom Owers5850ae7ac9ea
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/microsoft_clarity/microsoft_clarity.py b/products/warehouse_sources/backend/temporal/data_imports/sources/microsoft_clarity/microsoft_clarity.py
index 8ecfb4425cab..a6455d7611e2 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/microsoft_clarity/microsoft_clarity.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/microsoft_clarity/microsoft_clarity.py
@@ -3,6 +3,7 @@ from typing import Any, Optional
import requests
import structlog
+from urllib3.util.retry import Retry
from products.warehouse_sources.backend.temporal.data_imports.sources.common.http import make_tracked_session
from products.warehouse_sources.backend.temporal.data_imports.sources.common.typings import SourceResponse
@@ -37,6 +38,9 @@ TOKEN_CHECK_FAILED_ERROR = (
"Microsoft Clarity rejected the connection check. Confirm the token is still listed in Clarity "
"under Settings -> Data Export, then reconnect."
)
+CLARITY_UNREACHABLE_ERROR = (
+ "Couldn't reach Microsoft Clarity to check your token. This is usually temporary, so try again in a few minutes."
+)
def _build_params(num_of_days: str, dimensions: list[str]) -> dict[str, str]:
@@ -46,10 +50,10 @@ def _build_params(num_of_days: str, dimensions: list[str]) -> dict[str, str]:
return params
-def _fetch(token: str, num_of_days: str, dimensions: list[str]) -> requests.Response:
+def _fetch(token: str, num_of_days: str, dimensions: list[str], retry: Retry | None = None) -> requests.Response:
# make_tracked_session already retries 429/5xx with backoff; the daily quota is a hard cap so a
# 429 that survives those retries is genuine and should surface, not be retried again here.
- session = make_tracked_session(redact_values=(token,))
+ session = make_tracked_session(redact_values=(token,), retry=retry)
return session.get(
f"{BASE_URL}{INSIGHTS_PATH}",
params=_build_params(num_of_days, dimensions),
@@ -60,9 +64,11 @@ def _fetch(token: str, num_of_days: str, dimensions: list[str]) -> requests.Resp
def validate_credentials(token: str) -> tuple[bool, str | None]:
try:
- response = _fetch(token, _VALIDATION_NUM_OF_DAYS, [])
+ # The probe runs inline on an API worker, so it takes a single attempt. Default retries
+ # honor the Retry-After of a quota 429 and outlast the gateway timeout.
+ response = _fetch(token, _VALIDATION_NUM_OF_DAYS, [], retry=Retry(total=0))
except requests.RequestException:
- return False, "Could not reach Microsoft Clarity. Please try again."
+ return False, CLARITY_UNREACHABLE_ERROR
if response.ok:
return True, None
@@ -74,6 +80,8 @@ def validate_credentials(token: str) -> tuple[bool, str | None]:
# The project's 10-requests/day quota is already exhausted (possibly by other tooling), but
# that doesn't mean the token itself is invalid — don't block source creation over it.
return True, None
+ if response.status_code >= 500:
+ return False, CLARITY_UNREACHABLE_ERROR
LOGGER.warning("Microsoft Clarity rejected the credential probe", status_code=response.status_code)
return False, TOKEN_CHECK_FAILED_ERROR
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/microsoft_clarity/tests/test_microsoft_clarity.py b/products/warehouse_sources/backend/temporal/data_imports/sources/microsoft_clarity/tests/test_microsoft_clarity.py
index 09c90474ffca..713405782719 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/microsoft_clarity/tests/test_microsoft_clarity.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/microsoft_clarity/tests/test_microsoft_clarity.py
@@ -15,6 +15,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.generated_
MicrosoftClaritySourceConfig,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.microsoft_clarity.microsoft_clarity import (
+ CLARITY_UNREACHABLE_ERROR,
INSIGHTS_PATH,
validate_credentials,
)
@@ -31,7 +32,7 @@ def _driver() -> SourceDriver:
class TestValidateCredentials:
@parameterized.expand(
[
- ("ok", 200, [], True, None),
+ ("ok", 200, [], True, None, {}),
(
"unauthorized",
401,
@@ -39,6 +40,7 @@ class TestValidateCredentials:
False,
"Your Microsoft Clarity API token is invalid or expired. Generate a new token in Clarity "
"under Settings -> Data Export and reconnect.",
+ {},
),
(
"forbidden",
@@ -47,8 +49,10 @@ class TestValidateCredentials:
False,
"Your Microsoft Clarity API token isn't authorized for this project. Generate one in the "
"Clarity project you want to sync, under Settings -> Data Export, then reconnect.",
+ {},
),
- ("quota_exceeded_still_valid", 429, None, True, None),
+ ("quota_exceeded_still_valid", 429, None, True, None, {"Retry-After": "3600"}),
+ ("server_error", 503, None, False, CLARITY_UNREACHABLE_ERROR, {"Retry-After": "120"}),
(
"unexpected_status",
400,
@@ -56,6 +60,7 @@ class TestValidateCredentials:
False,
"Microsoft Clarity rejected the connection check. Confirm the token is still listed in Clarity "
"under Settings -> Data Export, then reconnect.",
+ {},
),
]
)
@@ -66,14 +71,15 @@ class TestValidateCredentials:
json_body: Any,
expected_valid: bool,
expected_error: str | None,
+ headers: dict[str, str],
) -> None:
- answer = ScriptedResponse(status=status, json=json_body)
- script = always(answer) if status == 429 else [answer]
- with scripted_network(script) as network:
+ with scripted_network(always(ScriptedResponse(status=status, json=json_body, headers=headers))) as network:
valid, error = validate_credentials("token")
assert valid is expected_valid
assert error == expected_error
+ assert len(network.requests_log) == 1
+ assert network.elapsed == 0
assert {request.path for request in network.requests_log} == {INSIGHTS_PATH}
assert {request.param("numOfDays") for request in network.requests_log} == {"1"}
assert {request.headers["authorization"] for request in network.requests_log} == {"Bearer token"}
@@ -84,7 +90,7 @@ class TestValidateCredentials:
valid, error = validate_credentials("token")
assert valid is False
- assert error == "Could not reach Microsoft Clarity. Please try again."
+ assert error == CLARITY_UNREACHABLE_ERROR
assert {request.path for request in network.requests_log} == {INSIGHTS_PATH}