: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): hand over broken copies and keep failed checks apart (#118164)
Jimmy Zhu6e6414f99ea0
diff --git a/products/alerts_platform/backend/comparison/platform_history.py b/products/alerts_platform/backend/comparison/platform_history.pyindex b4f48c89a852..7ab8029b4291 100644--- a/products/alerts_platform/backend/comparison/platform_history.py+++ b/products/alerts_platform/backend/comparison/platform_history.py@@ -30,9 +30,10 @@ MAX_CHECKS_PER_WINDOW = 200_000# Derived from the contract, so a field added there cannot be left out of the SELECT._COLUMNS = tuple(f.name for f in fields(PlatformCheck) if f.name not in ("team_id", "legacy_configuration_id"))-# `LIMIT 1 BY` deduplicates on the pair the writer names, because the insert token only covers a-# retry of the same batch and the engine remembers a bounded window of tokens. The ORDER BY is the-# table's sort key with `team_id` fixed, so rows stream in order and arrive grouped by alert.+# `LIMIT 1 BY` deduplicates on the check the writer names, because the insert token only covers a+# retry of the same batch and the engine remembers a bounded window of tokens. The configuration is+# part of it, because every failed check with no instance shares the nil `alert_id`. The ORDER BY is+# the table's sort key with `team_id` fixed, so rows stream in order and arrive grouped by alert._SELECT_SQL = f"""SELECT {", ".join(_COLUMNS)}FROM {PLATFORM_ALERT_EVENTS_TABLE}@@ -41,7 +42,7 @@ WHERE team_id = %(team_id)sAND occurred_at >= %(since)sAND occurred_at < %(until)sORDER BY configuration_id, alert_id, occurred_at, evaluation_key-LIMIT 1 BY alert_id, evaluation_key+LIMIT 1 BY configuration_id, alert_id, evaluation_keyLIMIT %(limit)s"""diff --git a/products/alerts_platform/backend/facade/contracts.py b/products/alerts_platform/backend/facade/contracts.pyindex c522d37a4567..e660ee8ff8f6 100644--- a/products/alerts_platform/backend/facade/contracts.py+++ b/products/alerts_platform/backend/facade/contracts.py@@ -1072,9 +1072,11 @@ class CheckRef:"""Addresses one platform check.`evaluation_key` is minted by the source and names the evaluation the check answered, so the- pair survives a retry that recomputes the same check.+ key survives a retry that recomputes the same check. The configuration tells apart failed+ checks of different configurations, which all carry the nil `alert_id`."""+ configuration_id: UUIDalert_id: UUIDevaluation_key: str@@ -1109,7 +1111,9 @@ class PlatformCheck:@propertydef ref(self) -> CheckRef:- return CheckRef(alert_id=self.alert_id, evaluation_key=self.evaluation_key)+ return CheckRef(+ configuration_id=self.configuration_id, alert_id=self.alert_id, evaluation_key=self.evaluation_key+ )class SourceCoverage(StrEnum):diff --git a/products/alerts_platform/backend/logic/platform_alert_events.py b/products/alerts_platform/backend/logic/platform_alert_events.pyindex b0309e6c8f81..11bd456e0ae6 100644--- a/products/alerts_platform/backend/logic/platform_alert_events.py+++ b/products/alerts_platform/backend/logic/platform_alert_events.py@@ -7,8 +7,8 @@ keeping.ClickHouse has no unique constraint, so the insert carries a deduplication token instead. Thetoken names the batch by its contents, and the replicated engine drops a second insert thatarrives under a token it has already seen. A reader still deduplicates on-`(alert_id, evaluation_key)`, because the token only covers a retry of the same batch and the-engine only remembers a bounded window of them.+`(configuration_id, alert_id, evaluation_key)`, because the token only covers a retry of the same+batch and the engine only remembers a bounded window of them."""import jsondiff --git a/products/alerts_platform/backend/logic/platform_lifecycle.py b/products/alerts_platform/backend/logic/platform_lifecycle.pyindex dba9072fe778..79cb05906441 100644--- a/products/alerts_platform/backend/logic/platform_lifecycle.py+++ b/products/alerts_platform/backend/logic/platform_lifecycle.py@@ -710,7 +710,9 @@ def take_ownership(handover: PlatformOwnershipHandover) -> bool:)key = _InstanceKey(configuration_id=str(configuration.id), grouping_key="")alert = _alerts_for_write(handover.team_id, [configuration], [key])[key]- if alert.state == PlatformAlertState.FIRING or alert.firing_started_at is not None:+ # A failing check status means the machine already ended the firing the row still holds.+ live_firing = alert.state == PlatformAlertState.FIRING or alert.firing_started_at is not None+ if live_firing and configuration.check_status == PlatformAlertConfiguration.CheckStatus.OK:return False# ERRORED and BROKEN are the configuration's check status, so the instance starts at rest.failing = handover.state in (PlatformAlertState.ERRORED, PlatformAlertState.BROKEN)@@ -728,8 +730,9 @@ def take_ownership(handover: PlatformOwnershipHandover) -> bool:)alert.state = PlatformAlertState.NOT_FIRING if failing else handover.state+ alert.firing_started_at = Nonealert.last_notified_at = handover.last_notified_at- alert.save(update_fields=["state", "last_notified_at"])+ alert.save(update_fields=["state", "firing_started_at", "last_notified_at"])return Truediff --git a/products/alerts_platform/backend/tests/test_comparison_platform_history.py b/products/alerts_platform/backend/tests/test_comparison_platform_history.pyindex 2199044655f1..67ca98cf6113 100644--- a/products/alerts_platform/backend/tests/test_comparison_platform_history.py+++ b/products/alerts_platform/backend/tests/test_comparison_platform_history.py@@ -19,6 +19,7 @@ from products.alerts_platform.backend.comparison.platform_history import ()from products.alerts_platform.backend.facade.contracts import SourceKindfrom products.alerts_platform.backend.logic.platform_alert_events import PlatformAlertEventRow, insert_events+from products.alerts_platform.backend.logic.platform_lifecycle import CONFIGURATION_ROW_ALERT_IDfrom products.alerts_platform.backend.models import PlatformAlertConfigurationfrom products.alerts_platform.backend.models.platform_alert_events_sql import PLATFORM_ALERT_EVENTS_TABLE@@ -124,6 +125,17 @@ class TestReadPlatformChecks(ClickhouseTestMixin, APIBaseTest):assert len(checks) == 2assert len({check.evaluation_key for check in checks}) == 2+ def test_failed_checks_of_two_configurations_in_one_slot_both_read(self) -> None:+ first, second = (self._configuration(legacy_configuration_id=uuid4()) for _ in range(2))+ key = _key("2026-09-30T11:59:00+00:00")+ for configuration in (first, second):+ self._record(configuration, evaluation_key=key, alert_id=CONFIGURATION_ROW_ALERT_ID)++ checks = self._read()++ assert {check.configuration_id for check in checks} == {first.id, second.id}+ assert len({check.ref for check in checks}) == 2+def test_a_source_the_configuration_model_does_not_accept_is_an_error(self) -> None:# Without this an unregistered source reads as one that made no checks, and whoever runs# the first insight comparison concludes the parallel run is quiet.diff --git a/products/alerts_platform/backend/tests/test_platform_lifecycle.py b/products/alerts_platform/backend/tests/test_platform_lifecycle.pyindex b5d3546cdcbf..dc9e07b8d1e1 100644--- a/products/alerts_platform/backend/tests/test_platform_lifecycle.py+++ b/products/alerts_platform/backend/tests/test_platform_lifecycle.py@@ -127,11 +127,21 @@ class TestPlatformAlertLifecycle(ClickhouseTestMixin, APIBaseTest):assert self._alert().state == "not_firing"- def test_a_copy_inside_its_own_firing_is_not_taken_over(self) -> None:+ @parameterized.expand(+ [+ ("a_live_firing_stays_with_its_source", "ok", False, ("source", "firing", True)),+ # The machine ended the firing when the check broke, so the row's firing is stale.+ ("a_broken_copy_is_taken_over", "broken", True, ("platform", "not_firing", False)),+ ("an_errored_copy_is_taken_over", "errored", True, ("platform", "not_firing", False)),+ ]+ )+ def test_a_copy_inside_its_own_firing_is_taken_over_only_once_that_firing_ended(+ self, _name: str, check_status: str, expected_taken: bool, expected: tuple[str, str, bool]+ ) -> None:legacy_id = uuid4()with team_scope(self.team.id):PlatformAlertConfiguration.objects.filter(id=self.configuration.id).update(- legacy_configuration_id=legacy_id+ legacy_configuration_id=legacy_id, check_status=check_status)PlatformAlert.objects.create(team=self.team, configuration=self.configuration, state="firing", firing_started_at=self.cutoff@@ -149,9 +159,10 @@ class TestPlatformAlertLifecycle(ClickhouseTestMixin, APIBaseTest):))- assert not taken+ alert = self._alert()self.configuration.refresh_from_db()- assert (self.configuration.owner, self._alert().state) == ("source", "firing")+ assert taken is expected_taken+ assert (self.configuration.owner, alert.state, alert.firing_started_at is not None) == expected@parameterized.expand([