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(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.py
index 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)s
AND occurred_at >= %(since)s
AND occurred_at < %(until)s
ORDER BY configuration_id, alert_id, occurred_at, evaluation_key
-LIMIT 1 BY alert_id, evaluation_key
+LIMIT 1 BY configuration_id, alert_id, evaluation_key
LIMIT %(limit)s
"""
diff --git a/products/alerts_platform/backend/facade/contracts.py b/products/alerts_platform/backend/facade/contracts.py
index 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: UUID
alert_id: UUID
evaluation_key: str
@@ -1109,7 +1111,9 @@ class PlatformCheck:
@property
def 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.py
index 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. The
token names the batch by its contents, and the replicated engine drops a second insert that
arrives 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 json
diff --git a/products/alerts_platform/backend/logic/platform_lifecycle.py b/products/alerts_platform/backend/logic/platform_lifecycle.py
index 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 = None
alert.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 True
diff --git a/products/alerts_platform/backend/tests/test_comparison_platform_history.py b/products/alerts_platform/backend/tests/test_comparison_platform_history.py
index 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 SourceKind
from products.alerts_platform.backend.logic.platform_alert_events import PlatformAlertEventRow, insert_events
+from products.alerts_platform.backend.logic.platform_lifecycle import CONFIGURATION_ROW_ALERT_ID
from products.alerts_platform.backend.models import PlatformAlertConfiguration
from 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) == 2
assert 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.py
index 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(
[