: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
feat(alerts): let each source admit creates on the platform (#129736)
Jimmy Zhu35c6cd3cf966
diff --git a/products/alerts_platform/backend/facade/api.py b/products/alerts_platform/backend/facade/api.pyindex 9989e6930574..b9fd9555e176 100644--- a/products/alerts_platform/backend/facade/api.py+++ b/products/alerts_platform/backend/facade/api.py@@ -205,6 +205,34 @@ def list_configurations()+def count_configurations(+ *,+ team_id: int,+ source_kind: SourceKind,+ native_only: bool = False,+ enabled_only: bool = False,+ check_interval_minutes: int | None = None,+) -> int:+ """How many alerts of a kind a team has on the platform. For a source's admission.++ A source that keeps rows counts them and adds `native_only`. A copy stands for a row the source+ already counts, and a copy left by a delete from before copies went with their row counts nothing.+ """+ return platform_reads.count_configurations(+ team_id=team_id,+ source_kind=source_kind.value,+ native_only=native_only,+ enabled_only=enabled_only,+ check_interval_minutes=check_interval_minutes,+ )+++def hold_cap_lock(*, team_id: int, source_kind: SourceKind) -> None:+ """The lock the platform holds while a source checks its caps, for a source creating an alert+ outside the platform. Call it inside the create's transaction."""+ platform_writes.hold_cap_lock(team_id, source_kind)++def get_configuration(*, team_id: int, source_kinds: Sequence[str], configuration_id: UUID, lock: bool = False) -> PlatformAlertConfigurationView | None:diff --git a/products/alerts_platform/backend/facade/contracts.py b/products/alerts_platform/backend/facade/contracts.pyindex a62df745fbcf..884d859188e5 100644--- a/products/alerts_platform/backend/facade/contracts.py+++ b/products/alerts_platform/backend/facade/contracts.py@@ -1110,6 +1110,28 @@ class SourceConfigTeamCheck(Protocol):def __call__(self, *, team_id: int, edit: PlatformAlertConfigurationEdit) -> None: ...+class SourceAdmission(Protocol):+ """Decides whether a team may add an alert of the kind, or turn one on. The platform calls it on a+ create, where `current` is None, and on an edit that turns the alert on, and on no other edit.++ `check_interval_minutes` is the alert's stored cadence on an edit, and the kind's fixed cadence on+ a native create. It is None on a create through the source, which sets the cadence itself.++ It runs in the write's transaction, after the platform takes a lock for the team and kind, so two+ concurrent writes cannot both pass a count. It raises `PlatformAlertCreateRefused` to refuse a+ create and `InvalidPlatformAlertEdit` to refuse an edit.+ """++ def __call__(+ self,+ *,+ team_id: int,+ current: PlatformAlertConfigurationEdit | None,+ edit: PlatformAlertConfigurationEdit,+ check_interval_minutes: int | None,+ ) -> None: ...++class NativeAlertRemoveDestinations(Protocol):"""Removes the destinations of an alert that is about to be deleted.@@ -1144,7 +1166,8 @@ class SourceConfigSpec:`model` validates `source_config`. `validate` raises `InvalidPlatformAlertEdit` for a rule thatspans fields, such as one that reads `enabled`. `check_in_team` runs when a create or an edit- sets `source_config`. `mirror` runs in the edit's transaction. Without+ sets `source_config`. `admit` runs on every create and every edit that turns an alert on through the+ platform, and enforces the source's caps and plan entitlements. `mirror` runs in the edit's transaction. Without`create` or `delete`, the API cannot create or delete alerts of the kind, unless the kind is`native`: then the platform creates and deletes them itself, and the source keeps no row."""@@ -1152,6 +1175,7 @@ class SourceConfigSpec:model: type[BaseModel]validate: Callable[[PlatformAlertConfigurationEdit], None]check_in_team: SourceConfigTeamCheck | None = None+ admit: SourceAdmission | None = Nonemirror: SourceEditMirror | None = Nonecreate: SourceCreate | None = Nonedelete: SourceDelete | None = Nonediff --git a/products/alerts_platform/backend/logic/platform_reads.py b/products/alerts_platform/backend/logic/platform_reads.pyindex 972b66ea9554..55600fe5b42c 100644--- a/products/alerts_platform/backend/logic/platform_reads.py+++ b/products/alerts_platform/backend/logic/platform_reads.py@@ -127,6 +127,28 @@ def list_configurations()+def count_configurations(+ *,+ team_id: int,+ source_kind: str,+ native_only: bool = False,+ enabled_only: bool = False,+ check_interval_minutes: int | None = None,+) -> int:+ """How many alerts of a kind a team has on the platform, whoever owns them. With `native_only`, only+ the ones with no source row."""+ configurations = PlatformAlertConfiguration.objects.for_team(team_id, canonical=True).filter(+ source_kind=source_kind+ )+ if native_only:+ configurations = configurations.filter(legacy_configuration_id__isnull=True)+ if enabled_only:+ configurations = configurations.filter(enabled=True)+ if check_interval_minutes is not None:+ configurations = configurations.filter(check_interval_minutes=check_interval_minutes)+ return configurations.count()++def get_configuration(*, team_id: int, source_kinds: Sequence[str], configuration_id: UUID, lock: bool = False) -> PlatformAlertConfigurationView | None:diff --git a/products/alerts_platform/backend/logic/platform_writes.py b/products/alerts_platform/backend/logic/platform_writes.pyindex db6811fe028c..41108326094a 100644--- a/products/alerts_platform/backend/logic/platform_writes.py+++ b/products/alerts_platform/backend/logic/platform_writes.py@@ -10,7 +10,7 @@ from datetime import datetime, timedeltafrom typing import Finalfrom uuid import UUID-from django.db import transaction+from django.db import connection, transactionfrom django.db.models import QuerySetfrom django.utils import timezone as django_timezone@@ -118,6 +118,36 @@ def _validated(return validated+def hold_cap_lock(team_id: int, source_kind: SourceKind) -> None:+ """Serializes a team's cap checks for a kind until the caller's transaction ends. A source that+ also creates alerts outside the platform takes it too, so two creates cannot both pass a count."""+ # In autocommit the lock ends with its own statement, so it would serialize nothing.+ if not connection.in_atomic_block:+ raise RuntimeError("The cap lock must be taken inside a transaction.")+ # A transaction-scoped advisory lock per team and kind, rather than a lock on the Team row, which+ # foreign-key checks from every child write also take.+ with connection.cursor() as cursor:+ cursor.execute(+ "SELECT pg_advisory_xact_lock(hashtextextended(%s, 0))", [f"alerts_platform:{team_id}:{source_kind.value}"]+ )+++def _admit(+ *,+ team_id: int,+ source_kind: SourceKind,+ current: PlatformAlertConfigurationEdit | None,+ edit: PlatformAlertConfigurationEdit,+ check_interval_minutes: int | None,+) -> None:+ """Runs the source's admission for a write, in the caller's transaction."""+ spec = source_configs.spec(source_kind)+ if spec is None or spec.admit is None:+ return+ hold_cap_lock(team_id, source_kind)+ spec.admit(team_id=team_id, current=current, edit=edit, check_interval_minutes=check_interval_minutes)++def update_configuration(*,team_id: int,@@ -150,7 +180,7 @@ def update_configuration(configuration = readable.select_for_update().first()if configuration is None:return None- edit, pending = _apply(configuration, change, now=django_timezone.now())+ edit, pending = _apply(configuration, change, now=django_timezone.now(), admit=True)if write_source is not None:write_source(edit)control_plane.announce_after_commit(pending)@@ -174,16 +204,24 @@ def create_configuration(raise PlatformAlertNotEditable(f"{source_kind} alerts cannot be created through this API.")validated = _validated(source_kind, None, edit, team_id=team_id)if spec.native is not None:- configuration = platform_lifecycle.create_native_configuration(- team_id=team_id,- source_kind=source_kind,- edit=validated,- check_interval_minutes=spec.native.check_interval_minutes,- destinations_owner=PlatformAlertOwner.PLATFORM- if spec.native.platform_destinations- else PlatformAlertOwner.SOURCE,- now=django_timezone.now(),- )+ with transaction.atomic():+ _admit(+ team_id=team_id,+ source_kind=source_kind,+ current=None,+ edit=validated,+ check_interval_minutes=spec.native.check_interval_minutes,+ )+ configuration = platform_lifecycle.create_native_configuration(+ team_id=team_id,+ source_kind=source_kind,+ edit=validated,+ check_interval_minutes=spec.native.check_interval_minutes,+ destinations_owner=PlatformAlertOwner.PLATFORM+ if spec.native.platform_destinations+ else PlatformAlertOwner.SOURCE,+ now=django_timezone.now(),+ )else:assert spec.create is not Noneconfiguration = _create_through_source(team_id, source_kind, spec.create, validated, created_by_id)@@ -203,6 +241,7 @@ def _create_through_source(created_by_id: int | None,) -> PlatformAlertConfiguration:with transaction.atomic():+ _admit(team_id=team_id, source_kind=source_kind, current=None, edit=validated, check_interval_minutes=None)legacy_configuration_id = create(team_id=team_id, edit=validated, created_by_id=created_by_id)configuration = _locked_owned_copy(team_id, legacy_configuration_id)if configuration is None:@@ -467,15 +506,26 @@ def _apply(*,now: datetime,snoozing: bool = False,+ admit: bool = False,) -> tuple[PlatformAlertConfigurationEdit, control_plane.PendingClose | None]:"""Validates and writes an edit onto a configuration locked by the caller, and ends the firings it- makes meaningless."""+ makes meaningless. With `admit`, the source's admission decides first on an edit that turns the+ alert on: an edit the source made on its own row has already passed its own checks."""if configuration.owner != PlatformAlertOwner.PLATFORM:raise PlatformAlertNotEditable(f"This alert is managed by the {configuration.source_kind} product. Edit it there.")+ source_kind = SourceKind(configuration.source_kind)current = edit_of(configuration)- edit = _validated(SourceKind(configuration.source_kind), current, change(current), team_id=configuration.team_id)+ edit = _validated(source_kind, current, change(current), team_id=configuration.team_id)+ if admit and edit.enabled and not current.enabled:+ _admit(+ team_id=configuration.team_id,+ source_kind=source_kind,+ current=current,+ edit=edit,+ check_interval_minutes=configuration.check_interval_minutes,+ )regrouped = not edit.grouping.splits_like(current.grouping)ending = control_plane.ending_reason(current, edit, regrouped=regrouped, snoozing=snoozing)diff --git a/products/alerts_platform/backend/tests/test_native_alerts.py b/products/alerts_platform/backend/tests/test_native_alerts.pyindex b37b6304310f..e8412f36efa4 100644--- a/products/alerts_platform/backend/tests/test_native_alerts.py+++ b/products/alerts_platform/backend/tests/test_native_alerts.py@@ -14,12 +14,14 @@ from posthog.models.activity_logging.activity_log import ActivityLogfrom posthog.models.scoping import team_scopefrom products.alerts_platform.backend.facade import testing as platform_testing+from products.alerts_platform.backend.facade.api import count_configurationsfrom products.alerts_platform.backend.facade.contracts import (Grouping,IncidentAction,InvalidPlatformAlertEdit,NativeAlertLifecycle,PlatformAlertConfigurationEdit,+ PlatformAlertCreateRefused,SourceConfigSpec,SourceKind,)@@ -123,6 +125,41 @@ class TestNativeAlerts(ClickhouseTestMixin, APIBaseTest):assert self.check_in_team.called is checked+ def test_admission_refuses_a_create_past_the_cap_and_sees_an_edit_that_turns_the_alert_on(self) -> None:+ admitted: list[tuple[bool, bool, int | None]] = []++ def admit(+ *,+ team_id: int,+ current: PlatformAlertConfigurationEdit | None,+ edit: PlatformAlertConfigurationEdit,+ check_interval_minutes: int | None,+ ) -> None:+ admitted.append((current is None, edit.enabled, check_interval_minutes))+ if current is None and count_configurations(team_id=team_id, source_kind=SourceKind.LOGS) >= 1:+ raise PlatformAlertCreateRefused("At the cap.")++ with patch(_SOURCE_CONFIGS, return_value=replace(self.spec, admit=admit)):+ configuration_id = self._create()+ with pytest.raises(PlatformAlertCreateRefused):+ self._create()+ with team_scope(self.team.id):+ platform_writes.update_configuration(+ team_id=self.team.id,+ source_kinds=[SourceKind.LOGS.value],+ configuration_id=configuration_id,+ change=lambda current: replace(current, enabled=False),+ )+ platform_writes.update_configuration(+ team_id=self.team.id,+ source_kinds=[SourceKind.LOGS.value],+ configuration_id=configuration_id,+ change=lambda current: replace(current, enabled=True),+ )++ assert admitted == [(True, True, 5), (True, True, 5), (False, True, 5)]+ assert count_configurations(team_id=self.team.id, source_kind=SourceKind.LOGS) == 1+def test_deleting_a_firing_native_alert_resolves_by_its_own_id_before_its_destinations_go(self) -> None:configuration_id = self._create()with team_scope(self.team.id):