: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): revive a cut-over alert with its source, and admit only on the parallel run (#118059)
Jimmy Zhuf84d1cbbcdf1
diff --git a/products/alerts_platform/backend/logic/platform_lifecycle.py b/products/alerts_platform/backend/logic/platform_lifecycle.pyindex 79cb05906441..06c189cf1bc8 100644--- a/products/alerts_platform/backend/logic/platform_lifecycle.py+++ b/products/alerts_platform/backend/logic/platform_lifecycle.py@@ -634,23 +634,23 @@ def upsert_configuration(upsert: PlatformAlertUpsert, *, revive: bool = False) -def upsert_if_running(upsert: PlatformAlertUpsert, *, revive: bool = False) -> None:"""Copies one configuration in when its team already runs that source here.- A team runs a source here while any of its copies of that source is enabled. A team the backfill- left out of its sample, or one `disable_configurations` switched off, gets nothing, and neither- does a copy an operator switched off by name, so a write in the source never widens a sample or- undoes an operator's switch-off.+ A copy the platform owns is the production alert, so it always follows its source, including+ back on after the source turns it off. Otherwise a team runs a source here while any of its+ parallel-run copies of that source is enabled. A team the backfill left out of its sample, or one+ `disable_configurations` switched off, gets nothing, and neither does a copy an operator switched+ off by name, so a write in the source never widens a sample or undoes an operator's switch-off."""with transaction.atomic():_hold_admission(upsert.source_kind.value, exclusive=False)- running = (- PlatformAlertConfiguration.objects.for_team(upsert.team_id)- .filter(source_kind=upsert.source_kind.value, enabled=True)- .exists()- )- switched_off = (- PlatformAlertConfiguration.objects.for_team(upsert.team_id)- .filter(legacy_configuration_id=upsert.legacy_configuration_id, switched_off_at__isnull=False)- .exists()+ team_copies = PlatformAlertConfiguration.objects.for_team(upsert.team_id).filter(+ source_kind=upsert.source_kind.value)+ existing = team_copies.filter(legacy_configuration_id=upsert.legacy_configuration_id).first()+ if existing is not None and existing.owner == PlatformAlertOwner.PLATFORM:+ upsert_configuration(upsert, revive=revive)+ return+ running = team_copies.filter(owner=PlatformAlertOwner.SOURCE, enabled=True).exists()+ switched_off = existing is not None and existing.switched_off_at is not Noneif running and not switched_off:upsert_configuration(upsert, revive=revive)diff --git a/products/alerts_platform/backend/tests/test_platform_lifecycle.py b/products/alerts_platform/backend/tests/test_platform_lifecycle.pyindex 5478513525d7..f4158eab5d9e 100644--- a/products/alerts_platform/backend/tests/test_platform_lifecycle.py+++ b/products/alerts_platform/backend/tests/test_platform_lifecycle.py@@ -1,3 +1,4 @@+import dataclassesfrom datetime import UTC, datetime, timedeltafrom typing import Anyfrom uuid import UUID, uuid4@@ -20,6 +21,7 @@ from products.alerts_platform.backend.facade.api import (slot_of,take_ownership,upsert_configuration,+ upsert_if_running,)from products.alerts_platform.backend.facade.contracts import (MAX_INSTANCES_CEILING,@@ -549,23 +551,58 @@ class TestPlatformAlertLifecycle(ClickhouseTestMixin, APIBaseTest):assert len(due) == expected_slotsdef _copy(self, legacy_id: UUID, *, revive: bool = False, **overrides: Any) -> None:- fields: dict[str, Any] = {- "legacy_configuration_id": legacy_id,- "team_id": self.team.id,- "name": "Copied alert",- "enabled": True,- "source_kind": SourceKind.LOGS,- "source_config": {"condition": {"threshold_count": 1, "threshold_operator": "above", "window_minutes": 5}},- "check_interval_minutes": 5,- "evaluation_periods": 1,- "datapoints_to_alarm": 1,- "cooldown_minutes": 0,- "schedule_restriction": None,- "next_check_at": self.cutoff - timedelta(minutes=1),- "snooze_until": None,- }- fields.update(overrides)- upsert_configuration(PlatformAlertUpsert(**fields), revive=revive)+ upsert_configuration(dataclasses.replace(self._upsert(legacy_id), **overrides), revive=revive)++ def _upsert(self, legacy_id: UUID) -> PlatformAlertUpsert:+ return PlatformAlertUpsert(+ legacy_configuration_id=legacy_id,+ team_id=self.team.id,+ name="Copied alert",+ enabled=True,+ source_kind=SourceKind.LOGS,+ source_config={"condition": {"threshold_count": 1, "threshold_operator": "above", "window_minutes": 5}},+ check_interval_minutes=5,+ evaluation_periods=1,+ datapoints_to_alarm=1,+ cooldown_minutes=0,+ schedule_restriction=None,+ next_check_at=self.cutoff - timedelta(minutes=1),+ snooze_until=None,+ )++ def _own(self, legacy_id: UUID) -> None:+ with team_scope(self.team.id):+ PlatformAlertConfiguration.objects.filter(legacy_configuration_id=legacy_id).update(+ owner=PlatformAlertOwner.PLATFORM+ )++ def _copies(self, *legacy_ids: UUID) -> dict[UUID, bool]:+ with team_scope(self.team.id):+ rows = PlatformAlertConfiguration.objects.filter(legacy_configuration_id__in=legacy_ids)+ return {row.legacy_configuration_id: row.enabled for row in rows if row.legacy_configuration_id}++ def test_a_cut_over_alert_comes_back_on_with_its_source(self) -> None:+ cut_over = uuid4()+ self._copy(cut_over)+ self._own(cut_over)+ # The parallel run is over, and the person then turned the alert off.+ disable_configurations(SourceKind.LOGS, team_id=self.team.id)+ self._copy(cut_over, enabled=False)++ upsert_if_running(self._upsert(cut_over), revive=True)++ assert self._copies(cut_over) == {cut_over: True}++ def test_a_cut_over_alert_does_not_reopen_a_switched_off_parallel_run(self) -> None:+ cut_over, parallel, created = uuid4(), uuid4(), uuid4()+ self._copy(cut_over)+ self._own(cut_over)+ self._copy(parallel)+ disable_configurations(SourceKind.LOGS, team_id=self.team.id)++ upsert_if_running(self._upsert(created), revive=True)++ assert self._copies(cut_over, parallel, created) == {cut_over: True, parallel: False}@parameterized.expand([