: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): offer the snooze menu on a native insight Slack message (#120603)
Jimmy Zhu2d3d525bbd9c
diff --git a/products/alerts/backend/logic/insight_alert_destinations.py b/products/alerts/backend/logic/insight_alert_destinations.pyindex 29a50572db14..693d2ab157ca 100644--- a/products/alerts/backend/logic/insight_alert_destinations.py+++ b/products/alerts/backend/logic/insight_alert_destinations.py@@ -48,6 +48,22 @@ _SNOOZE_OPTIONS: Final = ()+def snooze_select(alert_id: str) -> dict[str, Any]:+ """The snooze menu of an insight alert's Slack message, which the Slack app's snooze handler reads.++ The stock template passes a Hog placeholder for the id, and a native message passes the id itself.+ """+ return {+ "action_id": "insight_alert_snooze",+ "placeholder": {"text": "Snooze…", "type": "plain_text"},+ "options": [+ {"text": {"text": label, "type": "plain_text"}, "value": f"{alert_id}|{duration}"}+ for label, duration in _SNOOZE_OPTIONS+ ],+ "type": "static_select",+ }++def _slack_blocks() -> list[Any]:return [{@@ -87,18 +103,7 @@ def _slack_blocks() -> list[Any]:"text": {"text": "View Insight", "type": "plain_text"},"type": "button",},- {- "action_id": "insight_alert_snooze",- "placeholder": {"text": "Snooze…", "type": "plain_text"},- "options": [- {- "text": {"text": label, "type": "plain_text"},- "value": "{event.properties.alert_id}|" + duration,- }- for label, duration in _SNOOZE_OPTIONS- ],- "type": "static_select",- },+ snooze_select("{event.properties.alert_id}"),],},]diff --git a/products/alerts/backend/platform_alert_wording.py b/products/alerts/backend/platform_alert_wording.pyindex 59c99add749f..574c0ef57041 100644--- a/products/alerts/backend/platform_alert_wording.py+++ b/products/alerts/backend/platform_alert_wording.py@@ -11,6 +11,7 @@ from __future__ import annotationsfrom posthog.utils import absolute_uri+from products.alerts.backend.logic.insight_alert_destinations import snooze_selectfrom products.alerts_platform.backend.facade.contracts import (AlertEventKind,AnnouncedTransition,@@ -22,7 +23,10 @@ from products.alerts_platform.backend.facade.contracts import (def describe_insight_transition(*, project_id: int, transition: AnnouncedTransition) -> SourceDescription:detail = transition.source_detail- breaches = (detail.get("breaches") or []) if transition.kind == AlertEventKind.FIRING else []+ # A failure keeps the platform's own wording, so only a firing states breaches or offers a snooze.+ firing = transition.kind == AlertEventKind.FIRING+ breaches = (detail.get("breaches") or []) if firing else []+ alert_id = detail.get("alert_id") if firing else Noneinsight_name = detail.get("insight_name")insight_short_id = detail.get("insight_short_id")return SourceDescription(@@ -32,6 +36,7 @@ def describe_insight_transition(*, project_id: int, transition: AnnouncedTransitdata_link=MessageLink(label="View insight", url=insight_url(project_id, insight_short_id))if insight_short_idelse None,+ slack_elements=(snooze_select(alert_id),) if alert_id else (),)diff --git a/products/alerts/backend/platform_source_cycle.py b/products/alerts/backend/platform_source_cycle.pyindex ffb00e791528..95b694dee9f7 100644--- a/products/alerts/backend/platform_source_cycle.py+++ b/products/alerts/backend/platform_source_cycle.py@@ -725,6 +725,8 @@ def _detail(alert: AlertConfiguration, breaches: Sequence[str], chart: RenderedIdetail: dict[str, Any] = {"breaches": [_clipped(breach) for breach in breaches[:MAX_DETAIL_BREACHES]],**_context(alert),+ # The legacy alert's id, which a native message's snooze menu snoozes.+ "alert_id": str(alert.id),}if chart is not None:# The ids rather than a URL, because the URL opens the chart without a login and history keepsdiff --git a/products/alerts/backend/test/test_platform_alert_wording.py b/products/alerts/backend/test/test_platform_alert_wording.pyindex f426752f556c..1f8cb0996e55 100644--- a/products/alerts/backend/test/test_platform_alert_wording.py+++ b/products/alerts/backend/test/test_platform_alert_wording.py@@ -7,6 +7,7 @@ from parameterized import parameterizedfrom posthog.utils import absolute_uri+from products.alerts.backend.logic.insight_alert_destinations import snooze_selectfrom products.alerts.backend.platform_alert_wording import describe_insight_transitionfrom products.alerts_platform.backend.facade.contracts import (AlertEventKind,@@ -22,6 +23,7 @@ DETAIL: dict[str, Any] = {"insight_short_id": "abc123","insight_chart_asset_id": 7,"insight_chart_render_user_id": 3,+ "alert_id": "01900000-0000-0000-0000-000000000001",}LINK = MessageLink(label="View insight", url=absolute_uri("/project/7/insights/abc123"))@@ -37,6 +39,7 @@ class TestDescribeInsightTransition(SimpleTestCase):details=(MessageDetail(label="Threshold breached", value=DETAIL["breaches"][0]),),context=("Insight: Signups",),data_link=LINK,+ slack_elements=(snooze_select("01900000-0000-0000-0000-000000000001"),),),),(@@ -49,6 +52,7 @@ class TestDescribeInsightTransition(SimpleTestCase):),context=("Insight: Signups",),data_link=LINK,+ slack_elements=(snooze_select("01900000-0000-0000-0000-000000000001"),),),),# The platform states a failure in its own words, so only the insight's name and link are added.diff --git a/products/alerts/backend/test/test_platform_source_cycle.py b/products/alerts/backend/test/test_platform_source_cycle.pyindex eb5ef3b3c057..2117dfd583a8 100644--- a/products/alerts/backend/test/test_platform_source_cycle.py+++ b/products/alerts/backend/test/test_platform_source_cycle.py@@ -206,6 +206,7 @@ class TestPlatformInsightEvaluation(APIBaseTest):"breaches": ["above 100"],"insight_name": alert.insight.name,"insight_short_id": alert.insight.short_id,+ "alert_id": str(alert.id),**chart,}diff --git a/products/alerts_platform/backend/delivery/describers.py b/products/alerts_platform/backend/delivery/describers.pyindex f9d2c2cf93cc..e82eeee66df0 100644--- a/products/alerts_platform/backend/delivery/describers.py+++ b/products/alerts_platform/backend/delivery/describers.py@@ -30,6 +30,7 @@ MAX_DATA_LINK_URL_CHARS: Final = 2000# Its alt text has a 2000-character limit, which a clip keeps the image inside.MAX_IMAGE_URL_CHARS: Final = 3000MAX_IMAGE_ALT_TEXT_CHARS: Final = 2000+MAX_SLACK_ELEMENTS: Final = 3logger = structlog.get_logger(__name__)@@ -82,6 +83,8 @@ def _bounded(source: SourceKind, description: SourceDescription) -> SourceDescridetails=description.details,context=tuple(clip_text(line, MAX_CONTEXT_LINE_CHARS) for line in description.context[:MAX_CONTEXT_LINES]),data_link=data_link,+ # The buttons share the actions block, which Slack caps at 25 elements.+ slack_elements=description.slack_elements[:MAX_SLACK_ELEMENTS],)diff --git a/products/alerts_platform/backend/delivery/message.py b/products/alerts_platform/backend/delivery/message.pyindex d83140fbf9e4..d89d47e1d3c9 100644--- a/products/alerts_platform/backend/delivery/message.py+++ b/products/alerts_platform/backend/delivery/message.py@@ -72,6 +72,7 @@ class AlertMessage:context: tuple[str, ...] = ()data_link: MessageLink | None = Noneimage: MessageImage | None = None+ slack_elements: tuple[dict[str, Any], ...] = ()incident_action: IncidentAction | None = None@property@@ -202,5 +203,6 @@ def build_message(context=description.context,data_link=description.data_link,image=resolve_image(announcement.source, team_id=team_id, transition=transition),+ slack_elements=description.slack_elements,incident_action=incident_action,)diff --git a/products/alerts_platform/backend/delivery/slack.py b/products/alerts_platform/backend/delivery/slack.pyindex ea79d0b23e8c..d9c5f2eac06c 100644--- a/products/alerts_platform/backend/delivery/slack.py+++ b/products/alerts_platform/backend/delivery/slack.py@@ -69,7 +69,9 @@ def blocks_for(message: AlertMessage) -> list[dict[str, Any]]:blocks.append(context_block(clip_text(context, MAX_CONTEXT_CHARS)))if message.image is not None:blocks.append(image_block(message.image.url, message.image.alt_text))- blocks.append(actions_block([SlackButton(text=link.label, url=link.url) for link in message.links]))+ actions = actions_block([SlackButton(text=link.label, url=link.url) for link in message.links])+ actions["elements"].extend(message.slack_elements)+ blocks.append(actions)return blocksdiff --git a/products/alerts_platform/backend/facade/contracts.py b/products/alerts_platform/backend/facade/contracts.pyindex bc9dace05324..848edb4eec26 100644--- a/products/alerts_platform/backend/facade/contracts.py+++ b/products/alerts_platform/backend/facade/contracts.py@@ -1268,6 +1268,9 @@ class SourceDescription:context: tuple[str, ...] = ()# Where the data behind the alert is, such as the matching logs.data_link: MessageLink | None = None+ # Slack elements that act on the alert from the message, such as a snooze menu, beside the+ # buttons. They are Slack's own Block Kit, so every other provider leaves them out.+ slack_elements: tuple[dict[str, Any], ...] = ()class SourceDescriber(Protocol):diff --git a/products/alerts_platform/backend/tests/test_delivery_slack.py b/products/alerts_platform/backend/tests/test_delivery_slack.pyindex 68593521c4f6..c8110b8f3dea 100644--- a/products/alerts_platform/backend/tests/test_delivery_slack.py+++ b/products/alerts_platform/backend/tests/test_delivery_slack.py@@ -1,3 +1,4 @@+from dataclasses import replacefrom typing import Any, castimport pytest@@ -101,6 +102,14 @@ class TestSlackBlocks(SimpleTestCase):"alt_text": "Insight chart",}+ def test_a_source_slack_element_follows_the_buttons_in_their_block(self) -> None:+ menu = {"type": "static_select", "action_id": "snooze"}++ actions = blocks_for(replace(alert_message(), slack_elements=(menu,)))[-1]++ assert [element["type"] for element in actions["elements"]] == ["button", menu["type"]]+ assert actions["elements"][-1] is menu+def test_the_source_data_link_comes_before_the_alert_link(self) -> None:data_url = "https://app.example.com/project/1/logs"message = alert_message(data_link=MessageLink(label="View logs", url=data_url))