: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): fire hog functions on alerts whose destinations the platform keeps (#128758)
Jimmy Zhuae1bf47bff63
diff --git a/products/alerts_platform/backend/delivery/evaluation.py b/products/alerts_platform/backend/delivery/evaluation.pyindex 7b2d0670a026..1268fe9abffb 100644--- a/products/alerts_platform/backend/delivery/evaluation.py+++ b/products/alerts_platform/backend/delivery/evaluation.py@@ -5,7 +5,7 @@ is what decides which destinations there are and which transport each one takes."""import json-from collections.abc import Callable+from collections.abc import Callable, Sequencefrom dataclasses import replacefrom datetime import UTC, datetimefrom functools import cache@@ -402,14 +402,19 @@ def _split_by_path(A transition inside a firing takes the path its firing took first, so a firing and its resolvenever split across paths. Any other transition takes the path its destinations allow now.++ An alert whose destinations the platform keeps sends every transition to them natively. Its+ stock destinations are `AlertDestination` rows, so any HogFunction listening on the alert is one+ a person built, and the source also emits every transition for it. That includes a function+ from a custom template or one with no `alert_id` filter, which no destination lookup lists, so+ the source emits without asking who listens, as its own stack did. No destination is on both+ paths, so nothing is sent twice. Moving an existing alert's destinations onto the platform must+ therefore disable the HogFunctions it copies, or each of them hears every transition as well."""- # Destinations in `AlertDestination` have no HogFunction to emit for.- if (- not request.owned_by_platform- or platform_destinations is not None- or delivery_paths.emitter_for(request.source) is None- ):+ if not request.owned_by_platform or delivery_paths.emitter_for(request.source) is None:return announced, ()+ if platform_destinations is not None:+ return announced, _emissions(request, announced.transitions)# Read once per delivery, because the chooser reads every destination of the alert.@cache@@ -418,11 +423,11 @@ def _split_by_path(source=request.source,team_id=request.team_id,alert_id=request.destination_alert_id,- allowed_event_ids=[*request.event_ids_by_kind.values(), *request.event_ids_by_incident_action.values()],+ allowed_event_ids=_all_event_ids(request),)native: list[AnnouncedTransition] = []- emissions: list[LegacyEventEmission] = []+ emitted: list[AnnouncedTransition] = []for transition in announced.transitions:path = (episode_paths.episode_path(@@ -435,9 +440,16 @@ def _split_by_path(if transition.episode_started_at is not Noneelse choose())- if path == DeliveryPath.NATIVE:- native.append(transition)- continue+ (native if path == DeliveryPath.NATIVE else emitted).append(transition)+ return replace(announced, transitions=tuple(native)), _emissions(request, emitted)+++def _emissions(+ request: AlertDeliveryRequest, transitions: Sequence[AnnouncedTransition]+) -> tuple[LegacyEventEmission, ...]:+ """The events the source emits for its HogFunctions, one per transition a destination subscribes to."""+ emissions: list[LegacyEventEmission] = []+ for transition in transitions:subscribed = _subscribed_events(request, transition)if subscribed.message_event or subscribed.incident_event:emissions.append(@@ -447,7 +459,12 @@ def _split_by_path(incident_action=subscribed.incident_action if subscribed.incident_event else None,))- return replace(announced, transitions=tuple(native)), tuple(emissions)+ return tuple(emissions)+++def _all_event_ids(request: AlertDeliveryRequest) -> list[str]:+ """Every event the alert's HogFunctions can listen to: its message events and its incident events."""+ return [*request.event_ids_by_kind.values(), *request.event_ids_by_incident_action.values()]def _overflow_notice_keys(diff --git a/products/alerts_platform/backend/tests/test_delivery_evaluation.py b/products/alerts_platform/backend/tests/test_delivery_evaluation.pyindex 6eca12d4839e..98d1c9f21a3e 100644--- a/products/alerts_platform/backend/tests/test_delivery_evaluation.py+++ b/products/alerts_platform/backend/tests/test_delivery_evaluation.py@@ -812,6 +812,28 @@ class TestDeliveryPath(APIBaseTest):assert sorted(channel for channel, _ in RecordingTransport.sends) == native_channelsassert (outcome.emitted_transitions, outcome.emitted_events) == (emitted, emitted)+ def test_an_alert_whose_destinations_the_platform_keeps_emits_for_its_hog_functions(self) -> None:+ PlatformAlertConfiguration.objects.for_team(self.team.id).filter(id=self.configuration.id).update(+ destinations_owner=PlatformAlertOwner.PLATFORM+ )+ create_alert_destinations(+ team_id=self.team.id,+ configuration_id=self.configuration.id,+ destinations=(+ NewAlertDestination(+ data=cast(AlertDestinationData, {**SLACK, "slack_channel_id": "C-native"}),+ event_kinds=(AlertEventKind.FIRING, AlertEventKind.RESOLVED),+ ),+ ),+ )++ # The lookup lists no HogFunction, as for one built from a custom template, and the event still goes out.+ # A failing chooser would also emit, so the native send shows the path choice is never read here.+ self._run(_announcement(_transition(AlertEventKind.FIRING)), self._fails, groups=[SUBSCRIBER])++ assert len(self.emitted) == 1+ assert [channel for channel, _ in RecordingTransport.sends] == ["C-native"]+def test_a_firing_and_its_resolve_take_the_path_the_firing_took(self) -> None:fired = self._run(_announcement(_transition(AlertEventKind.FIRING)),