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.

feat(alerts): count slack refusals a customer must fix apart from delivery failures (#126138)

Jon McCallumc7cc0b75a50b
diff --git a/products/alerts_platform/backend/delivery/dispatch.py b/products/alerts_platform/backend/delivery/dispatch.py
index 1052318a2a5f..dae30c53d2e1 100644
--- a/products/alerts_platform/backend/delivery/dispatch.py
+++ b/products/alerts_platform/backend/delivery/dispatch.py
@@ -18,6 +18,7 @@ from products.alerts_platform.backend.delivery.thread_store import ThreadBusy, T
from products.alerts_platform.backend.delivery.transport import (
DeliveryError,
DeliveryTransport,
+ DestinationMisconfigured,
MessageHandle,
RootEditor,
)
@@ -31,6 +32,7 @@ from products.alerts_platform.backend.facade.contracts import (
)
from products.alerts_platform.backend.logic.platform_reads import alert_snapshot
from products.alerts_platform.backend.temporal.metrics import (
+ DeliveryOutcomeLabel,
increment_deliveries,
increment_root_edit_failures,
record_delivery_lag,
@@ -141,7 +143,8 @@ def _send(
succeeded=False,
error=str(error) if isinstance(error, DeliveryError) else type(error).__name__,
)
- safe_record(increment_deliveries, message.source.value, transport.provider, DeliveryPath.NATIVE.value, "failed")
+ outcome: DeliveryOutcomeLabel = "misconfigured" if isinstance(error, DestinationMisconfigured) else "failed"
+ safe_record(increment_deliveries, message.source.value, transport.provider, DeliveryPath.NATIVE.value, outcome)
raise
record_delivery(
team_id=team_id,
diff --git a/products/alerts_platform/backend/delivery/slack.py b/products/alerts_platform/backend/delivery/slack.py
index 1dcead578150..716cbc095a12 100644
--- a/products/alerts_platform/backend/delivery/slack.py
+++ b/products/alerts_platform/backend/delivery/slack.py
@@ -27,7 +27,7 @@ from posthog.slack.formatting import escape_slack_mrkdwn
from products.alerts_platform.backend.delivery.describers import slack_renderer_for
from products.alerts_platform.backend.delivery.message import UNTRACKED_GROUPS_LABEL, AlertMessage
-from products.alerts_platform.backend.delivery.transport import DeliveryError, MessageHandle
+from products.alerts_platform.backend.delivery.transport import DeliveryError, DestinationMisconfigured, MessageHandle
from products.alerts_platform.backend.facade.contracts import (
AlertDestinationData,
AlertEventKind,
@@ -161,6 +161,11 @@ def _without_image_content(content: SlackMessageContent) -> SlackMessageContent:
)
+# Only codes a customer's own setup causes. A code a platform bug can also cause, such as
+# `missing_scope` or `channel_not_found`, stays a failure so the platform's alert still sees it.
+_MISCONFIGURED_ERRORS: Final = frozenset({"account_inactive", "not_in_channel"})
+
+
def _refused_image(error: SlackApiError, content: SlackMessageContent) -> bool:
return error.response.get("error") == IMAGE_REFUSAL and _without_image_content(content) != content
@@ -222,9 +227,9 @@ class SlackTransport:
content = _without_image_content(content)
response = post(content)
except SlackApiError as error:
- # A refusal a person can fix, such as a channel the bot has left, rather than a
- # transport failure a retry would clear.
- raise DeliveryError(f"Slack refused the message: {error.response.get('error')}") from error
+ code = error.response.get("error")
+ refusal = DestinationMisconfigured if code in _MISCONFIGURED_ERRORS else DeliveryError
+ raise refusal(f"Slack refused the message: {code}") from error
timestamp = response.get("ts")
if not timestamp:
raise DeliveryError("Slack accepted the message but returned no timestamp to reply to.")
diff --git a/products/alerts_platform/backend/delivery/transport.py b/products/alerts_platform/backend/delivery/transport.py
index 4a33b2a21ad9..c5fa2345ffb0 100644
--- a/products/alerts_platform/backend/delivery/transport.py
+++ b/products/alerts_platform/backend/delivery/transport.py
@@ -32,6 +32,13 @@ class DeliveryError(Exception):
"""A send the provider refused. The caller decides whether to retry."""
+class DestinationMisconfigured(DeliveryError):
+ """A refusal that only the destination's owner can fix, such as a channel the bot is not in.
+
+ The platform did its part, so its own reliability metrics count this apart from a failure.
+ """
+
+
class DeliveryTransport(Protocol):
provider: str
diff --git a/products/alerts_platform/backend/temporal/metrics.py b/products/alerts_platform/backend/temporal/metrics.py
index 54c81e218024..4f7f84c8ca17 100644
--- a/products/alerts_platform/backend/temporal/metrics.py
+++ b/products/alerts_platform/backend/temporal/metrics.py
@@ -262,7 +262,7 @@ def record_batch_duration(source: str, duration_ms: int) -> None:
).record(dt.timedelta(milliseconds=duration_ms))
-DeliveryOutcomeLabel = Literal["sent", "failed", "already_delivered", "busy", "no_transport"]
+DeliveryOutcomeLabel = Literal["sent", "failed", "misconfigured", "already_delivered", "busy", "no_transport"]
_DELIVERIES_DESCRIPTION = "Transitions a delivery handled, by provider, delivery path and outcome"
_DELIVERY_LAG_DESCRIPTION = "Delay between the check that decided a transition and the send that announced it"
@@ -292,7 +292,8 @@ def increment_deliveries(source: str, provider: str, path: str, outcome: Deliver
`already_delivered` is a retry finding its send done, and `busy` a send that waits on another
one holding the thread. Neither sent anything, so a rising `sent` with a flat `failed` is the
- only reading that says alerts reach people.
+ only reading that says alerts reach people. `misconfigured` is a refusal only the destination's
+ owner can fix, so it stays out of `failed`, which measures the platform.
"""
labels: Attributes = {"source": source, "provider": provider, "path": path, "outcome": outcome}
try:
diff --git a/products/alerts_platform/backend/tests/test_delivery_dispatch.py b/products/alerts_platform/backend/tests/test_delivery_dispatch.py
index 5565be424256..80ed29f5e758 100644
--- a/products/alerts_platform/backend/tests/test_delivery_dispatch.py
+++ b/products/alerts_platform/backend/tests/test_delivery_dispatch.py
@@ -15,7 +15,7 @@ from products.alerts_platform.backend.delivery.dispatch import deliver
from products.alerts_platform.backend.delivery.message import AlertMessage
from products.alerts_platform.backend.delivery.telemetry import record_delivery
from products.alerts_platform.backend.delivery.thread_store import NullThreadStore, ThreadClaim, ThreadKey
-from products.alerts_platform.backend.delivery.transport import DeliveryError, MessageHandle
+from products.alerts_platform.backend.delivery.transport import DeliveryError, DestinationMisconfigured, MessageHandle
from products.alerts_platform.backend.facade.contracts import (
AlertDestinationData,
AlertEventKind,
@@ -304,14 +304,22 @@ class TestDeliveryDispatch(SimpleTestCase):
== lag_before + 1
)
- def test_a_provider_outage_is_counted_as_a_failed_delivery(self) -> None:
- transport = FakeTransport(error=ConnectionError("slack unreachable"))
+ @parameterized.expand(
+ [
+ ("outage", ConnectionError("slack unreachable"), "ConnectionError", "failed"),
+ ("misconfigured", DestinationMisconfigured("not_in_channel"), "not_in_channel", "misconfigured"),
+ ]
+ )
+ def test_a_failed_send_is_counted_by_whose_fix_it_is(
+ self, _name: str, error: Exception, logged: str, outcome: str
+ ) -> None:
+ transport = FakeTransport(error=error)
with (
patch("products.alerts_platform.backend.delivery.dispatch.record_delivery") as recorded,
recorded_metrics() as metrics,
):
- with pytest.raises(ConnectionError):
+ with pytest.raises(type(error)):
deliver(
transport=transport,
thread_store=NullThreadStore(),
@@ -324,9 +332,9 @@ class TestDeliveryDispatch(SimpleTestCase):
)
assert recorded.call_args.kwargs["succeeded"] is False
- # Its text can carry a credential URL, so only the class reaches the person's log.
- assert recorded.call_args.kwargs["error"] == "ConnectionError"
- assert [m.attributes["outcome"] for m in metrics if m.name == "alerts_platform_deliveries_total"] == ["failed"]
+ # An outage's text can carry a credential URL, so only its class reaches the person's log.
+ assert recorded.call_args.kwargs["error"] == logged
+ assert [m.attributes["outcome"] for m in metrics if m.name == "alerts_platform_deliveries_total"] == [outcome]
def test_a_store_that_remembers_nothing_still_delivers(self) -> None:
transport = FakeTransport()
diff --git a/products/alerts_platform/backend/tests/test_delivery_slack.py b/products/alerts_platform/backend/tests/test_delivery_slack.py
index 906a5a9254cb..6db6d15fcc3e 100644
--- a/products/alerts_platform/backend/tests/test_delivery_slack.py
+++ b/products/alerts_platform/backend/tests/test_delivery_slack.py
@@ -15,7 +15,7 @@ from posthog.slack.channels import MAX_HEADER_CHARS, MAX_SECTION_CHARS
from products.alerts_platform.backend.delivery.message import UNTRACKED_GROUPS_LABEL
from products.alerts_platform.backend.delivery.slack import SlackTransport, blocks_for, content_for
-from products.alerts_platform.backend.delivery.transport import DeliveryError, MessageHandle
+from products.alerts_platform.backend.delivery.transport import DeliveryError, DestinationMisconfigured, MessageHandle
from products.alerts_platform.backend.facade.contracts import (
AlertDestinationData,
AlertEventKind,
@@ -195,14 +195,24 @@ class TestSlackTransport(APIBaseTest):
with pytest.raises(DeliveryError):
self._send(team_id=self.team.id + 1)
- def test_a_refusal_from_slack_becomes_a_delivery_error(self) -> None:
+ @parameterized.expand(
+ [
+ ("not_in_channel", True),
+ ("account_inactive", True),
+ ("channel_not_found", False),
+ ("missing_scope", False),
+ ("ratelimited", False),
+ ("internal_error", False),
+ ]
+ )
+ def test_a_refusal_from_slack_becomes_a_delivery_error(self, code: str, misconfigured: bool) -> None:
with patch("products.alerts_platform.backend.delivery.slack.SlackIntegration") as slack_integration:
- slack_integration.return_value.client.chat_postMessage.side_effect = SlackApiError(
- "channel_not_found", {"error": "channel_not_found"}
- )
- with pytest.raises(DeliveryError):
+ slack_integration.return_value.client.chat_postMessage.side_effect = SlackApiError(code, {"error": code})
+ with pytest.raises(DeliveryError) as refused:
SlackTransport().deliver(team_id=self.team.id, target=self.target, message=MESSAGE)
+ assert isinstance(refused.value, DestinationMisconfigured) is misconfigured
+
def test_a_message_whose_image_slack_cannot_fetch_goes_again_without_it(self) -> None:
message = alert_message(image=MessageImage(url="https://app.example.com/chart.png", alt_text="Insight chart"))
with patch("products.alerts_platform.backend.delivery.slack.SlackIntegration") as slack_integration: