: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
fix(alerts): restore insight alert destinations (#83993)
Matt Puae7e7fa93a423
diff --git a/posthog/cdp/internal_events.py b/posthog/cdp/internal_events.pyindex f4e698c364df..8e706e87c0b7 100644--- a/posthog/cdp/internal_events.py+++ b/posthog/cdp/internal_events.py@@ -17,12 +17,17 @@ logger = structlog.get_logger(__name__)# filter (products/cdp hog_function queryset) and mirrored in the Node CDP consumer, so keep it# POSIX-compatible (no (?:...) groups).MANAGED_ALERT_EVENT_PATTERN = r"^\$[a-z0-9_]+_alert_(firing|resolved|errored|auto_disabled)$"+LEGACY_INSIGHT_ALERT_EVENT = "$insight_alert_firing"_MANAGED_ALERT_EVENT = re.compile(MANAGED_ALERT_EVENT_PATTERN)def is_managed_alert_internal_event(event_name: object) -> bool:"""Return whether an internal event is reserved for an alert-owned destination."""- return isinstance(event_name, str) and _MANAGED_ALERT_EVENT.fullmatch(event_name) is not None+ return (+ isinstance(event_name, str)+ and event_name != LEGACY_INSIGHT_ALERT_EVENT+ and _MANAGED_ALERT_EVENT.fullmatch(event_name) is not None+ )@dataclassdiff --git a/products/cdp/backend/api/hog_function.py b/products/cdp/backend/api/hog_function.pyindex d2c615847016..1c924c8a783b 100644--- a/products/cdp/backend/api/hog_function.py+++ b/products/cdp/backend/api/hog_function.py@@ -26,7 +26,11 @@ from posthog.api.log_entries import LogEntryMixinfrom posthog.api.routing import TeamAndOrgViewSetMixinfrom posthog.api.shared import SearchMatchTypeSerializerMixin, UserBasicSerializerfrom posthog.api.utils import action, log_activity_from_viewset-from posthog.cdp.internal_events import MANAGED_ALERT_EVENT_PATTERN, is_managed_alert_internal_event+from posthog.cdp.internal_events import (+ LEGACY_INSIGHT_ALERT_EVENT,+ MANAGED_ALERT_EVENT_PATTERN,+ is_managed_alert_internal_event,+)from posthog.cdp.services.icons import CDPIconsServicefrom posthog.cdp.site_functions import get_transpiled_functionfrom posthog.cdp.validation import (@@ -949,7 +953,9 @@ class HogFunctionViewSet(queryset = queryset.exclude(type=HogFunctionType.WAREHOUSE_SOURCE_WEBHOOK.value)# Managed alert destinations are single-event by construction, so events[0] is sufficient here.queryset = queryset.filter(- Q(filters__events__0__id__isnull=True) | ~Q(filters__events__0__id__regex=MANAGED_ALERT_EVENT_PATTERN)+ Q(filters__events__0__id__isnull=True)+ | Q(filters__events__0__id=LEGACY_INSIGHT_ALERT_EVENT)+ | ~Q(filters__events__0__id__regex=MANAGED_ALERT_EVENT_PATTERN))if not (self.action == "partial_update" and self.request.data.get("deleted") is False):diff --git a/products/cdp/backend/api/test/test_hog_function.py b/products/cdp/backend/api/test/test_hog_function.pyindex 0360a643c0a8..825d61a27559 100644--- a/products/cdp/backend/api/test/test_hog_function.py+++ b/products/cdp/backend/api/test/test_hog_function.py@@ -203,6 +203,30 @@ class TestHogFunctionAPIWithoutAvailableFeature(ClickhouseTestMixin, APIBaseTestself.assertEqual(response.json()["attr"], "filters")self.assertIn("managed through the alert API", response.json()["detail"])+ def test_generic_api_can_create_and_list_legacy_insight_alert_destinations(self):+ response = self.client.post(+ f"/api/projects/{self.team.id}/hog_functions/",+ data={+ "name": "Insight alert destination",+ "type": "internal_destination",+ "template_id": template_slack.id,+ "enabled": True,+ "inputs": {+ "slack_workspace": {"value": 1},+ "channel": {"value": "#general"},+ },+ "filters": {"events": [{"id": "$insight_alert_firing", "type": "events"}]},+ },+ )++ self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.json())+ function_id = response.json()["id"]++ list_response = self.client.get(f"/api/projects/{self.team.id}/hog_functions/?full=true")+ self.assertEqual(list_response.status_code, status.HTTP_200_OK)+ listed_ids = {item["id"] for item in list_response.json()["results"]}+ self.assertIn(function_id, listed_ids)+def test_generic_api_hides_and_cannot_patch_managed_alert_destinations(self):ordinary = HogFunction.objects.create(team=self.team,