: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): gate the alertmanager webhook body for teams off the legacy list (#129769)
Jimmy Zhuac3d4fb1f848
diff --git a/products/alerts/backend/facade/contracts.py b/products/alerts/backend/facade/contracts.pyindex 2da5dbeeb686..4a707e93a78f 100644--- a/products/alerts/backend/facade/contracts.py+++ b/products/alerts/backend/facade/contracts.py@@ -97,10 +97,9 @@ class HogFunctionPathReason(StrEnum):# A function takes the source's alert events without being bound to one alert. Native# delivery reads destinations per alert, so it would never send to it.UNBOUND = "unbound"- # A stock webhook whose team receives the platform body natively. Its receiver parses the- # body the HogFunction sends.- NEW_WEBHOOK_BODY = "new_webhook_body"WEBHOOK_BODY_UNKNOWN = "webhook_body_unknown"+ # Native delivery cannot send this source's legacy webhook body, and the team keeps it.+ LEGACY_WEBHOOK_BODY_NOT_NATIVE = "legacy_webhook_body_not_native"# Native delivery sends PagerDuty incident actions only, never the firing event.PAGERDUTY_NOT_NATIVE = "pagerduty_not_native"diff --git a/products/alerts/backend/logic/delivery_path.py b/products/alerts/backend/logic/delivery_path.pyindex 18b43342092a..1addbeb92ecc 100644--- a/products/alerts/backend/logic/delivery_path.py+++ b/products/alerts/backend/logic/delivery_path.py@@ -28,7 +28,7 @@ from products.alerts.backend.logic.destination_eligibility import (unbound_alert_destinations,)from products.alerts_platform.backend.facade.contracts import DeliveryPath, SourceKind-from products.alerts_platform.backend.facade.delivery import native_sends_legacy_webhook_body+from products.alerts_platform.backend.facade.delivery import has_legacy_webhook_body, keeps_legacy_webhook_bodyfrom products.cdp.backend.internal_events import LEGACY_INSIGHT_ALERT_EVENTlogger = structlog.get_logger(__name__)@@ -81,10 +81,10 @@ class _TeamDestinations:):return DeliveryPathChoice(reason=HogFunctionPathReason.PAGERDUTY_NOT_NATIVE)if any(destination.template_id == WebhookDestination.template_id for destination in destinations):- if self._native_webhook_body is None:+ if self._keeps_legacy_webhook_body is None:return DeliveryPathChoice(reason=HogFunctionPathReason.WEBHOOK_BODY_UNKNOWN)- if not self._native_webhook_body:- return DeliveryPathChoice(reason=HogFunctionPathReason.NEW_WEBHOOK_BODY)+ if self._keeps_legacy_webhook_body and not has_legacy_webhook_body(self._source):+ return DeliveryPathChoice(reason=HogFunctionPathReason.LEGACY_WEBHOOK_BODY_NOT_NATIVE)return DeliveryPathChoice()@cached_property@@ -96,5 +96,5 @@ class _TeamDestinations:)@cached_property- def _native_webhook_body(self) -> bool | None:- return native_sends_legacy_webhook_body(self._source, Team.objects.get(id=self._team_id))+ def _keeps_legacy_webhook_body(self) -> bool | None:+ return keeps_legacy_webhook_body(Team.objects.get(id=self._team_id))diff --git a/products/alerts/backend/test/test_delivery_path.py b/products/alerts/backend/test/test_delivery_path.pyindex f8122b2af537..1efb334b0eda 100644--- a/products/alerts/backend/test/test_delivery_path.py+++ b/products/alerts/backend/test/test_delivery_path.py@@ -1,5 +1,7 @@from uuid import uuid4+from unittest.mock import patch+from django.test import SimpleTestCasefrom parameterized import parameterized@@ -41,3 +43,36 @@ class TestDeliveryPathChoice(SimpleTestCase):other = team.choice([_destination("template-slack", event_id)])assert (paged, other.path) == (expected, DeliveryPath.NATIVE)++ @parameterized.expand(+ [+ ("logs_keeps_legacy_body", SourceKind.LOGS, True, DeliveryPathChoice()),+ ("logs_gets_platform_body", SourceKind.LOGS, False, DeliveryPathChoice()),+ (+ "insight_keeps_legacy_body",+ SourceKind.INSIGHT,+ True,+ DeliveryPathChoice(reason=HogFunctionPathReason.LEGACY_WEBHOOK_BODY_NOT_NATIVE),+ ),+ ("insight_gets_platform_body", SourceKind.INSIGHT, False, DeliveryPathChoice()),+ (+ "unreadable_flag",+ SourceKind.LOGS,+ None,+ DeliveryPathChoice(reason=HogFunctionPathReason.WEBHOOK_BODY_UNKNOWN),+ ),+ ]+ )+ def test_a_stock_webhook_delivers_natively_unless_its_team_keeps_a_body_native_delivery_lacks(+ self, _name: str, source: SourceKind, keeps_legacy_body: bool | None, expected: DeliveryPathChoice+ ) -> None:+ team = _TeamDestinations(source=source, team_id=1, allowed_event_ids=())+ event_id = f"${source.value}_alert_firing"++ with (+ patch("products.alerts.backend.logic.delivery_path.Team.objects.get"),+ patch(+ "products.alerts.backend.logic.delivery_path.keeps_legacy_webhook_body", return_value=keeps_legacy_body+ ),+ ):+ assert team.choice([_destination("template-webhook", event_id)]) == expecteddiff --git a/products/alerts_platform/backend/delivery/legacy_webhook.py b/products/alerts_platform/backend/delivery/legacy_webhook.pyindex 5351c2424f07..e45c1514a4eb 100644--- a/products/alerts_platform/backend/delivery/legacy_webhook.py+++ b/products/alerts_platform/backend/delivery/legacy_webhook.py@@ -18,6 +18,9 @@ if TYPE_CHECKING:# A closed list of teams. No team joins it after the platform delivers for its source.LEGACY_WEBHOOK_BODY_FLAG: Final = "alert-platform-legacy-webhook-body"+# A team off the list above keeps the legacy body until this turns on for it.+ALERTMANAGER_WEBHOOK_BODY_FLAG: Final = "alert-platform-alertmanager-webhook-body"+# The header value the HogFunction path sends with its body.LEGACY_WEBHOOK_VERSION: Final = "1"@@ -36,15 +39,14 @@ def body_for(source: SourceKind) -> LegacyWebhookBody | None:def keeps_legacy_webhook_body(team: Team) -> bool | None:- """Whether native delivery sends this team the body its source's own stack sent. None when unknown."""- return team_flag_enabled(LEGACY_WEBHOOK_BODY_FLAG, team)---def native_sends_legacy_body(source: SourceKind, team: Team) -> bool | None:- """Whether native delivery sends this team the webhook body its source's HogFunction sent. None when unknown.+ """Whether native delivery sends this team the body its source's own stack sent. None when unknown.- A source that registered no body gets the platform body natively, whatever the flag says.+ The gate is read before the list. A flag refresh can land between the two reads, and the list is+ always topped up before the gate turns on, so a gate read from the older definitions is either+ off, which keeps the legacy body, or already sees the topped-up list."""- if body_for(source) is None:- return False- return keeps_legacy_webhook_body(team)+ gets_alertmanager_body = team_flag_enabled(ALERTMANAGER_WEBHOOK_BODY_FLAG, team)+ listed = team_flag_enabled(LEGACY_WEBHOOK_BODY_FLAG, team)+ if listed is None or listed:+ return listed+ return None if gets_alertmanager_body is None else not gets_alertmanager_bodydiff --git a/products/alerts_platform/backend/facade/delivery.py b/products/alerts_platform/backend/facade/delivery.pyindex 18b84e72b56e..a0c61f3168bb 100644--- a/products/alerts_platform/backend/facade/delivery.py+++ b/products/alerts_platform/backend/facade/delivery.py@@ -75,9 +75,9 @@ def has_legacy_event_emitter(source: SourceKind) -> bool:return delivery_paths.emitter_for(source) is not None-def native_sends_legacy_webhook_body(source: SourceKind, team: Team) -> bool | None:- """Whether a stock webhook receives the body its HogFunction sent when delivered natively. None when unknown."""- return legacy_webhook.native_sends_legacy_body(source, team)+def has_legacy_webhook_body(source: SourceKind) -> bool:+ """Whether native delivery can send the source's legacy webhook body to the teams that keep it."""+ return legacy_webhook.body_for(source) is not Nonedef register_source_slack_renderer(source: SourceKind, renderer: SourceSlackRenderer) -> None:diff --git a/products/alerts_platform/backend/tests/test_delivery_evaluation.py b/products/alerts_platform/backend/tests/test_delivery_evaluation.pyindex 56dc8d35d138..bac64995fee5 100644--- a/products/alerts_platform/backend/tests/test_delivery_evaluation.py+++ b/products/alerts_platform/backend/tests/test_delivery_evaluation.py@@ -15,7 +15,10 @@ from posthog.utils import absolute_urifrom 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 deliver_evaluation-from products.alerts_platform.backend.delivery.legacy_webhook import LEGACY_WEBHOOK_BODY_FLAG+from products.alerts_platform.backend.delivery.legacy_webhook import (+ ALERTMANAGER_WEBHOOK_BODY_FLAG,+ LEGACY_WEBHOOK_BODY_FLAG,+)from products.alerts_platform.backend.delivery.message import AlertMessagefrom products.alerts_platform.backend.delivery.thread_store import ThreadBusyfrom products.alerts_platform.backend.delivery.transport import DeliveryError, MessageHandle@@ -579,23 +582,32 @@ class TestDeliverEvaluation(APIBaseTest):@parameterized.expand([- ("keeps_the_legacy_body", True, "1", "legacy"),- ("moved_to_alertmanager", False, "2", "4"),- ("flag_unreadable", RuntimeError("flags unreachable"), None, None),+ ("keeps_the_legacy_body", True, True, "1", "legacy"),+ ("moved_to_alertmanager", False, True, "2", "4"),+ ("alertmanager_body_not_turned_on", False, False, "1", "legacy"),+ ("flag_unreadable", RuntimeError("flags unreachable"), True, None, None),# The SDK swallows a failed lookup and answers None.- ("flag_lookup_swallowed", None, None, None),+ ("flag_lookup_swallowed", None, True, None, None),+ ("alertmanager_flag_lookup_swallowed", False, None, None, None),])def test_a_webhook_gets_the_body_its_team_still_receives(- self, _name: str, keeps_legacy: bool | Exception | None, header: str | None, body_version: str | None+ self,+ _name: str,+ listed: bool | Exception | None,+ alertmanager_body: bool | None,+ header: str | None,+ body_version: str | None,) -> None:flag_reads: list[str] = []def flag(key: str, *_args: Any, **_kwargs: Any) -> bool | None:flag_reads.append(key)- if isinstance(keeps_legacy, Exception):- raise keeps_legacy- return keeps_legacy+ if key == ALERTMANAGER_WEBHOOK_BODY_FLAG:+ return alertmanager_body+ if isinstance(listed, Exception):+ raise listed+ return listeddef legacy_body(**kwargs: Any) -> dict[str, Any]:return {"version": "legacy", "alert_id": kwargs["alert_id"]}@@ -628,19 +640,19 @@ class TestDeliverEvaluation(APIBaseTest):assert post.call_args.kwargs["headers"] == {"X-PostHog-Webhook-Version": header}assert post.call_args.args[1]["version"] == body_version- @parameterized.expand([("registered_a_body", True, True), ("registered_none", False, False)])- def test_native_delivery_keeps_the_webhook_body_only_for_a_source_that_registered_one(- self, _name: str, registered: bool, keeps: bool- ) -> None:- bodies: dict[SourceKind, Any] = {SourceKind.INSIGHT: lambda **_kwargs: {}} if registered else {}- with (- patch.dict(f"{_LEGACY_MODULE}._bodies", bodies, clear=True),- patch(- "products.alerts_platform.backend.delivery.team_flags.posthoganalytics.feature_enabled",- return_value=True,- ),+ def test_a_flag_refresh_between_the_two_reads_keeps_a_newly_listed_team_on_the_legacy_body(self) -> None:+ before = {LEGACY_WEBHOOK_BODY_FLAG: False, ALERTMANAGER_WEBHOOK_BODY_FLAG: False}+ after = {LEGACY_WEBHOOK_BODY_FLAG: True, ALERTMANAGER_WEBHOOK_BODY_FLAG: True}+ reads: list[str] = []++ def flag(key: str, *_args: Any, **_kwargs: Any) -> bool:+ reads.append(key)+ return (before if len(reads) == 1 else after)[key]++ with patch(+ "products.alerts_platform.backend.delivery.team_flags.posthoganalytics.feature_enabled", side_effect=flag):- assert legacy_webhook.native_sends_legacy_body(SourceKind.INSIGHT, self.team) is keeps+ assert legacy_webhook.keeps_legacy_webhook_body(self.team) is Truedef test_logs_registers_its_legacy_webhook_body_at_startup(self) -> None:# Every other case here patches the registry, so none of them notices a missing registration,diff --git a/products/logs/backend/test/test_platform_cutover.py b/products/logs/backend/test/test_platform_cutover.pyindex 1d4e5d2b274d..bcbe720512cf 100644--- a/products/logs/backend/test/test_platform_cutover.py+++ b/products/logs/backend/test/test_platform_cutover.py@@ -305,14 +305,6 @@ class TestPlatformCutover(APIBaseTest):True,HogFunctionPathReason.CUSTOMIZED,),- (- "stock_webhook_without_legacy_body",- "template-webhook",- WEBHOOK_INPUTS,- {},- False,- HogFunctionPathReason.NEW_WEBHOOK_BODY,- ),("stock_webhook_with_unreadable_flag","template-webhook",@@ -397,17 +389,18 @@ class TestPlatformCutover(APIBaseTest):@parameterized.expand([- ("stock_slack", "template-slack", SLACK_INPUTS),- ("stock_webhook_with_legacy_body", "template-webhook", WEBHOOK_INPUTS),+ ("stock_slack", "template-slack", SLACK_INPUTS, True),+ ("stock_webhook_with_legacy_body", "template-webhook", WEBHOOK_INPUTS, True),+ ("stock_webhook_off_the_legacy_list", "template-webhook", WEBHOOK_INPUTS, False),])def test_a_team_moves_when_every_destination_is_ready(- self, _name: str, template_id: str, inputs: dict[str, Any]+ self, _name: str, template_id: str, inputs: dict[str, Any], keeps_legacy_body: bool) -> None:alert = self._alert()self._destination(alert.id, template_id, inputs)- with patch(LEGACY_BODY_FLAG, return_value=True):+ with patch(LEGACY_BODY_FLAG, return_value=keeps_legacy_body):paths = team_delivery_paths(self.team.id)(result,) = cut_over_team(self.team.id, now=NOW)