: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(replay-vision): link each match in Slack alerts to its observation (#122351)
Kim Svatos Dugan1c6494c4f7ba
diff --git a/products/replay_vision/backend/alert_utils.py b/products/replay_vision/backend/alert_utils.pyindex 67a3b991dcca..37903f880837 100644--- a/products/replay_vision/backend/alert_utils.py+++ b/products/replay_vision/backend/alert_utils.py@@ -7,6 +7,8 @@ from django.db.models import Qimport structlog+from posthog.utils import absolute_uri+from products.alerts_platform.backend.facade.scheduling import (advance_next_check_at,compute_shard_offset_seconds as shared_compute_shard_offset_seconds,@@ -22,6 +24,7 @@ __all__ = ["compute_shard_offset_seconds","due_alerts_q","next_allowed_check_at",+ "observation_url",]# How often the Temporal schedule fires. Drives shard granularity in@@ -37,6 +40,10 @@ def compute_shard_offset_seconds(alert_id: UUID, check_interval_minutes: int) ->)+def observation_url(team_id: int, observation_id: str) -> str:+ return absolute_uri(f"/project/{team_id}/replay-vision/observations/{observation_id}")++def next_allowed_check_at(candidate: datetime,*,diff --git a/products/replay_vision/backend/temporal/vision_alerts/activities.py b/products/replay_vision/backend/temporal/vision_alerts/activities.pyindex 810909704ff5..522e6538f263 100644--- a/products/replay_vision/backend/temporal/vision_alerts/activities.py+++ b/products/replay_vision/backend/temporal/vision_alerts/activities.py@@ -45,6 +45,7 @@ from products.replay_vision.backend.alert_utils import (compute_shard_offset_seconds,due_alerts_q,next_allowed_check_at,+ observation_url,)from products.replay_vision.backend.models.replay_observation import ObservationStatus, ReplayObservationfrom products.replay_vision.backend.models.vision_alert import (@@ -739,6 +740,7 @@ def _emit_match_event().values_list("id", "scanner_result", "completed_at")by_id = {str(row_id): (scanner_result, completed_at) for row_id, scanner_result, completed_at in summary_rows}lines: list[str] = []+ mrkdwn_lines: list[str] = []budget = MATCH_SUMMARY_MAX_CHARSfor observation_id in observation_ids[:MATCH_SUMMARY_LINES]:scanner_result, completed_at = by_id.get(observation_id, (None, None))@@ -753,28 +755,31 @@ def _emit_match_event(stamp = f"({completed_at:%Y-%m-%d %H:%M} UTC) " if completed_at else ""body = f"{descriptor}: {explanation}" if explanation else descriptorline = f"- {stamp}{body}"+ # The link is appended after escaping, which would otherwise mangle its <url|label> syntax.+ mrkdwn_line = f"{escape_slack_mrkdwn(line)} <{observation_url(alert.team_id, observation_id)}|View>"# The Slack copy is what has to fit the block, so the budget measures the escaped length.- cost = len(escape_slack_mrkdwn(line)) + 1+ cost = len(mrkdwn_line) + 1if cost > budget:breaklines.append(line)+ mrkdwn_lines.append(mrkdwn_line)budget -= costremaining = len(observation_ids) - len(lines)if remaining > 0:lines.append(f"- and {remaining} more")+ mrkdwn_lines.append(f"- and {remaining} more")# Deterministic uuid over the drained row set: an identical-batch retry (crash# after ack, before the stamp, with no new rows) dedupes at ingestion.event_uuid = uuid5(NAMESPACE_URL, f"vision-alert-match:{alert.id}:{','.join(sorted(str(m) for m, _ in rows))}")- summary = "\n".join(lines)properties = {**_base_properties(alert, now),"matched_count": len(rows),"observation_ids": observation_ids,- "summary": escape_slack_mrkdwn(summary),- # Webhook consumers parse JSON, so they get the same text without the mrkdwn entities.- "summary_text": summary,+ "summary": "\n".join(mrkdwn_lines),+ # Webhook consumers parse JSON, so they get the same text without the mrkdwn entities or links.+ "summary_text": "\n".join(lines),}return produce_alert_internal_event(team_id=alert.team_id,diff --git a/products/replay_vision/backend/tests/test_vision_alerts_match_outbox.py b/products/replay_vision/backend/tests/test_vision_alerts_match_outbox.pyindex c87172d892d7..991131a26ae9 100644--- a/products/replay_vision/backend/tests/test_vision_alerts_match_outbox.py+++ b/products/replay_vision/backend/tests/test_vision_alerts_match_outbox.py@@ -10,6 +10,7 @@ from parameterized import parameterizedfrom posthog.models.utils import uuid7+from products.replay_vision.backend.alert_utils import observation_urlfrom products.replay_vision.backend.models.replay_observation import (ObservationStatus,ObservationTrigger,@@ -173,6 +174,9 @@ class TestVisionAlertMatchOutbox(BaseTest):# Slack reads mrkdwn, webhook consumers read JSON, so only one copy is entity-escaped.assert props["summary"].count("verdict=yes: checkout broke & stalled") == 3assert props["summary_text"].count("verdict=yes: checkout broke & stalled") == 3+ for observation_id in props["observation_ids"]:+ assert f"<{observation_url(self.team.id, observation_id)}|View>" in props["summary"]+ assert "/replay-vision/observations/" not in props["summary_text"]assert produce.call_args.kwargs["uuid"] is not Noneassert not VisionAlertMatch.all_teams.filter(alert_id=alert.id, delivered_at__isnull=True).exists()@@ -268,7 +272,9 @@ class TestVisionAlertMatchOutbox(BaseTest):_, produce = self._drain(delivered=True)summary = produce.call_args.kwargs["properties"]["summary"]assert "\n" not in summary- assert summary.endswith("Checkout stalled. forged row: the user retried twice")+ assert summary.endswith(+ f"Checkout stalled. forged row: the user retried twice <{observation_url(self.team.id, str(observation.id))}|View>"+ )def test_summary_caps_lines_but_stamps_all_rows(self) -> None:alert = self._make_match_alert(selection={})