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): auto-disable misconfigured SQL alerts instead of erroring loud (#67322)

Vasco de Krijger34d8b6b4a27e
diff --git a/posthog/tasks/alerts/utils.py b/posthog/tasks/alerts/utils.py
index 04a04286663d..7a7cbe8d9810 100644
--- a/posthog/tasks/alerts/utils.py
+++ b/posthog/tasks/alerts/utils.py
@@ -371,7 +371,13 @@ def add_alert_check(
return alert_check, notify
-def disable_invalid_alert(alert: AlertConfiguration, reason: str) -> None:
+def disable_invalid_alert(alert: AlertConfiguration, reason: str) -> AlertCheck:
+ """Auto-disable a misconfigured alert and email its subscribers.
+
+ Used for configuration problems that make the alert unevaluable as set up — a deliberate,
+ fail-loud outcome, not a bug — so the reason is surfaced to the owner rather than captured
+ as an exception. Returns the recorded ERRORED AlertCheck so callers can reference it.
+ """
logger.warning("check_alert.auto_disabling", alert_id=alert.id, reason=reason)
AlertConfiguration.objects.filter(pk=alert.pk).update(
enabled=False,
@@ -381,7 +387,7 @@ def disable_invalid_alert(alert: AlertConfiguration, reason: str) -> None:
alert.refresh_from_db()
targets_to_notify = alert.get_subscribed_users_emails()
- AlertCheck.objects.create(
+ alert_check = AlertCheck.objects.create(
alert_configuration=alert,
calculated_value=None,
condition=alert.condition,
@@ -391,6 +397,7 @@ def disable_invalid_alert(alert: AlertConfiguration, reason: str) -> None:
)
if targets_to_notify:
send_notifications_for_disabled(alert, reason, targets_to_notify)
+ return alert_check
def send_notifications_for_disabled(alert: AlertConfiguration, reason: str, targets: list[str]) -> None:
diff --git a/posthog/temporal/alerts/activities.py b/posthog/temporal/alerts/activities.py
index 467874472f5f..8be4652b8a3b 100644
--- a/posthog/temporal/alerts/activities.py
+++ b/posthog/temporal/alerts/activities.py
@@ -39,6 +39,7 @@ from posthog.temporal.alerts.types import (
from posthog.temporal.common.heartbeat import Heartbeater
from products.alerts.backend.evaluation import check_alert_for_insight
+from products.alerts.backend.evaluation.contract import AlertExtractionError
from products.alerts.backend.evaluation.validation import validate_alert_config
from products.alerts.backend.models.alert import AlertCheck, AlertConfiguration
from products.notifications.backend.facade.api import (
@@ -219,6 +220,17 @@ async def evaluate_alert(inputs: EvaluateAlertActivityInputs) -> EvaluateAlertRe
breaches = alert_evaluation_result.breaches
except CH_TRANSIENT_ERRORS:
raise
+ except AlertExtractionError as err:
+ # The alert can't be evaluated as configured (wrong query shape / bad config) — a
+ # deliberate fail-loud outcome, not a bug. Auto-disable and email the owner via the
+ # existing path instead of capturing it as an exception, which would pollute error
+ # tracking with a config problem that recurs on every check until fixed.
+ alert_check = disable_invalid_alert(alert, str(err))
+ return EvaluateAlertResult(
+ alert_check_id=str(alert_check.id),
+ should_notify=False, # disable_invalid_alert already emailed subscribers
+ new_state=AlertState.ERRORED,
+ )
except Exception as err:
logger.exception(f"Alert id = {alert.id}, failed to evaluate", exc_info=err)
capture_exception(
diff --git a/posthog/temporal/tests/test_alerts_activities.py b/posthog/temporal/tests/test_alerts_activities.py
index d32382d6b27e..e1b2667c3c25 100644
--- a/posthog/temporal/tests/test_alerts_activities.py
+++ b/posthog/temporal/tests/test_alerts_activities.py
@@ -33,6 +33,7 @@ from posthog.temporal.alerts.types import (
SkipReason,
)
+from products.alerts.backend.evaluation.contract import AlertExtractionError
from products.alerts.backend.evaluation.validation import THRESHOLD_BOUNDS_REQUIRED_MESSAGE
from products.alerts.backend.models.alert import AlertCheck, AlertConfiguration, Threshold
from products.product_analytics.backend.models.insight import Insight
@@ -355,6 +356,40 @@ class TestEvaluateAlert:
refreshed = await sync_to_async(AlertConfiguration.objects.get)(pk=alert.pk)
assert refreshed.enabled is True
+ async def test_evaluate_auto_disables_and_skips_error_tracking_on_extraction_error(self, alert_with_user) -> None:
+ # A misconfigured query (wrong shape / bad config) fails loud with AlertExtractionError. That's
+ # a config problem, not a bug: it must auto-disable + email the owner, not hit error tracking.
+ # alert_with_user has a subscriber, so this also exercises the send_notifications_for_disabled
+ # branch — guarding against a silent regression where the owner isn't told their alert died.
+ with (
+ patch(
+ "posthog.temporal.alerts.activities.check_alert_for_insight",
+ side_effect=AlertExtractionError("query returns 2 numeric columns — pick one"),
+ ),
+ patch("posthog.temporal.alerts.activities.capture_exception") as mock_capture,
+ patch("posthog.tasks.alerts.utils.send_notifications_for_disabled") as mock_notify,
+ ):
+ env = ActivityEnvironment()
+ result = await env.run(evaluate_alert, EvaluateAlertActivityInputs(alert_id=str(alert_with_user.id)))
+
+ assert result.new_state == AlertState.ERRORED
+ assert result.should_notify is False # disable_invalid_alert already emailed subscribers
+ mock_capture.assert_not_called()
+
+ check = await sync_to_async(AlertCheck.objects.get)(pk=result.alert_check_id)
+ assert check.state == AlertState.ERRORED
+ assert check.error is not None
+ assert "2 numeric columns" in check.error["message"]
+
+ refreshed = await sync_to_async(AlertConfiguration.objects.get)(pk=alert_with_user.pk)
+ assert refreshed.enabled is False
+
+ mock_notify.assert_called_once()
+ notified_alert, reason, targets = mock_notify.call_args.args
+ assert notified_alert.id == alert_with_user.id
+ assert "2 numeric columns" in reason
+ assert targets # the subscribed owner's email
+
async def test_evaluate_reraises_ch_transient_error(self, alert) -> None:
# Transient CH errors bubble up so Temporal's retry policy handles them.
with patch(