Skip to content

: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.

fix(alerts): keep one failing destination from blocking the others (#112186)

Jimmy Zhu5d280e750314
diff --git a/products/alerts_platform/backend/delivery/evaluation.py b/products/alerts_platform/backend/delivery/evaluation.py
index 0d4b1fe2b8d4..471d2855d5c7 100644
--- a/products/alerts_platform/backend/delivery/evaluation.py
+++ b/products/alerts_platform/backend/delivery/evaluation.py
@@ -17,7 +17,7 @@ from products.alerts_platform.backend.delivery.destinations import list_alert_de
from products.alerts_platform.backend.delivery.dispatch import deliver
from products.alerts_platform.backend.delivery.slack import SlackTransport
from products.alerts_platform.backend.delivery.thread_store import DatabaseThreadStore, ThreadBusy
-from products.alerts_platform.backend.delivery.transport import DeliveryTransport
+from products.alerts_platform.backend.delivery.transport import DeliveryError, DeliveryTransport
from products.alerts_platform.backend.facade.contracts import (
AlertDeliveryRequest,
AlertDestinationData,
@@ -94,6 +94,8 @@ def deliver_evaluation(request: AlertDeliveryRequest) -> DeliveryOutcome:
sent = 0
skipped = 0
busy: list[str] = []
+ failures: list[str] = []
+ first_refusal: DeliveryError | None = None
# Per subscription rather than per destination. A destination subscribes to some of the
# kinds an alert can announce, so one that asked for firings must not be handed the resolve
# that another group produced in the same evaluation.
@@ -115,10 +117,25 @@ def deliver_evaluation(request: AlertDeliveryRequest) -> DeliveryOutcome:
)
except ThreadBusy as error:
busy.append(str(error))
+ # A destination that refuses every send, such as a deleted channel, must not cost the
+ # destinations after it their message on every attempt. Temporal records the message
+ # and the cause chain, and a transport words its own refusals safely. Any other
+ # exception's text can carry a credential URL, so it is named by class and not chained.
+ except DeliveryError as error:
+ failures.append(str(error))
+ first_refusal = first_refusal or error
+ except Exception as error:
+ failures.append(type(error).__name__)
else:
sent += 1
+ # A held thread wins over a failure. The workflow waits a held thread out and then runs the
+ # whole delivery again, which retries the failed destinations too. A failure raised instead
+ # spends the activity's retries inside the claim's TTL, and the held message is lost.
if busy:
- raise ThreadBusy("; ".join(busy))
+ # The failures ride along, so a wait that ends still says which destinations refused.
+ raise ThreadBusy("; ".join([*busy, *failures]))
+ if failures:
+ raise DeliveryError(f"{len(failures)} destination(s) failed: " + "; ".join(failures)) from first_refusal
return DeliveryOutcome(live=True, sent=sent, skipped_without_transport=skipped)
diff --git a/products/alerts_platform/backend/tests/test_delivery_evaluation.py b/products/alerts_platform/backend/tests/test_delivery_evaluation.py
index 156b61dfefbe..d80716a2372f 100644
--- a/products/alerts_platform/backend/tests/test_delivery_evaluation.py
+++ b/products/alerts_platform/backend/tests/test_delivery_evaluation.py
@@ -1,13 +1,17 @@
from datetime import UTC, datetime
from typing import Any, cast
+import pytest
from posthog.test.base import APIBaseTest
from unittest.mock import patch
+from parameterized import parameterized
+
from products.alerts_platform.backend.delivery.destinations import list_alert_destination_groups
from products.alerts_platform.backend.delivery.evaluation import LIVE_DELIVERY_FLAG, deliver_evaluation
from products.alerts_platform.backend.delivery.message import AlertMessage
-from products.alerts_platform.backend.delivery.transport import MessageHandle
+from products.alerts_platform.backend.delivery.thread_store import ThreadBusy
+from products.alerts_platform.backend.delivery.transport import DeliveryError, MessageHandle
from products.alerts_platform.backend.facade.contracts import (
AlertDeliveryRequest,
AlertDestinationData,
@@ -27,6 +31,7 @@ RESOLVED_EVENT = "$logs_alert_resolved"
SLACK = cast(AlertDestinationData, {"type": DestinationType.SLACK, "slack_workspace_id": 1, "slack_channel_id": "C-1"})
WEBHOOK = cast(AlertDestinationData, {"type": DestinationType.WEBHOOK, "webhook_url": "https://example.com/hook"})
+TEAMS = cast(AlertDestinationData, {"type": DestinationType.TEAMS, "webhook_url": "https://example.com/teams"})
class RecordingTransport:
@@ -45,6 +50,20 @@ class RecordingTransport:
return MessageHandle(external_ref={"channel": self.channel_target(target), "ts": "1"})
+class RefusingTransport(RecordingTransport):
+ provider = "webhook"
+
+ def deliver(self, **kwargs: Any) -> MessageHandle | None:
+ raise DeliveryError("The webhook destination refused the message with status 410.")
+
+
+class HeldTransport(RecordingTransport):
+ provider = "teams"
+
+ def deliver(self, **kwargs: Any) -> MessageHandle | None:
+ raise ThreadBusy("thread is being posted to by another send")
+
+
def _request(team_id: int) -> AlertDeliveryRequest:
return AlertDeliveryRequest(
source=SourceKind.LOGS,
@@ -82,7 +101,13 @@ class TestDeliverEvaluation(APIBaseTest):
super().setUp()
RecordingTransport.sends = []
- def _run(self, announced: Any, by_event: dict[str, list[AlertDestinationGroup]], live: bool = True) -> Any:
+ def _run(
+ self,
+ announced: Any,
+ by_event: dict[str, list[AlertDestinationGroup]],
+ live: bool = True,
+ transports: dict[DestinationType, type] | None = None,
+ ) -> Any:
def groups(*, team_id: int, alert_id: str, allowed_event_ids: list[str]) -> list[AlertDestinationGroup]:
return by_event.get(allowed_event_ids[0], [])
@@ -91,7 +116,7 @@ class TestDeliverEvaluation(APIBaseTest):
patch(f"{_MODULE}.announcement", return_value=announced),
patch(f"{_MODULE}.list_alert_destination_groups", side_effect=groups),
patch(f"{_MODULE}.DatabaseThreadStore"),
- patch.dict(f"{_MODULE}._TRANSPORTS", {DestinationType.SLACK: RecordingTransport}),
+ patch.dict(f"{_MODULE}._TRANSPORTS", {DestinationType.SLACK: RecordingTransport, **(transports or {})}),
):
return deliver_evaluation(_request(self.team.id))
@@ -134,6 +159,24 @@ class TestDeliverEvaluation(APIBaseTest):
assert [channel for channel, _ in RecordingTransport.sends] == ["C-1"]
assert (outcome.sent, outcome.skipped_without_transport) == (1, 1)
+ @parameterized.expand(
+ [
+ ("a_refusal_is_raised_after_the_rest_are_sent", DeliveryError, [WEBHOOK, SLACK]),
+ ("a_held_thread_wins_over_a_refusal", ThreadBusy, [WEBHOOK, TEAMS, SLACK]),
+ ]
+ )
+ def test_one_destination_failing_does_not_cost_the_others_their_message(
+ self, _name: str, raised: type[Exception], destinations: list[AlertDestinationData]
+ ) -> None:
+ with pytest.raises(raised, match="status 410"):
+ self._run(
+ _announcement(_transition(AlertEventKind.FIRING)),
+ {FIRING_EVENT: [_group(destination) for destination in destinations]},
+ transports={DestinationType.WEBHOOK: RefusingTransport, DestinationType.TEAMS: HeldTransport},
+ )
+
+ assert [channel for channel, _ in RecordingTransport.sends] == ["C-1"]
+
def test_a_destination_that_is_not_fully_enabled_receives_nothing(self) -> None:
outcome = self._run(
_announcement(_transition(AlertEventKind.FIRING)),