: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(data-warehouse): keep warehouse alert destinations as platform rows (#125218)
Jimmy Zhu8a70a8a98678
diff --git a/products/alerts/backend/facade/contracts.py b/products/alerts/backend/facade/contracts.pyindex 8ec9d2e436aa..29c355e927e3 100644--- a/products/alerts/backend/facade/contracts.py+++ b/products/alerts/backend/facade/contracts.py@@ -7,10 +7,9 @@ an insight alert means, and the hooks other sources use to reach this product'sfrom __future__ import annotations-from collections.abc import Collectionfrom datetime import datetimefrom enum import StrEnum-from typing import Any, Final, Protocol+from typing import Any, Finalfrom uuid import UUIDfrom posthog.dataclasses import frozen@@ -140,15 +139,6 @@ class AlertInternalEventsNotDelivered(Exception):"""The broker did not take every event, so the caller retries."""-class AlertEmailRecipientResolver(Protocol):- """The email addresses one alert reaches besides its destinations, for a source that registers one.-- The source applies its own access rules, so every address it returns may read what the alert says.- """-- def __call__(self, *, team_id: int, alert_id: str) -> Collection[str]: ...--@frozenclass MovedAlertRuntime:"""Where an alert the platform owns stands, in the insight alert's terms."""diff --git a/products/alerts/backend/facade/destinations.py b/products/alerts/backend/facade/destinations.pyindex e83d4e00b848..52e905fd2129 100644--- a/products/alerts/backend/facade/destinations.py+++ b/products/alerts/backend/facade/destinations.py@@ -29,7 +29,6 @@ from products.alerts_platform.backend.facade.contracts import ()from ..logic import (- alert_email,delivery_destinations,delivery_path,destination_bucket_report,@@ -40,7 +39,6 @@ from ..logic import (insight_alert_message,)from .contracts import (- AlertEmailRecipientResolver,AlertInternalEvent,DeliveryPathChoice,DestinationBucket,@@ -298,16 +296,6 @@ def list_delivery_destination_groups()-def register_email_recipient_resolver(*, event_ids: Collection[str], resolver: AlertEmailRecipientResolver) -> None:- """Platform delivery of these events also emails the addresses `resolver` returns for the alert.-- Register the events a source maps firing and resolved onto, and not its incident events, or an- incident would send a second email. Call it from the source's `ready()`. Raises when another- resolver already owns one of the events.- """- alert_email.register_recipient_resolver(event_ids=event_ids, resolver=resolver)--def classify_alert_destinations(*, team_id: int, alert_id: str, allowed_event_ids: Collection[str]) -> tuple[DestinationEligibility, ...]:diff --git a/products/alerts/backend/logic/alert_email.py b/products/alerts/backend/logic/alert_email.pyindex 87cb7012676c..04328f6ea8a9 100644--- a/products/alerts/backend/logic/alert_email.py+++ b/products/alerts/backend/logic/alert_email.py@@ -2,10 +2,6 @@An insight alert emails its subscribed users. Both senders, the production check and the sharedplatform, read recipients here, so a user who loses access to the insight stops receiving either.--Another source registers a resolver for the event ids it maps firing and resolved onto, and the-platform then emails the addresses that resolver returns for each alert. The source owns the-access rules, because only it knows who may read what its alerts say."""from __future__ import annotations@@ -15,7 +11,6 @@ from typing import Finalfrom uuid import UUIDfrom products.access_control.backend.facade.user_access_control import UserAccessControl-from products.alerts.backend.facade.contracts import AlertEmailRecipientResolverfrom products.alerts.backend.models.alert import AlertConfigurationfrom products.alerts_platform.backend.facade.contracts import (AlertDestinationData,@@ -71,32 +66,3 @@ def email_groups(addresses: Iterable[str]) -> list[AlertDestinationGroup]:return []data: AlertDestinationData = {"type": DestinationType.EMAIL, "email_addresses": unique}return [AlertDestinationGroup(hog_function_ids=(), data=data, fully_enabled=True)]---_recipient_resolvers: dict[str, AlertEmailRecipientResolver] = {}---def register_recipient_resolver(*, event_ids: Collection[str], resolver: AlertEmailRecipientResolver) -> None:- for event_id in event_ids:- if event_id in SUBSCRIBER_EVENT_IDS:- raise RuntimeError(f"{event_id} already emails the subscribers of an insight alert.")- registered = _recipient_resolvers.get(event_id)- # One owner per event. A second registrant would silently replace who the first one emails.- if registered is not None and registered is not resolver:- raise RuntimeError(f"A different email recipient resolver is already registered for {event_id}.")- for event_id in event_ids:- _recipient_resolvers[event_id] = resolver---def registered_email_groups(- *, team_id: int, alert_id: str, allowed_event_ids: Collection[str]-) -> list[AlertDestinationGroup]:- """One email group for the addresses that the resolvers of these events return for the alert."""- resolvers = {- id(resolver): resolver- for event_id in allowed_event_ids- if (resolver := _recipient_resolvers.get(event_id)) is not None- }- return email_groups(- address for resolver in resolvers.values() for address in resolver(team_id=team_id, alert_id=alert_id)- )diff --git a/products/alerts/backend/logic/delivery_destinations.py b/products/alerts/backend/logic/delivery_destinations.pyindex 98cd8ef35efa..8db1a0fa29dd 100644--- a/products/alerts/backend/logic/delivery_destinations.py+++ b/products/alerts/backend/logic/delivery_destinations.py@@ -9,7 +9,6 @@ from products.alerts.backend.logic.alert_email import (insight_alert,insight_email_groups,recipients_with_access,- registered_email_groups,)from products.alerts.backend.logic.alert_in_app import insight_in_app_groupsfrom products.alerts.backend.logic.destinations import list_alert_destination_groups@@ -20,14 +19,13 @@ def list_delivery_destination_groups(*, team_id: int, alert_id: str, allowed_event_ids: Collection[str]) -> list[AlertDestinationGroup]:"""An alert's HogFunction destinations, plus its subscribers' email and in-app notifications- for an insight alert, plus the email recipients another source registered for its events.+ for an insight alert.Separate from `list_alert_destination_groups`, which the alert APIs list and delete through. Asubscriber group there would show up as a destination a person could delete."""groups = [*list_alert_destination_groups(team_id=team_id, alert_id=alert_id, allowed_event_ids=allowed_event_ids),- *registered_email_groups(team_id=team_id, alert_id=alert_id, allowed_event_ids=allowed_event_ids),]event_ids = SUBSCRIBER_EVENT_IDS.intersection(allowed_event_ids)if not event_ids:diff --git a/products/alerts/backend/test/test_alert_email.py b/products/alerts/backend/test/test_alert_email.pyindex ea1d4b563665..06b36f8ebf0c 100644--- a/products/alerts/backend/test/test_alert_email.py+++ b/products/alerts/backend/test/test_alert_email.py@@ -1,20 +1,12 @@-import pytestfrom posthog.test.base import APIBaseTestfrom unittest.mock import patch-from django.test import SimpleTestCase-from parameterized import parameterizedfrom posthog.models.user import Userfrom products.access_control.backend.facade.user_access_control import UserAccessControl-from products.alerts.backend.facade.destinations import (- list_alert_destination_groups,- list_delivery_destination_groups,- register_email_recipient_resolver,-)-from products.alerts.backend.logic import alert_email+from products.alerts.backend.facade.destinations import list_alert_destination_groups, list_delivery_destination_groupsfrom products.alerts.backend.logic.alert_email import INSIGHT_ALERT_ERRORED_EVENT_IDfrom products.alerts.backend.models.alert import AlertConfiguration, AlertSubscriptionfrom products.alerts.backend.test.insight_alerts import create_insight_alert@@ -87,30 +79,3 @@ class TestInsightAlertEmailDestination(APIBaseTest):)assert groups == []---class TestEmailRecipientRegistration(SimpleTestCase):- @staticmethod- def _nobody(*, team_id: int, alert_id: str) -> list[str]:- return []-- @staticmethod- def _somebody(*, team_id: int, alert_id: str) -> list[str]:- return ["[email protected]"]-- @parameterized.expand(- [- ("an_event_another_source_registered", "$registered_firing", "already registered"),- ("an_insight_subscriber_event", LEGACY_INSIGHT_ALERT_EVENT, "subscribers of an insight alert"),- ]- )- def test_a_second_owner_for_an_event_is_refused(self, _name: str, event_id: str, message: str) -> None:- with patch.dict(alert_email._recipient_resolvers, clear=True):- register_email_recipient_resolver(event_ids=["$registered_firing"], resolver=self._nobody)- # Registering the same resolver again, as a second `ready()` does, is not a conflict.- register_email_recipient_resolver(event_ids=["$registered_firing"], resolver=self._nobody)-- with pytest.raises(RuntimeError, match=message):- register_email_recipient_resolver(event_ids=["$unclaimed", event_id], resolver=self._somebody)-- assert alert_email._recipient_resolvers == {"$registered_firing": self._nobody}diff --git a/products/alerts_platform/backend/delivery/evaluation.py b/products/alerts_platform/backend/delivery/evaluation.pyindex 2c92eb6cfe9a..41818a80ab1f 100644--- a/products/alerts_platform/backend/delivery/evaluation.py+++ b/products/alerts_platform/backend/delivery/evaluation.py@@ -18,7 +18,7 @@ from posthog.dataclasses import frozenfrom posthog.models import Teamfrom posthog.utils import absolute_uri-from products.alerts_platform.backend.delivery import delivery_paths, episode_paths, legacy_webhook+from products.alerts_platform.backend.delivery import delivery_paths, episode_paths, legacy_webhook, recipientsfrom products.alerts_platform.backend.delivery.destinations import list_alert_destination_groupsfrom products.alerts_platform.backend.delivery.discord import DiscordTransportfrom products.alerts_platform.backend.delivery.dispatch import deliver@@ -503,7 +503,7 @@ def _subscribed_events(request: AlertDeliveryRequest, transition: AnnouncedTrans@frozenclass _SubscribedDestination:- # The HogFunction that subscribed this destination to the event, which its sends are recorded under.+ # The HogFunction or `AlertDestination` row its sends are recorded under. None for a source's own recipients.id: str | Nonedata: AlertDestinationData# Whether the destination hears the events the source emits for its HogFunctions.@@ -515,6 +515,8 @@ class _PlatformDestinations:"""An alert's enabled `AlertDestination` rows, with the kind each of its event ids announces."""destinations: tuple[StoredAlertDestination, ...]+ # The addresses the source's own settings name, read once per delivery.+ recipients: tuple[str, ...]kind_by_event_id: dict[str, AlertEventKind]incident_event_ids: frozenset[str]@@ -541,6 +543,12 @@ def _platform_destinations(request: AlertDeliveryRequest) -> _PlatformDestinatiodestinations=enabled_alert_destinations(team_id=request.team_id, configuration_id=UUID(request.configuration_id)),+ # Recipients read messages only, so a delivery that only moves an incident skips the lookup.+ recipients=recipients.addresses(+ request.source, team_id=request.team_id, configuration_id=UUID(request.configuration_id)+ )+ if request.sends_messages+ else (),kind_by_event_id={**{event_id: AlertEventKind(kind) for kind, event_id in request.event_ids_by_kind.items()},**incident_kinds,@@ -588,9 +596,14 @@ def _platform_subscribers(event_id: str, platform: _PlatformDestinations) -> lisif kind is None:return []incident = event_id in platform.incident_event_ids- return [+ subscribers = [_SubscribedDestination(id=str(destination.id), data=destination.data, receives_emitted_events=False)for destination in platform.destinationsif (destination.data["type"] in INCIDENT_DESTINATION_TYPES) == incidentand (incident or kind in destination.event_kinds)]+ # The source's recipients read messages. An incident edge is for an incident manager only.+ if platform.recipients and not incident:+ email: AlertDestinationData = {"type": DestinationType.EMAIL, "email_addresses": list(platform.recipients)}+ subscribers.append(_SubscribedDestination(id=None, data=email, receives_emitted_events=False))+ return subscribersdiff --git a/products/alerts_platform/backend/delivery/recipients.py b/products/alerts_platform/backend/delivery/recipients.pynew file mode 100644index 000000000000..9d85c584d4ed--- /dev/null+++ b/products/alerts_platform/backend/delivery/recipients.py@@ -0,0 +1,31 @@+"""Who a source's own settings say to email, beside the destinations a person added.++A warehouse alert can email the project members who get data pipeline error emails, for example.+The platform imports no source, so a source registers its lookup here when Django starts.+"""++from uuid import UUID++from products.alerts_platform.backend.facade.contracts import SourceKind, SourceRecipients++_recipients: dict[SourceKind, SourceRecipients] = {}+++def register(source: SourceKind, recipients: SourceRecipients) -> None:+ # One owner per source. A second registrant would silently replace who the first one emails.+ registered = _recipients.get(source)+ if registered is not None and registered is not recipients:+ raise RuntimeError(f"A different recipient lookup is already registered for {source} alerts.")+ _recipients[source] = recipients+++def addresses(source: SourceKind, *, team_id: int, configuration_id: UUID) -> tuple[str, ...]:+ """The addresses to email about the alert, or none for a source that registered no lookup."""+ recipients = _recipients.get(source)+ if recipients is None:+ return ()+ # Sorted and deduplicated, because delivery tells destinations apart by their data. The same+ # people in another order would read as a second destination.+ return tuple(+ sorted({address for address in recipients(team_id=team_id, configuration_id=configuration_id) if address})+ )diff --git a/products/alerts_platform/backend/facade/contracts.py b/products/alerts_platform/backend/facade/contracts.pyindex 86affd107826..c35ee855e5fe 100644--- a/products/alerts_platform/backend/facade/contracts.py+++ b/products/alerts_platform/backend/facade/contracts.py@@ -1413,6 +1413,15 @@ class SourceDescriber(Protocol):def __call__(self, *, project_id: int, transition: AnnouncedTransition) -> SourceDescription: ...+class SourceRecipients(Protocol):+ """Who a source's own settings say to email about an alert, beside the destinations a person added.++ It runs once per delivery of an alert whose destinations the platform owns.+ """++ def __call__(self, *, team_id: int, configuration_id: UUID) -> list[str]: ...++@frozenclass SlackMessageContent:text: strdiff --git a/products/alerts_platform/backend/facade/delivery.py b/products/alerts_platform/backend/facade/delivery.pyindex 589ee4ab49f2..18b84e72b56e 100644--- a/products/alerts_platform/backend/facade/delivery.py+++ b/products/alerts_platform/backend/facade/delivery.py@@ -10,6 +10,7 @@ from products.alerts_platform.backend.delivery import (describers,destinations,legacy_webhook,+ recipients,)from products.alerts_platform.backend.facade.contracts import (AlertDestinationData,@@ -20,6 +21,7 @@ from products.alerts_platform.backend.facade.contracts import (SourceDescriber,SourceImageResolver,SourceKind,+ SourceRecipients,SourceSlackRenderer,)@@ -31,6 +33,10 @@ def register_destination_resolver(resolver: DestinationResolver) -> None:destinations.register(resolver)+def register_source_recipients(source: SourceKind, lookup: SourceRecipients) -> None:+ recipients.register(source, lookup)++def register_source_describer(source: SourceKind, describer: SourceDescriber) -> None:describers.register(source, describer)diff --git a/products/alerts_platform/backend/tests/test_delivery_evaluation.py b/products/alerts_platform/backend/tests/test_delivery_evaluation.pyindex 047b5fad3258..2a061635e35e 100644--- a/products/alerts_platform/backend/tests/test_delivery_evaluation.py+++ b/products/alerts_platform/backend/tests/test_delivery_evaluation.py@@ -12,7 +12,7 @@ from parameterized import parameterizedfrom posthog.utils import absolute_uri-from products.alerts_platform.backend.delivery import delivery_paths, describers, legacy_webhook+from products.alerts_platform.backend.delivery import delivery_paths, describers, legacy_webhook, recipientsfrom products.alerts_platform.backend.delivery.destinations import list_alert_destination_groupsfrom products.alerts_platform.backend.delivery.evaluation import LIVE_DELIVERY_FLAG, deliver_evaluationfrom products.alerts_platform.backend.delivery.legacy_webhook import LEGACY_WEBHOOK_BODY_FLAG@@ -93,6 +93,13 @@ class PagingTransport(RecordingTransport):return str(target.get("pagerduty_routing_key", ""))+class EmailingTransport(RecordingTransport):+ provider = "email"++ def channel_target(self, target: AlertDestinationData) -> str:+ return ",".join(target.get("email_addresses", []))++class RefusingTransport(RecordingTransport):provider = "webhook"@@ -406,15 +413,22 @@ class TestDeliverEvaluation(APIBaseTest):)hog_function = [_group(cast(AlertDestinationData, {**SLACK, "slack_channel_id": "C-hog"}))]- self._run(- _announcement(_transition(kind)),- {FIRING_EVENT: hog_function, RESOLVED_EVENT: hog_function, OPENED_EVENT: hog_function},- incident_actions={"": action},- owned_by_platform=True,- )+ def project_members(*, team_id: int, configuration_id: UUID) -> list[str]:+ with patch.dict(recipients._recipients, {SourceKind.LOGS: project_members}):+ self._run(+ _announcement(_transition(kind)),+ {FIRING_EVENT: hog_function, RESOLVED_EVENT: hog_function, OPENED_EVENT: hog_function},+ incident_actions={"": action},+ owned_by_platform=True,+ transports={DestinationType.EMAIL: EmailingTransport},+ )++ # The source's recipients read the message once, and never the incident edge.assert [(channel, m.headline) for channel, m in RecordingTransport.sends if channel != "R-1"] == [- ("C-1", headline)+ ("C-1", headline),+ ("[email protected],[email protected]", headline),]assert [m.incident_action for channel, m in RecordingTransport.sends if channel == "R-1"] == [action]@@ -643,6 +657,23 @@ class TestDeliverEvaluation(APIBaseTest):== [])+ def test_a_second_recipient_lookup_for_a_source_is_refused(self) -> None:+ def first(*, team_id: int, configuration_id: UUID) -> list[str]:+ return []++ def second(*, team_id: int, configuration_id: UUID) -> list[str]:+ return ["[email protected]"]++ with patch.dict(recipients._recipients, clear=True):+ recipients.register(SourceKind.LOGS, first)+ # A second `ready()` registers the same lookup again, which is not a conflict.+ recipients.register(SourceKind.LOGS, first)++ with pytest.raises(RuntimeError, match="already registered"):+ recipients.register(SourceKind.LOGS, second)++ assert recipients._recipients == {SourceKind.LOGS: first}+class TestDeliveryPath(APIBaseTest):def setUp(self) -> None:diff --git a/products/warehouse_sources/backend/facade/sync_alerts.py b/products/warehouse_sources/backend/facade/sync_alerts.pyindex 9b4d210445c7..20664eed7f4e 100644--- a/products/warehouse_sources/backend/facade/sync_alerts.py+++ b/products/warehouse_sources/backend/facade/sync_alerts.py@@ -1,9 +1,8 @@"""Warehouse alert destination vocabulary for the warehouse presentation endpoints."""from products.warehouse_sources.backend.sync_alerts.destinations import (- EVENT_IDS,- EVENT_KIND_CONFIG,WAREHOUSE_ALERT_DESTINATION_TYPES,+ WAREHOUSE_DESTINATION_EVENT_KINDS,)-__all__ = ["EVENT_IDS", "EVENT_KIND_CONFIG", "WAREHOUSE_ALERT_DESTINATION_TYPES"]+__all__ = ["WAREHOUSE_DESTINATION_EVENT_KINDS", "WAREHOUSE_ALERT_DESTINATION_TYPES"]diff --git a/products/warehouse_sources/backend/presentation/views/warehouse_alert_destinations.py b/products/warehouse_sources/backend/presentation/views/warehouse_alert_destinations.pyindex 5ee900e1cc45..3b17a4d2b390 100644--- a/products/warehouse_sources/backend/presentation/views/warehouse_alert_destinations.py+++ b/products/warehouse_sources/backend/presentation/views/warehouse_alert_destinations.py@@ -1,11 +1,11 @@"""Notification destinations of a warehouse alert on the alerts platform.The alert itself is created, edited and deleted through the platform API (`platform_alerts`). Its-destinations are HogFunctions this product owns, matched on the alert's configuration id, so they-are managed here.+destinations are `AlertDestination` rows the platform keeps, managed here so the warehouse access+checks apply to them."""-from typing import Any, Literal, cast+from typing import Any, Final, Literal, castfrom uuid import UUIDfrom django.db import models, transaction@@ -27,36 +27,36 @@ from posthog.permissions import PostHogFeatureFlagPermissionfrom products.alerts.backend.facade.destinations import (MAX_DESTINATIONS_PER_ALERT,- build_alert_destination_config,- create_alert_destination_hog_functions,- destination_handles_event_kind,- list_alert_destination_groups,redact_destination_data,- soft_delete_alert_destinations,validate_destination_data,)-from products.alerts_platform.backend.facade.api import get_configuration+from products.alerts_platform.backend.facade.api import (+ create_alert_destinations,+ delete_alert_destinations,+ get_configuration,+ list_alert_destinations,+)from products.alerts_platform.backend.facade.contracts import (AlertDestinationData,AlertDestinationValidationError,+ AlertEventKind,DestinationType,+ NewAlertDestination,PagerDutyRegion,PagerDutySeverity,PlatformAlertConfigurationView,SourceKind,+ StoredAlertDestination,)from products.alerts_platform.backend.facade.enums import PlatformAlertOwnerfrom products.warehouse_sources.backend.facade.sync_alerts import (- EVENT_IDS,- EVENT_KIND_CONFIG,WAREHOUSE_ALERT_DESTINATION_TYPES,+ WAREHOUSE_DESTINATION_EVENT_KINDS,)-MAX_DESTINATION_IDS_PER_DELETE_REQUEST = 100WAREHOUSE_ALERT_KINDS = (SourceKind.WAREHOUSE_SOURCE.value, SourceKind.WAREHOUSE_DESTINATION.value)# The warehouse data an alert names is gated by the same resource as the sources page.WAREHOUSE_RESOURCE: Literal["external_data_source"] = "external_data_source"-_SLACK_CONTEXT_ELEMENTS = ("Project: <{project.url}|{project.name}>",)# Choices classes for the serializers below. Each label is its value, so the OpenAPI enums keep the# names their ENUM_NAME_OVERRIDES entries in posthog/settings/web.py give them. TextChoices supports# this functional API at runtime, but its type stubs only describe member construction.@@ -64,25 +64,50 @@ WarehouseAlertDestinationTypeChoices = cast(Any, models.TextChoices)("WarehouseAlertDestinationTypeChoices",[(member.name, (member.value, member.value)) for member in WAREHOUSE_ALERT_DESTINATION_TYPES],)+WarehouseAlertEventKindChoices = cast(Any, models.TextChoices)(+ "WarehouseAlertEventKindChoices",+ [(member.name, (member.value, member.value)) for member in WAREHOUSE_DESTINATION_EVENT_KINDS],+)PagerDutySeverityChoices = cast(Any, models.TextChoices)("PagerDutySeverityChoices", [(member.name, (member.value, member.value)) for member in PagerDutySeverity])PagerDutyRegionChoices = cast(Any, models.TextChoices)("PagerDutyRegionChoices", [(member.name, (member.value, member.value)) for member in PagerDutyRegion])+_EVENT_KINDS_HELP = (+ "Which transitions send a message. A PagerDuty destination opens an incident when the alert fires and "+ "resolves it when the alert resolves, whatever this lists."+)+++_WEBHOOK_FIELDS: Final = ("webhook_url",)+# The settings each type keeps. The serializer accepts every type's fields and fills the PagerDuty+# defaults for all of them, and a field of another type can hold a credential that its redaction misses.+_FIELDS_BY_TYPE: Final[dict[DestinationType, tuple[str, ...]]] = {+ DestinationType.SLACK: ("slack_workspace_id", "slack_channel_id", "slack_channel_name"),+ DestinationType.WEBHOOK: _WEBHOOK_FIELDS,+ DestinationType.TEAMS: _WEBHOOK_FIELDS,+ DestinationType.DISCORD: _WEBHOOK_FIELDS,+ DestinationType.PAGERDUTY: ("pagerduty_routing_key", "pagerduty_severity", "pagerduty_region"),+}+++def _split(attrs: dict[str, Any]) -> tuple[AlertDestinationData, tuple[AlertEventKind, ...]]:+ """A create request's destination settings, apart from the kinds it asks to hear about."""+ destination_type = DestinationType(attrs["type"])+ data: dict[str, Any] = {key: attrs[key] for key in _FIELDS_BY_TYPE[destination_type] if key in attrs}+ data["type"] = destination_type+ return cast(AlertDestinationData, data), tuple(AlertEventKind(kind) for kind in attrs["event_kinds"])class WarehouseAlertDestinationSerializer(serializers.Serializer):- hog_function_ids = serializers.ListField(- child=serializers.UUIDField(),- help_text="HogFunction IDs that make up this destination, one per alert event kind it handles. "- "Send them all to the delete endpoint to remove the destination.",- )+ id = serializers.UUIDField(help_text="Destination ID. Send it to the delete endpoint to remove the destination.")type = serializers.ChoiceField(choices=WarehouseAlertDestinationTypeChoices.choices, help_text="Notification destination type.")- enabled = serializers.BooleanField(- help_text="Whether every HogFunction of the destination is enabled, so it notifies for every event kind."+ enabled = serializers.BooleanField(help_text="Whether the destination is sent to.")+ event_kinds = serializers.ListField(+ child=serializers.ChoiceField(choices=WarehouseAlertEventKindChoices.choices), help_text=_EVENT_KINDS_HELP)slack_workspace_id = serializers.IntegerField(required=False, help_text="Integration ID of the Slack workspace. Set when type=slack."@@ -114,6 +139,13 @@ class WarehouseAlertCreateDestinationSerializer(serializers.Serializer):type = serializers.ChoiceField(choices=WarehouseAlertDestinationTypeChoices.choices, help_text="Notification destination type.")+ event_kinds = serializers.ListField(+ child=serializers.ChoiceField(choices=WarehouseAlertEventKindChoices.choices),+ required=False,+ min_length=1,+ default=[kind.value for kind in WAREHOUSE_DESTINATION_EVENT_KINDS],+ help_text=f"{_EVENT_KINDS_HELP} Defaults to every kind.",+ )slack_workspace_id = serializers.IntegerField(required=False, help_text="Integration ID for the Slack workspace. Required when type=slack.")@@ -143,8 +175,7 @@ class WarehouseAlertCreateDestinationSerializer(serializers.Serializer):)def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:- data = cast(AlertDestinationData, attrs)- data["type"] = DestinationType(attrs["type"])+ data, _ = _split(attrs)try:validate_destination_data(data, allowed_destination_types=WAREHOUSE_ALERT_DESTINATION_TYPES)except AlertDestinationValidationError as error:@@ -152,21 +183,26 @@ class WarehouseAlertCreateDestinationSerializer(serializers.Serializer):return attrs-class WarehouseAlertDestinationCreatedSerializer(serializers.Serializer):- hog_function_ids = serializers.ListField(- child=serializers.UUIDField(), help_text="HogFunction IDs created for the destination, one per event kind."- )--class WarehouseAlertDeleteDestinationSerializer(serializers.Serializer):- hog_function_ids = serializers.ListField(+ destination_ids = serializers.ListField(child=serializers.UUIDField(),min_length=1,- max_length=MAX_DESTINATION_IDS_PER_DELETE_REQUEST,- help_text="HogFunction IDs of one destination, as the list endpoint returns them. They are deleted together.",+ max_length=MAX_DESTINATIONS_PER_ALERT,+ help_text="IDs of the alert's destinations to delete, as the list endpoint returns them.",)+def _serialized(destination: StoredAlertDestination) -> dict[str, Any]:+ return WarehouseAlertDestinationSerializer(+ {+ "id": destination.id,+ "enabled": destination.enabled,+ "event_kinds": [kind.value for kind in destination.event_kinds],+ **redact_destination_data(destination.data),+ }+ ).data++class WarehouseAlertDestinationViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):"""Destinations of a warehouse alert. The URL's ID is the alert's platform configuration ID."""@@ -201,60 +237,31 @@ class WarehouseAlertDestinationViewSet(TeamAndOrgViewSetMixin, viewsets.GenericV@action(detail=True, methods=["GET"], url_path="destinations", pagination_class=None)def destinations(self, request: Request, pk: str, **kwargs: Any) -> Response:alert = self._alert(pk)- groups = list_alert_destination_groups(- team_id=self.team_id,- alert_id=str(alert.destination_alert_id),- allowed_event_ids=EVENT_IDS[SourceKind(alert.source_kind)],- )return Response(- WarehouseAlertDestinationSerializer(- [- {- "hog_function_ids": list(group.hog_function_ids),- "enabled": group.fully_enabled,- **redact_destination_data(group.data),- }- for group in groups- ],- many=True,- ).data+ [+ _serialized(destination)+ for destination in list_alert_destinations(team_id=self.team_id, configuration_id=alert.id)+ ])@extend_schema(request=WarehouseAlertCreateDestinationSerializer,responses={- 201: WarehouseAlertDestinationCreatedSerializer,+ 201: WarehouseAlertDestinationSerializer,400: OpenApiResponse(description="The destination settings are invalid."),},- description=(- "Add a notification destination to a warehouse alert. One HogFunction is created per alert event kind "- "(firing, resolved). A PagerDuty destination gets the incident opened and incident closed kinds "- "instead, so each incident it opens is resolved."- ),+ description="Add a notification destination to a warehouse alert.",)@destinations.mapping.postdef create_destination(self, request: Request, pk: str, **kwargs: Any) -> Response:alert = self._alert(pk)serializer = WarehouseAlertCreateDestinationSerializer(data=request.data)serializer.is_valid(raise_exception=True)- data = cast(AlertDestinationData, serializer.validated_data)- source_kind = SourceKind(alert.source_kind)- kinds = EVENT_KIND_CONFIG[source_kind]- event_kinds = [kind for kind, spec in kinds.items() if destination_handles_event_kind(data["type"], spec)]- configs = [- build_alert_destination_config(- spec=kinds[kind],- alert_id=str(alert.destination_alert_id),- alert_name=alert.name,- data=data,- slack_context_elements=_SLACK_CONTEXT_ELEMENTS,- )- for kind in event_kinds- ]+ data, event_kinds = _split(serializer.validated_data)try:with transaction.atomic():- # The alert's row stays locked until the destinations commit. A delete that got there- # first leaves no row, and one that comes later waits, then removes these destinations too.+ # The alert's row stays locked until the destination commits, so two adds cannot both+ # pass the limit. A delete that got there first leaves no row.if (get_configuration(team_id=self.team_id, source_kinds=WAREHOUSE_ALERT_KINDS, configuration_id=alert.id, lock=True@@ -263,21 +270,17 @@ class WarehouseAlertDestinationViewSet(TeamAndOrgViewSetMixin, viewsets.GenericV):raise NotFound()# Each destination is another message every time the alert fires, so one alert has only a few.- existing = list_alert_destination_groups(- team_id=self.team_id,- alert_id=str(alert.destination_alert_id),- allowed_event_ids=EVENT_IDS[source_kind],- )- if len(existing) >= MAX_DESTINATIONS_PER_ALERT:+ if (+ len(list_alert_destinations(team_id=self.team_id, configuration_id=alert.id))+ >= MAX_DESTINATIONS_PER_ALERT+ ):raise ValidationError(f"This alert already has {MAX_DESTINATIONS_PER_ALERT} destinations. Remove one to add another.")- hog_function_ids = create_alert_destination_hog_functions(- configs,+ (created,) = create_alert_destinations(team_id=self.team_id,- created_by_id=cast(User, request.user).id,- alert_id=str(alert.destination_alert_id),- allowed_event_ids=EVENT_IDS[source_kind],+ configuration_id=alert.id,+ destinations=[NewAlertDestination(data=data, event_kinds=event_kinds)],)except AlertDestinationValidationError as error:raise as_drf_validation_error(error)@@ -289,44 +292,39 @@ class WarehouseAlertDestinationViewSet(TeamAndOrgViewSetMixin, viewsets.GenericV"alert_id": str(alert.id),"source_kind": alert.source_kind,"type": data["type"],- "event_kinds": event_kinds,+ "event_kinds": [kind.value for kind in event_kinds],},team=self.team,request=request,)- return Response(- WarehouseAlertDestinationCreatedSerializer({"hog_function_ids": list(hog_function_ids)}).data,- status=status.HTTP_201_CREATED,- )+ return Response(_serialized(created), status=status.HTTP_201_CREATED)@extend_schema(request=WarehouseAlertDeleteDestinationSerializer,responses={- 204: OpenApiResponse(description="The destination's HogFunctions are deleted."),- 400: OpenApiResponse(description="An ID does not belong to this alert, or names part of a destination."),+ 204: OpenApiResponse(description="The destinations are deleted."),+ 400: OpenApiResponse(description="An ID does not belong to this alert."),},- description="Delete one notification destination of a warehouse alert, all its HogFunctions together.",+ description="Delete notification destinations of a warehouse alert.",)@action(detail=True, methods=["POST"], url_path="destinations/delete")def delete_destination(self, request: Request, pk: str, **kwargs: Any) -> Response:alert = self._alert(pk)serializer = WarehouseAlertDeleteDestinationSerializer(data=request.data)serializer.is_valid(raise_exception=True)- hog_function_ids: list[UUID] = serializer.validated_data["hog_function_ids"]- try:- soft_delete_alert_destinations(- team_id=self.team_id,- alert_id=str(alert.destination_alert_id),- allowed_event_ids=EVENT_IDS[SourceKind(alert.source_kind)],- hog_function_ids=hog_function_ids,- )- except AlertDestinationValidationError as error:- raise as_drf_validation_error(error)+ destination_ids: set[UUID] = set(serializer.validated_data["destination_ids"])+ # A partial delete would leave the caller unsure which destinations remain, so refuse first.+ known = {+ destination.id for destination in list_alert_destinations(team_id=self.team_id, configuration_id=alert.id)+ }+ if unknown := sorted(str(destination_id) for destination_id in destination_ids - known):+ raise ValidationError({"destination_ids": f"No destination of this alert has the ID {', '.join(unknown)}."})+ delete_alert_destinations(team_id=self.team_id, configuration_id=alert.id, destination_ids=destination_ids)report_user_action(cast(User, request.user),"warehouse alert destination deleted",- {"alert_id": str(alert.id), "source_kind": alert.source_kind, "count": len(hog_function_ids)},+ {"alert_id": str(alert.id), "source_kind": alert.source_kind, "count": len(destination_ids)},team=self.team,request=request,)diff --git a/products/warehouse_sources/backend/sync_alerts/destinations.py b/products/warehouse_sources/backend/sync_alerts/destinations.pyindex 3d9dbb809630..1d6525d5801a 100644--- a/products/warehouse_sources/backend/sync_alerts/destinations.py+++ b/products/warehouse_sources/backend/sync_alerts/destinations.py@@ -1,26 +1,20 @@-"""What each warehouse alert event says, and where its destinations may send it.+"""The event ids of each warehouse alert kind, and where its destinations may send.-A destination is a set of HogFunctions, one per event kind it handles. They are the stored form of-the destination: native delivery reads them to find who to tell and words the message itself. Nothing-emits their events, and the generic HogFunction API refuses to edit them, so their templates never run.+A warehouse alert's destinations are `AlertDestination` rows on the platform, which words each+message itself. Delivery and the email recipient resolver address a transition by these event ids.The evaluation reads Postgres rows and records no failed check, so these alerts only fire and resolve."""-from dataclasses import replace-from typing import Final, Literal+from typing import Finalfrom products.alerts_platform.backend.facade.contracts import (- AlertDestinationAction,AlertEventKind,DestinationType,- EventKindSpec,IncidentAction,SourceKind,)-EventKind = Literal["firing", "resolved", "incident_opened", "incident_closed"]-WAREHOUSE_ALERT_DESTINATION_TYPES: Final = (DestinationType.SLACK,DestinationType.WEBHOOK,@@ -29,121 +23,27 @@ WAREHOUSE_ALERT_DESTINATION_TYPES: Final = (DestinationType.PAGERDUTY,)-_SOURCES_URL = "{project.url}/data-management/sources"-_DESTINATIONS_URL = "{project.url}/data-management/warehouse-destinations"---def _event_kinds(- *,- prefix: str,- product_label: str,- headline: str,- details: tuple[tuple[str, str], ...],- url: str,- data: dict[str, str],-) -> dict[EventKind, EventKindSpec]:- firing = EventKindSpec(- event_id=f"${prefix}_firing",- display_kind="firing",- header=f"🔴 {headline} '{{event.properties.alert_name}}' is firing",- details=details,- primary_action_url=url,- primary_action_label="View in PostHog",- webhook_body={- "id": "{event.uuid}",- "type": f"{prefix}.firing",- "timestamp": "{event.properties.triggered_at}",- "data": data,- },- product_label=product_label,- )- return {- "firing": firing,- "resolved": replace(- firing,- event_id=f"${prefix}_resolved",- display_kind="resolved",- header=f"🟢 {headline} '{{event.properties.alert_name}}' is resolved",- webhook_body={**firing.webhook_body, "type": f"{prefix}.resolved"},- ),- # The edge kinds follow every move into and out of FIRING, whatever cooldown decided. An- # incident manager needs a resolve for every trigger, or the incident it opened stays open.- "incident_opened": replace(- firing,- event_id=f"${prefix}_incident_opened",- display_kind="incident opened",- webhook_body={},- additional_actions=(AlertDestinationAction(url=url, label="View in PostHog"),),- incident_action=IncidentAction.TRIGGER,- ),- "incident_closed": replace(- firing,- event_id=f"${prefix}_incident_closed",- display_kind="incident closed",- header=f"{headline} '{{event.properties.alert_name}}' stopped firing",- details=(("Reason", "{event.properties.reason}"),),- webhook_body={},- incident_action=IncidentAction.RESOLVE,- ),- }-+# What a destination may ask to hear about. An incident manager hears every incident edge whatever it asks for.+WAREHOUSE_DESTINATION_EVENT_KINDS: Final = (AlertEventKind.FIRING, AlertEventKind.RESOLVED)-EVENT_KIND_CONFIG: Final[dict[SourceKind, dict[EventKind, EventKindSpec]]] = {- SourceKind.WAREHOUSE_SOURCE: _event_kinds(- prefix="warehouse_source_alert",- product_label="warehouse sync alert",- headline="Warehouse sync alert",- details=(- ("Source", "{event.properties.source}"),- ("Tables", "{event.properties.tables}"),- ("Error", "{event.properties.error}"),- ),- url=_SOURCES_URL,- data={- "alert_id": "{event.properties.alert_id}",- "alert_name": "{event.properties.alert_name}",- "source_id": "{event.properties.source_id}",- "source": "{event.properties.source}",- "tables": "{event.properties.tables}",- "error": "{event.properties.error}",- },- ),- SourceKind.WAREHOUSE_DESTINATION: _event_kinds(- prefix="warehouse_destination_alert",- product_label="warehouse destination alert",- headline="Warehouse destination alert",- details=(- ("Destination", "{event.properties.destination}"),- ("Status", "{event.properties.status}"),- ("Error", "{event.properties.error}"),- ),- url=_DESTINATIONS_URL,- data={- "alert_id": "{event.properties.alert_id}",- "alert_name": "{event.properties.alert_name}",- "destination_id": "{event.properties.destination_id}",- "destination": "{event.properties.destination}",- "status": "{event.properties.status}",- "error": "{event.properties.error}",- },- ),+_PREFIXES: Final[dict[SourceKind, str]] = {+ SourceKind.WAREHOUSE_SOURCE: "warehouse_source_alert",+ SourceKind.WAREHOUSE_DESTINATION: "warehouse_destination_alert",}-EVENT_IDS: Final[dict[SourceKind, tuple[str, ...]]] = {- source: tuple(spec.event_id for spec in kinds.values()) for source, kinds in EVENT_KIND_CONFIG.items()-}--# Which event id each announced kind's destinations filter on. The platform imports no source, so-# the delivery request carries this map.+# Which event id each announced kind goes out under. The platform imports no source, so the+# delivery request carries this map.EVENT_IDS_BY_KIND: Final[dict[SourceKind, dict[str, str]]] = {- source: {- AlertEventKind.FIRING.value: kinds["firing"].event_id,- AlertEventKind.RESOLVED.value: kinds["resolved"].event_id,- }- for source, kinds in EVENT_KIND_CONFIG.items()+ source: {kind.value: f"${prefix}_{kind.value}" for kind in WAREHOUSE_DESTINATION_EVENT_KINDS}+ for source, prefix in _PREFIXES.items()}+# The edge kinds follow every move into and out of FIRING, whatever cooldown decided. An incident+# manager needs a resolve for every trigger, or the incident it opened stays open.EVENT_IDS_BY_INCIDENT_ACTION: Final[dict[SourceKind, dict[str, str]]] = {- source: {spec.incident_action.value: spec.event_id for spec in kinds.values() if spec.incident_action}- for source, kinds in EVENT_KIND_CONFIG.items()+ source: {+ IncidentAction.TRIGGER.value: f"${prefix}_incident_opened",+ IncidentAction.RESOLVE.value: f"${prefix}_incident_closed",+ }+ for source, prefix in _PREFIXES.items()}diff --git a/products/warehouse_sources/backend/sync_alerts/email_recipients.py b/products/warehouse_sources/backend/sync_alerts/email_recipients.pyindex 903a5f7e0fb9..31d53f7fc732 100644--- a/products/warehouse_sources/backend/sync_alerts/email_recipients.py+++ b/products/warehouse_sources/backend/sync_alerts/email_recipients.py@@ -18,15 +18,8 @@ from products.alerts_platform.backend.facade.contracts import SourceKindWAREHOUSE_ALERT_KINDS: Final = (SourceKind.WAREHOUSE_SOURCE.value, SourceKind.WAREHOUSE_DESTINATION.value)-def project_member_emails(*, team_id: int, alert_id: str) -> list[str]:- """The addresses a warehouse alert emails, or none when the alert does not email project members.-- `alert_id` is the alert's configuration id, which is the id a warehouse alert's destinations match on.- """- try:- configuration_id = UUID(alert_id)- except ValueError:- return []+def project_member_emails(*, team_id: int, configuration_id: UUID) -> list[str]:+ """The addresses a warehouse alert emails, or none when the alert does not email project members."""configuration = get_configuration(team_id=team_id, source_kinds=WAREHOUSE_ALERT_KINDS, configuration_id=configuration_id)diff --git a/products/warehouse_sources/backend/sync_alerts/evaluation.py b/products/warehouse_sources/backend/sync_alerts/evaluation.pyindex cd26cf1210bb..5405cff8335b 100644--- a/products/warehouse_sources/backend/sync_alerts/evaluation.py+++ b/products/warehouse_sources/backend/sync_alerts/evaluation.py@@ -23,8 +23,7 @@ import structlogfrom posthog.dataclasses import frozenfrom posthog.models import Team-from products.alerts.backend.facade.destinations import configured_destination_template_ids-from products.alerts_platform.backend.facade.api import due_checks, slot_of+from products.alerts_platform.backend.facade.api import due_checks, has_incident_destination, slot_offrom products.alerts_platform.backend.facade.contracts import (AlertDeliveryRequest,InstanceCheckState,@@ -243,7 +242,6 @@ def _request(A firing that opens or closes needs a delivery even when cooldown or mute held the message,because a paging destination needs one resolve for every trigger."""- destination_alert_id = str(check.destination_alert_id)actions = {verdict.group.grouping_key: actionfor verdict in decision.verdicts@@ -254,11 +252,7 @@ def _request()is not None}- if actions and not configured_destination_template_ids(- team_id=check.team_id,- alert_id=destination_alert_id,- allowed_event_ids=tuple(EVENT_IDS_BY_INCIDENT_ACTION[source].values()),- ):+ if actions and not has_incident_destination(team_id=check.team_id, configuration_id=check.id):actions = {}sends_messages = any(verdict.outcome.notification != NotificationAction.NONE for verdict in decision.verdicts)if not sends_messages and not actions:@@ -269,7 +263,7 @@ def _request(team_id=check.team_id,configuration_id=str(check.id),evaluation_key=recorded.evaluation_key,- destination_alert_id=destination_alert_id,+ destination_alert_id=str(check.destination_alert_id),event_ids_by_kind=EVENT_IDS_BY_KIND[source],event_ids_by_incident_action=EVENT_IDS_BY_INCIDENT_ACTION[source],incident_actions=actions,diff --git a/products/warehouse_sources/backend/sync_alerts/registration.py b/products/warehouse_sources/backend/sync_alerts/registration.pyindex 06573cc10599..5574db7d0d50 100644--- a/products/warehouse_sources/backend/sync_alerts/registration.py+++ b/products/warehouse_sources/backend/sync_alerts/registration.py@@ -1,10 +1,10 @@"""Plugs warehouse alerts into the alerts platform, which imports no source. Called from `ready()`."""-from products.alerts.backend.facade.destinations import register_email_recipient_resolver+from uuid import UUID+from products.alerts_platform.backend.facade.contracts import SourceConfigSpec, SourceKind-from products.alerts_platform.backend.facade.delivery import register_source_describer+from products.alerts_platform.backend.facade.delivery import register_source_describer, register_source_recipientsfrom products.alerts_platform.backend.facade.source_configs import register_source_config-from products.warehouse_sources.backend.sync_alerts.destinations import EVENT_IDS_BY_KINDfrom products.warehouse_sources.backend.sync_alerts.wording import (describe_destination_transition,describe_source_transition,@@ -27,12 +27,12 @@ def load_destination_alert_config() -> SourceConfigSpec:return WAREHOUSE_DESTINATION_ALERT_CONFIG-def project_member_emails(*, team_id: int, alert_id: str) -> list[str]:+def project_member_emails(*, team_id: int, configuration_id: UUID) -> list[str]:from products.warehouse_sources.backend.sync_alerts.email_recipients import ( # noqa: PLC0415 - keeps the platform API and the email tasks off the startup pathproject_member_emails as resolve,)- return resolve(team_id=team_id, alert_id=alert_id)+ return resolve(team_id=team_id, configuration_id=configuration_id)def register() -> None:@@ -41,9 +41,5 @@ def register() -> None:register_source_describer(SourceKind.WAREHOUSE_DESTINATION, describe_destination_transition)register_source_config(SourceKind.WAREHOUSE_SOURCE, load_source_alert_config)register_source_config(SourceKind.WAREHOUSE_DESTINATION, load_destination_alert_config)- # Firing and resolved only. The incident events reach an incident manager, and an email for- # them would send each person a second copy of the same transition.- register_email_recipient_resolver(- event_ids=[event_id for by_kind in EVENT_IDS_BY_KIND.values() for event_id in by_kind.values()],- resolver=project_member_emails,- )+ register_source_recipients(SourceKind.WAREHOUSE_SOURCE, project_member_emails)+ register_source_recipients(SourceKind.WAREHOUSE_DESTINATION, project_member_emails)diff --git a/products/warehouse_sources/backend/sync_alerts/source_config.py b/products/warehouse_sources/backend/sync_alerts/source_config.pyindex 0e46a21dde58..68348113892a 100644--- a/products/warehouse_sources/backend/sync_alerts/source_config.py+++ b/products/warehouse_sources/backend/sync_alerts/source_config.py@@ -1,7 +1,7 @@"""The `source_config` of a warehouse alert on the alerts platform, and its native lifecycle.-A warehouse alert has no row of its own in this product. The platform creates and deletes it, and its-configuration id is the id its destinations match on.+A warehouse alert has no row of its own in this product. The platform creates and deletes it, and+keeps its destinations as `AlertDestination` rows that go with it."""from collections.abc import Callable@@ -10,7 +10,6 @@ from uuid import UUIDfrom pydantic import BaseModel, ConfigDict, Field-from products.alerts.backend.facade.destinations import soft_delete_all_alert_destinationsfrom products.alerts_platform.backend.facade.contracts import (GroupingMode,InvalidPlatformAlertEdit,@@ -21,7 +20,7 @@ from products.alerts_platform.backend.facade.contracts import ()from products.warehouse_sources.backend.models.external_data_destination import ExternalDataDestinationfrom products.warehouse_sources.backend.models.external_data_source import ExternalDataSource-from products.warehouse_sources.backend.sync_alerts.destinations import EVENT_IDS, EVENT_IDS_BY_INCIDENT_ACTION+from products.warehouse_sources.backend.sync_alerts.destinations import EVENT_IDS_BY_INCIDENT_ACTIONfrom products.warehouse_sources.backend.sync_alerts.wording import (DEFAULT_GROUP_BY,DESTINATION_ID_KEY,@@ -126,18 +125,12 @@ def _spec(def check_in_team(*, team_id: int, edit: PlatformAlertConfigurationEdit) -> None:_require_ids_in_team(source_kind, team_id, edit)- def remove_destinations(*, team_id: int, configuration_id: UUID) -> None:- soft_delete_all_alert_destinations(- team_id=team_id, alert_id=str(configuration_id), allowed_event_ids=EVENT_IDS[source_kind]- )-return SourceConfigSpec(model=model,validate=validate,check_in_team=check_in_team,- native=NativeAlertLifecycle(- check_interval_minutes=CHECK_INTERVAL_MINUTES, remove_destinations=remove_destinations- ),+ # No `remove_destinations`, so the platform owns the destinations from the start.+ native=NativeAlertLifecycle(check_interval_minutes=CHECK_INTERVAL_MINUTES),incident_event_ids=EVENT_IDS_BY_INCIDENT_ACTION[source_kind],alert_url=alert_url,)diff --git a/products/warehouse_sources/backend/tests/test_sync_alerts.py b/products/warehouse_sources/backend/tests/test_sync_alerts.pyindex 6ae215eacdc6..137374a016dc 100644--- a/products/warehouse_sources/backend/tests/test_sync_alerts.py+++ b/products/warehouse_sources/backend/tests/test_sync_alerts.py@@ -17,15 +17,12 @@ from posthog.models.team.team import Teamfrom posthog.models.user import Userfrom products.access_control.backend.models.access_control import AccessControl-from products.alerts.backend.facade.destinations import (- MAX_DESTINATIONS_PER_ALERT,- list_alert_destination_groups,- list_delivery_destination_groups,-)+from products.alerts.backend.facade.destinations import MAX_DESTINATIONS_PER_ALERTfrom products.alerts_platform.backend.facade import testing as platform_testingfrom products.alerts_platform.backend.facade.api import (create_configuration,delete_configuration,+ list_alert_destinations,list_configurations,record_outcomes,slot_of,@@ -33,7 +30,6 @@ from products.alerts_platform.backend.facade.api import (from products.alerts_platform.backend.facade.contracts import (AlertEventKind,AnnouncedTransition,- DestinationType,Grouping,GroupingMode,IncidentAction,@@ -42,14 +38,12 @@ from products.alerts_platform.backend.facade.contracts import (SourceBatchEvaluation,SourceKind,)-from products.cdp.backend.models.hog_functions.hog_function import HogFunction-from products.cdp.backend.templates.fixtures import template_pagerduty, template_slack-from products.cdp.backend.templates.hog_function_template import sync_template_to_dbfrom products.warehouse_sources.backend.models.external_data_destination import ExternalDataDestinationfrom products.warehouse_sources.backend.models.external_data_schema import ExternalDataSchemafrom products.warehouse_sources.backend.models.external_data_source import ExternalDataSourcefrom products.warehouse_sources.backend.presentation.views import warehouse_alert_destinations as destinations_view-from products.warehouse_sources.backend.sync_alerts.destinations import EVENT_IDS, EVENT_IDS_BY_KIND+from products.warehouse_sources.backend.sync_alerts.destinations import EVENT_IDS_BY_KIND+from products.warehouse_sources.backend.sync_alerts.email_recipients import project_member_emailsfrom products.warehouse_sources.backend.sync_alerts.evaluation import evaluate_warehouse_alert_batchfrom products.warehouse_sources.backend.sync_alerts.wording import (describe_destination_transition,@@ -132,15 +126,14 @@ class TestWarehouseAlerts(APIBaseTest):self.cutoff += timedelta(minutes=5)platform_testing.set_due_at(configuration_id, self.cutoff - timedelta(minutes=1))- def _add_pagerduty(self, configuration_id: UUID) -> list[str]:- sync_template_to_db(template_pagerduty)+ def _add_pagerduty(self, configuration_id: UUID) -> str:added = self.client.post(f"/api/projects/{self.team.id}/warehouse_alerts/{configuration_id}/destinations/",{"type": "pagerduty", "pagerduty_routing_key": PAGERDUTY_ROUTING_KEY},format="json",)assert added.status_code == status.HTTP_201_CREATED, added.json()- return added.json()["hog_function_ids"]+ return added.json()["id"]def _create_on_the_platform(self, body: dict[str, Any]) -> Any:return self.client.post(f"/api/projects/{self.team.id}/platform_alerts/", body, format="json")@@ -423,7 +416,6 @@ class TestWarehouseAlerts(APIBaseTest):assert response.status_code == expected, response.json()def test_an_alert_and_its_slack_destination_route_a_failure_to_that_channel(self) -> None:- sync_template_to_db(template_slack)schema = self._schema(self._source(), "charges", Status.FAILED)created = self._create_on_the_platform(@@ -442,16 +434,24 @@ class TestWarehouseAlerts(APIBaseTest):destinations_url = f"/api/projects/{self.team.id}/warehouse_alerts/{alert['id']}/destinations/"added = self.client.post(destinations_url,- {"type": "slack", "slack_workspace_id": 42, "slack_channel_id": "C123", "slack_channel_name": "data"},+ {+ "type": "slack",+ "slack_workspace_id": 42,+ "slack_channel_id": "C123",+ "slack_channel_name": "data",+ "pagerduty_routing_key": PAGERDUTY_ROUTING_KEY,+ },format="json",)assert added.status_code == status.HTTP_201_CREATED, added.json()- hog_function_ids = added.json()["hog_function_ids"](listed,) = self.client.get(destinations_url).json()- assert (listed["type"], listed["slack_channel_id"], sorted(listed["hog_function_ids"])) == (+ assert listed == added.json()+ # A Slack row keeps no PagerDuty setting, neither the defaults nor a key sent alongside.+ assert not {key for key in listed if key.startswith("pagerduty_")}+ assert (listed["type"], listed["slack_channel_id"], listed["event_kinds"]) == ("slack","C123",- sorted(hog_function_ids),+ ["firing", "resolved"],)with team_scope(self.team.id):@@ -464,14 +464,26 @@ class TestWarehouseAlerts(APIBaseTest):assert delivery.destination_alert_id == alert["id"]firing_event_id = delivery.event_ids_by_kind[AlertEventKind.FIRING.value]assert firing_event_id == EVENT_IDS_BY_KIND[SourceKind.WAREHOUSE_SOURCE]["firing"]- (routed,) = list_alert_destination_groups(- team_id=self.team.id, alert_id=delivery.destination_alert_id, allowed_event_ids=[firing_event_id]- )- assert routed.data["slack_channel_id"] == "C123"deleted = self.client.delete(f"/api/projects/{self.team.id}/platform_alerts/{alert['id']}/")assert deleted.status_code == status.HTTP_204_NO_CONTENT- assert not HogFunction.objects.filter(id__in=hog_function_ids, deleted=False).exists()+ assert list_alert_destinations(team_id=self.team.id, configuration_id=UUID(alert["id"])) == ()++ def test_deleting_a_destination_refuses_an_id_of_another_alert(self) -> None:+ mine = self._alert(SourceKind.WAREHOUSE_SOURCE, {}, "source_id")+ other = self._alert(SourceKind.WAREHOUSE_SOURCE, {}, "source_id")+ kept, theirs = self._add_pagerduty(mine), self._add_pagerduty(other)+ delete_url = f"/api/projects/{self.team.id}/warehouse_alerts/{mine}/destinations/delete/"++ refused = self.client.post(delete_url, {"destination_ids": [kept, theirs]}, format="json")+ assert refused.status_code == status.HTTP_400_BAD_REQUEST, refused.json()+ assert [len(list_alert_destinations(team_id=self.team.id, configuration_id=id_)) for id_ in (mine, other)] == [+ 1,+ 1,+ ]++ assert self.client.post(delete_url, {"destination_ids": [kept]}, format="json").status_code == 204+ assert list_alert_destinations(team_id=self.team.id, configuration_id=mine) == ()@parameterized.expand([@@ -503,23 +515,22 @@ class TestWarehouseAlerts(APIBaseTest):def test_deleting_a_firing_alert_resolves_its_incident_before_the_destination_goes(self) -> None:schema = self._schema(self._source(), "charges", Status.FAILED)configuration_id = self._alert(SourceKind.WAREHOUSE_SOURCE, {"group_by": "schema"}, "schema_id")- hog_function_ids = self._add_pagerduty(configuration_id)+ self._add_pagerduty(configuration_id)self._next_check(configuration_id, self._only_outcome(self._evaluate(SourceKind.WAREHOUSE_SOURCE)))sent: list[tuple[str, dict[str, IncidentAction], int]] = []def deliver(request: Any) -> None:- live = HogFunction.objects.filter(id__in=hog_function_ids, deleted=False).count()+ live = len(list_alert_destinations(team_id=self.team.id, configuration_id=configuration_id))sent.append((request.destination_alert_id, request.incident_actions, live))with patch("products.alerts_platform.backend.temporal.web_start.deliver_evaluation", side_effect=deliver):deleted = self.client.delete(f"/api/projects/{self.team.id}/platform_alerts/{configuration_id}/")assert deleted.status_code == status.HTTP_204_NO_CONTENT- assert sent == [(str(configuration_id), {str(schema.id): IncidentAction.RESOLVE}, len(hog_function_ids))]- assert not HogFunction.objects.filter(id__in=hog_function_ids, deleted=False).exists()+ assert sent == [(str(configuration_id), {str(schema.id): IncidentAction.RESOLVE}, 1)]+ assert list_alert_destinations(team_id=self.team.id, configuration_id=configuration_id) == ()def test_an_alert_takes_only_a_few_destinations(self) -> None:- sync_template_to_db(template_slack)configuration_id = self._alert(SourceKind.WAREHOUSE_SOURCE, {}, "source_id")destinations_url = f"/api/projects/{self.team.id}/warehouse_alerts/{configuration_id}/destinations/"@@ -539,7 +550,6 @@ class TestWarehouseAlerts(APIBaseTest):assert len(self.client.get(destinations_url).json()) == MAX_DESTINATIONS_PER_ALERTdef test_a_destination_added_while_its_alert_is_deleted_is_refused(self) -> None:- sync_template_to_db(template_slack)configuration_id = self._alert(SourceKind.WAREHOUSE_SOURCE, {}, "source_id")real_get_configuration = destinations_view.get_configuration@@ -561,7 +571,7 @@ class TestWarehouseAlerts(APIBaseTest):)assert response.status_code == status.HTTP_404_NOT_FOUND, response.json()- assert not HogFunction.objects.filter(team=self.team, deleted=False).exists()+ assert list_alert_destinations(team_id=self.team.id, configuration_id=configuration_id) == ()@parameterized.expand([@@ -675,20 +685,9 @@ class TestWarehouseAlerts(APIBaseTest):configuration_id = self._alert(kind, {"email_project_members": email_project_members}, grouping_key)expected = sorted([self.user.email, person.email] if reached else [self.user.email])- groups_by_event = {- event_id: list_delivery_destination_groups(- team_id=self.team.id, alert_id=str(configuration_id), allowed_event_ids=[event_id]- )- for event_id in EVENT_IDS[kind]- }-- message_event_ids = set(EVENT_IDS_BY_KIND[kind].values())- assert {event_id: [group.data for group in groups] for event_id, groups in groups_by_event.items()} == {- event_id: [{"type": DestinationType.EMAIL, "email_addresses": expected}]- if email_project_members and event_id in message_event_ids- else []- for event_id in EVENT_IDS[kind]- }+ assert project_member_emails(team_id=self.team.id, configuration_id=configuration_id) == (+ expected if email_project_members else []+ )def test_a_failing_table_emails_the_project_members_of_an_alert_that_emails_them(self) -> None:self._person("member")diff --git a/products/warehouse_sources/frontend/generated/api.schemas.ts b/products/warehouse_sources/frontend/generated/api.schemas.tsindex c4cba5e5575a..3407b6ccd04f 100644--- a/products/warehouse_sources/frontend/generated/api.schemas.ts+++ b/products/warehouse_sources/frontend/generated/api.schemas.ts@@ -14744,6 +14744,18 @@ export const WarehouseAlertDestinationTypeEnumApi = {Pagerduty: 'pagerduty',} as const+/**+ * * `firing` - firing+ * * `resolved` - resolved+ */+export type WarehouseAlertEventKindChoicesEnumApi =+ (typeof WarehouseAlertEventKindChoicesEnumApi)[keyof typeof WarehouseAlertEventKindChoicesEnumApi]++export const WarehouseAlertEventKindChoicesEnumApi = {+ Firing: 'firing',+ Resolved: 'resolved',+} as const+/*** * `critical` - critical* * `error` - error@@ -14771,8 +14783,8 @@ export const PagerDutyRegionEnumApi = {} as constexport interface WarehouseAlertDestinationApi {- /** HogFunction IDs that make up this destination, one per alert event kind it handles. Send them all to the delete endpoint to remove the destination. */- hog_function_ids: string[]+ /** Destination ID. Send it to the delete endpoint to remove the destination. */+ id: string/** Notification destination type.** * `slack` - slack@@ -14781,8 +14793,10 @@ export interface WarehouseAlertDestinationApi {* * `discord` - discord* * `pagerduty` - pagerduty */type: WarehouseAlertDestinationTypeEnumApi- /** Whether every HogFunction of the destination is enabled, so it notifies for every event kind. */+ /** Whether the destination is sent to. */enabled: boolean+ /** Which transitions send a message. A PagerDuty destination opens an incident when the alert fires and resolves it when the alert resolves, whatever this lists. */+ event_kinds: WarehouseAlertEventKindChoicesEnumApi[]/** Integration ID of the Slack workspace. Set when type=slack. */slack_workspace_id?: number/** Slack channel ID. Set when type=slack. */@@ -14816,6 +14830,11 @@ export interface WarehouseAlertCreateDestinationApi {* * `discord` - discord* * `pagerduty` - pagerduty */type: WarehouseAlertDestinationTypeEnumApi+ /**+ * Which transitions send a message. A PagerDuty destination opens an incident when the alert fires and resolves it when the alert resolves, whatever this lists. Defaults to every kind.+ * @minItems 1+ */+ event_kinds?: WarehouseAlertEventKindChoicesEnumApi[]/** Integration ID for the Slack workspace. Required when type=slack. */slack_workspace_id?: number/** Slack channel ID. Required when type=slack. */@@ -14840,18 +14859,13 @@ export interface WarehouseAlertCreateDestinationApi {pagerduty_region?: PagerDutyRegionEnumApi}-export interface WarehouseAlertDestinationCreatedApi {- /** HogFunction IDs created for the destination, one per event kind. */- hog_function_ids: string[]-}-export interface WarehouseAlertDeleteDestinationApi {/**- * HogFunction IDs of one destination, as the list endpoint returns them. They are deleted together.+ * IDs of the alert's destinations to delete, as the list endpoint returns them.* @minItems 1- * @maxItems 100+ * @maxItems 5*/- hog_function_ids: string[]+ destination_ids: string[]}export interface WarehouseColumnStatisticsApi {diff --git a/products/warehouse_sources/frontend/generated/api.ts b/products/warehouse_sources/frontend/generated/api.tsindex 6bbe25e02533..4ae20df14117 100644--- a/products/warehouse_sources/frontend/generated/api.ts+++ b/products/warehouse_sources/frontend/generated/api.ts@@ -68,7 +68,6 @@ import type {WarehouseAlertCreateDestinationApi,WarehouseAlertDeleteDestinationApi,WarehouseAlertDestinationApi,- WarehouseAlertDestinationCreatedApi,WarehouseColumnStatisticsApi,WarehouseColumnStatisticsListParams,WebhookInfoResponseApi,@@ -1553,15 +1552,15 @@ export const getWarehouseAlertsDestinationsCreateUrl = (projectId: string, id: s}/**- * Add a notification destination to a warehouse alert. One HogFunction is created per alert event kind (firing, resolved). A PagerDuty destination gets the incident opened and incident closed kinds instead, so each incident it opens is resolved.+ * Add a notification destination to a warehouse alert.*/export const warehouseAlertsDestinationsCreate = async (projectId: string,id: string,warehouseAlertCreateDestinationApi: WarehouseAlertCreateDestinationApi,options?: RequestInit-): Promise<WarehouseAlertDestinationCreatedApi> => {- return apiMutator<WarehouseAlertDestinationCreatedApi>(getWarehouseAlertsDestinationsCreateUrl(projectId, id), {+): Promise<WarehouseAlertDestinationApi> => {+ return apiMutator<WarehouseAlertDestinationApi>(getWarehouseAlertsDestinationsCreateUrl(projectId, id), {...options,method: 'POST',headers: { 'Content-Type': 'application/json', ...options?.headers },@@ -1574,7 +1573,7 @@ export const getWarehouseAlertsDestinationsDeleteCreateUrl = (projectId: string,}/**- * Delete one notification destination of a warehouse alert, all its HogFunctions together.+ * Delete notification destinations of a warehouse alert.*/export const warehouseAlertsDestinationsDeleteCreate = async (projectId: string,diff --git a/products/warehouse_sources/frontend/generated/api.zod.ts b/products/warehouse_sources/frontend/generated/api.zod.tsindex af4bb0a8ebaa..a2e75c95959c 100644--- a/products/warehouse_sources/frontend/generated/api.zod.ts+++ b/products/warehouse_sources/frontend/generated/api.zod.ts@@ -723,8 +723,9 @@ export const ExternalDataSourcesStoreCredentialsCreateBody = /* @__PURE__ */ zod.describe('Deep\/recursive schema (opaque in Zod — use TypeScript types for full shape)')/**- * Add a notification destination to a warehouse alert. One HogFunction is created per alert event kind (firing, resolved). A PagerDuty destination gets the incident opened and incident closed kinds instead, so each incident it opens is resolved.+ * Add a notification destination to a warehouse alert.*/+export const warehouseAlertsDestinationsCreateBodyPagerdutySeverityDefault = `critical`export const warehouseAlertsDestinationsCreateBodyPagerdutyRegionDefault = `us`@@ -737,6 +738,13 @@ export const WarehouseAlertsDestinationsCreateBody = /* @__PURE__ */ zod.object(.describe('Notification destination type.\n\n\* `slack` - slack\n\* `webhook` - webhook\n\* `teams` - teams\n\* `discord` - discord\n\* `pagerduty` - pagerduty'),+ event_kinds: zod+ .array(zod.enum(['firing', 'resolved']).describe('\* `firing` - firing\n\* `resolved` - resolved'))+ .min(1)+ .default([`firing`, `resolved`])+ .describe(+ 'Which transitions send a message. A PagerDuty destination opens an incident when the alert fires and resolves it when the alert resolves, whatever this lists. Defaults to every kind.'+ ),slack_workspace_id: zod.number().optional()@@ -763,14 +771,14 @@ export const WarehouseAlertsDestinationsCreateBody = /* @__PURE__ */ zod.object(})/**- * Delete one notification destination of a warehouse alert, all its HogFunctions together.+ * Delete notification destinations of a warehouse alert.*/-export const warehouseAlertsDestinationsDeleteCreateBodyHogFunctionIdsMax = 100+export const warehouseAlertsDestinationsDeleteCreateBodyDestinationIdsMax = 5export const WarehouseAlertsDestinationsDeleteCreateBody = /* @__PURE__ */ zod.object({- hog_function_ids: zod+ destination_ids: zod.array(zod.uuid()).min(1)- .max(warehouseAlertsDestinationsDeleteCreateBodyHogFunctionIdsMax)- .describe('HogFunction IDs of one destination, as the list endpoint returns them. They are deleted together.'),+ .max(warehouseAlertsDestinationsDeleteCreateBodyDestinationIdsMax)+ .describe("IDs of the alert's destinations to delete, as the list endpoint returns them."),})diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex 8e7aaa42f35d..8b87621e7a76 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -123725,6 +123725,18 @@ export namespace Schemas {Pagerduty: 'pagerduty',} as const;+ /**+ * * `firing` - firing+ * * `resolved` - resolved+ */+ export type WarehouseAlertEventKindChoicesEnum = typeof WarehouseAlertEventKindChoicesEnum[keyof typeof WarehouseAlertEventKindChoicesEnum];+++ export const WarehouseAlertEventKindChoicesEnum = {+ Firing: 'firing',+ Resolved: 'resolved',+ } as const;+export interface WarehouseAlertCreateDestination {/** Notification destination type.*@@ -123734,6 +123746,11 @@ export namespace Schemas {* * `discord` - discord* * `pagerduty` - pagerduty */type: WarehouseAlertDestinationTypeEnum;+ /**+ * Which transitions send a message. A PagerDuty destination opens an incident when the alert fires and resolves it when the alert resolves, whatever this lists. Defaults to every kind.+ * @minItems 1+ */+ event_kinds?: WarehouseAlertEventKindChoicesEnum[];/** Integration ID for the Slack workspace. Required when type=slack. */slack_workspace_id?: number;/** Slack channel ID. Required when type=slack. */@@ -123760,16 +123777,16 @@ export namespace Schemas {export interface WarehouseAlertDeleteDestination {/**- * HogFunction IDs of one destination, as the list endpoint returns them. They are deleted together.+ * IDs of the alert's destinations to delete, as the list endpoint returns them.* @minItems 1- * @maxItems 100+ * @maxItems 5*/- hog_function_ids: string[];+ destination_ids: string[];}export interface WarehouseAlertDestination {- /** HogFunction IDs that make up this destination, one per alert event kind it handles. Send them all to the delete endpoint to remove the destination. */- hog_function_ids: string[];+ /** Destination ID. Send it to the delete endpoint to remove the destination. */+ id: string;/** Notification destination type.** * `slack` - slack@@ -123778,8 +123795,10 @@ export namespace Schemas {* * `discord` - discord* * `pagerduty` - pagerduty */type: WarehouseAlertDestinationTypeEnum;- /** Whether every HogFunction of the destination is enabled, so it notifies for every event kind. */+ /** Whether the destination is sent to. */enabled: boolean;+ /** Which transitions send a message. A PagerDuty destination opens an incident when the alert fires and resolves it when the alert resolves, whatever this lists. */+ event_kinds: WarehouseAlertEventKindChoicesEnum[];/** Integration ID of the Slack workspace. Set when type=slack. */slack_workspace_id?: number;/** Slack channel ID. Set when type=slack. */@@ -123804,11 +123823,6 @@ export namespace Schemas {pagerduty_region?: PagerDutyRegionEnum;}- export interface WarehouseAlertDestinationCreated {- /** HogFunction IDs created for the destination, one per event kind. */- hog_function_ids: string[];- }-export interface WarehouseConnection {/** Postgres connection host. The warehouse name is the SNI subdomain, e.g. my-warehouse.dw.us.postwh.com */host: string;