: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
fix(alerts): keep platform insight copies on when a check lacks data (#130416)
Jimmy Zhu8e6e5b886027
diff --git a/products/alerts/backend/evaluation/contract.py b/products/alerts/backend/evaluation/contract.pyindex 463c3ddc2941..72e14fe382a3 100644--- a/products/alerts/backend/evaluation/contract.py+++ b/products/alerts/backend/evaluation/contract.py@@ -95,6 +95,9 @@ class AlertExtractionError(Exception):Routed to the errored-alert notification path — distinct from "evaluated fine,no data this interval", which is represented as SeriesPoint(value=None).++ Its subclass `AlertDataUnavailableError` keeps the alert enabled, so a caller that disables on+ this error catches the subclass first."""diff --git a/products/alerts/backend/platform_source_cycle.py b/products/alerts/backend/platform_source_cycle.pyindex 9f487a28479c..2069a90aa1b9 100644--- a/products/alerts/backend/platform_source_cycle.py+++ b/products/alerts/backend/platform_source_cycle.py@@ -43,7 +43,7 @@ from posthog.temporal.alerts.retry_policy import alert_timeoutsfrom products.alerts.backend.evaluation import check_alert_for_insightfrom products.alerts.backend.evaluation.comparator import MAX_BREACH_MESSAGES-from products.alerts.backend.evaluation.contract import AlertExtractionError+from products.alerts.backend.evaluation.contract import AlertDataUnavailableError, AlertExtractionErrorfrom products.alerts.backend.evaluation.dispatcher import PerSeriesEvaluation, check_alert_per_seriesfrom products.alerts.backend.facade.contracts import INSIGHT_EVENT_IDS_BY_INCIDENT_ACTIONfrom products.alerts.backend.insight_alert_state_machine import INSIGHT_ALERT_POLICY, insight_snapshot@@ -777,15 +777,16 @@ def _decide()except LLMDetectorPausedError as error:# As production: no retry, and the alert stays enabled so it resumes once access or credits return.- return _verdict(+ return _errored_stays_enabled(check,snapshot,- CheckInput(threshold_breached=False, error_message=str(error)),+ alert,now=now,- skip=SkipReason.QUERY_FAILED,- context=_context(alert),+ message=str(error),check_detail={ERROR_CODE_KEY: LLM_DETECTOR_PAUSED_ERROR_CODE},)+ except AlertDataUnavailableError as error:+ return _errored_stays_enabled(check, snapshot, alert, now=now, message=str(error))except (AlertExtractionError, LLMDetectorMisconfiguredError) as error:return _broken(check, snapshot, alert, now=now, message=str(error))except Exception as error:@@ -863,6 +864,31 @@ def _broken()+def _errored_stays_enabled(+ check: PlatformAlertCheckInput,+ snapshot: AlertSnapshot,+ alert: AlertConfiguration,+ *,+ now: datetime,+ message: str,+ check_detail: dict[str, Any] | None = None,+) -> PlatformAlertOutcome:+ """A check that reached no verdict, but that a later check can, such as one whose history is still short.++ As production, it errors and the copy stays enabled. `AlertDataUnavailableError` subclasses+ `AlertExtractionError`, so its catch must come before the one that sends the copy to `_broken`.+ """+ return _verdict(+ check,+ snapshot,+ CheckInput(threshold_breached=False, error_message=message),+ now=now,+ skip=SkipReason.QUERY_FAILED,+ context=_context(alert),+ check_detail=check_detail,+ )++def _query_failed(check: PlatformAlertCheckInput,snapshot: AlertSnapshot,@@ -941,6 +967,8 @@ def _decide_grouped()except ClickHouseBudgetFull:return None+ except AlertDataUnavailableError as error:+ return _errored_stays_enabled(check, snapshot, alert, now=now, message=str(error))except AlertExtractionError as error:return _broken(check, snapshot, alert, now=now, message=str(error))except Exception as error:diff --git a/products/alerts/backend/test/test_platform_source_cycle.py b/products/alerts/backend/test/test_platform_source_cycle.pyindex 201ff6a75b0a..d54c1f205929 100644--- a/products/alerts/backend/test/test_platform_source_cycle.py+++ b/products/alerts/backend/test/test_platform_source_cycle.py@@ -31,6 +31,7 @@ from posthog.temporal.alerts.retry_policy import ALERT_EVALUATE_RETRY_POLICYfrom products.alerts.backend.evaluation.contract import (CREATOR_LEFT_DISABLE_REASON,+ AlertDataUnavailableError,AlertExtractionError,AlertOwnerAccessError,)@@ -137,6 +138,7 @@ class TestPlatformInsightEvaluation(APIBaseTest):*,truncated: bool = False,breach: str = "above 100",+ error: Exception | None = None,) -> PlatformAlertOutcome:slot = slot_of(configuration.next_check_at, CUTOFF)expires_at = time.time() + 3600@@ -153,7 +155,7 @@ class TestPlatformInsightEvaluation(APIBaseTest):),truncated=truncated,)- with patch(f"{_MODULE}.check_alert_per_series", return_value=evaluated):+ with patch(f"{_MODULE}.check_alert_per_series", return_value=evaluated, side_effect=error):evaluation = evaluate_insight_check(self.team.id, slot, CUTOFF, str(configuration.id), held_until=expires_at, evaluation_id="test")@@ -758,6 +760,25 @@ class TestPlatformInsightEvaluation(APIBaseTest):)assert outcome.failure.source_detail["error_code"] == LLM_DETECTOR_PAUSED_ERROR_CODE+ @parameterized.expand([("single", False), ("breakdown", True)])+ def test_a_check_without_enough_data_errors_and_stays_enabled(self, _name: str, breakdown: bool) -> None:+ error = AlertDataUnavailableError("not enough rows")+ outcome: PlatformAlertOutcome | None+ if breakdown:+ grouping = Grouping(mode=GroupingMode.BY_RESULT_LABELS, keys=("breakdown",))+ outcome = self._evaluate_per_series(+ self._copy(self._alert(), grouping=grouping.to_stored()), {}, error=error+ )+ else:+ outcome, _ = self._evaluate(self._copy(self._alert()), result=error)++ assert outcome is not None and outcome.failure is not None+ assert (outcome.failure.kind, outcome.error_message, outcome.disable) == (+ AlertEventKind.ERRORED,+ "not enough rows",+ False,+ )+@parameterized.expand([("retried", False), ("last_attempt", True)])def test_an_ai_detector_without_a_verdict_is_retried_and_only_its_last_attempt_records_an_error(self, _name: str, final_attempt: bool