: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): keep scheduling when metrics fail (#98580)
Vasco de Krijger9e76b8c30083
diff --git a/posthog/temporal/alerts/activities.py b/posthog/temporal/alerts/activities.pyindex abfed2160385..6fed101fc150 100644--- a/posthog/temporal/alerts/activities.py+++ b/posthog/temporal/alerts/activities.py@@ -1,3 +1,4 @@+import asyncioimport tracebackfrom concurrent.futures import ThreadPoolExecutorfrom datetime import UTC, datetime@@ -15,6 +16,7 @@ from posthog.schema import AlertStatefrom posthog.hogql.errors import TableAccessDeniedErrorfrom posthog.clickhouse.query_tagging import Feature, Product, tag_queries+from posthog.dataclasses import frozenfrom posthog.email import is_email_availablefrom posthog.errors import CH_TRANSIENT_ERRORSfrom posthog.exceptions_capture import capture_exception@@ -51,6 +53,7 @@ from posthog.temporal.alerts.types import (SkipReason,)from posthog.temporal.common.heartbeat import Heartbeater+from posthog.temporal.common.metrics import get_metric_meterfrom products.alerts.backend.evaluation import check_alert_for_insightfrom products.alerts.backend.evaluation.contract import AlertExtractionError@@ -72,13 +75,21 @@ logger = structlog.get_logger(__name__)_NOTIFICATION_DELIVERY_EXECUTOR = ThreadPoolExecutor(max_workers=10, thread_name_prefix="insight-alert-delivery")+@frozen+class _RetrievedAlerts:+ alerts: list[AlertInfo]+ due_count: int+ oldest_due_at: datetime | None+ polled_at: datetime++@temporalio.activity.defnasync def retrieve_due_alerts(inputs: ScheduleDueAlertChecksWorkflowInputs | None = None) -> list[AlertInfo]:if inputs is None:inputs = ScheduleDueAlertChecksWorkflowInputs()@database_sync_to_async(thread_sensitive=False)- def get_alerts() -> list[AlertInfo]:+ def get_alerts() -> _RetrievedAlerts:polled_at = datetime.now(UTC)calculation_interval_order = Case(@@ -134,11 +145,37 @@ async def retrieve_due_alerts(inputs: ScheduleDueAlertChecksWorkflowInputs | Nondue_alert_metrics = due_alerts_query.aggregate(due_count=Count("id"), oldest_due_at=Min(Coalesce("next_check_at", "created_at")))- record_due_insight_alert_metrics(due_alert_metrics["due_count"], due_alert_metrics["oldest_due_at"], polled_at)- return alerts+ return _RetrievedAlerts(+ alerts=alerts,+ due_count=due_alert_metrics["due_count"],+ oldest_due_at=due_alert_metrics["oldest_due_at"],+ polled_at=polled_at,+ )- async with Heartbeater():- return await get_alerts()+ retrieved = await get_alerts()+ try:+ await asyncio.to_thread(+ record_due_insight_alert_metrics,+ retrieved.due_count,+ retrieved.oldest_due_at,+ retrieved.polled_at,+ )+ except Exception:+ logger.exception("Failed to record due insight alert metrics")++ try:+ meter = get_metric_meter()+ meter.create_counter(+ "insight_alert_scheduler_capacity",+ "Alert scheduling capacity made available across successful retrieval runs",+ ).add(inputs.max_alerts_per_run)+ meter.create_counter(+ "insight_alert_scheduler_alerts_selected",+ "Due alerts selected across successful alert scheduler retrieval runs",+ ).add(len(retrieved.alerts))+ except Exception:+ logger.exception("Failed to record alert scheduler capacity metrics")+ return retrieved.alertsdef _has_active_destinations(alert: AlertConfiguration) -> bool:diff --git a/posthog/temporal/tests/test_alerts_activities.py b/posthog/temporal/tests/test_alerts_activities.pyindex c8a83f524560..8fbed5721f04 100644--- a/posthog/temporal/tests/test_alerts_activities.py+++ b/posthog/temporal/tests/test_alerts_activities.py@@ -50,7 +50,6 @@ from posthog.temporal.alerts.types import (PrepareAction,PrepareAlertActivityInputs,RecordFailedEvaluationActivityInputs,- ScheduleDueAlertChecksWorkflowInputs,SkipReason,)@@ -134,31 +133,6 @@ async def _create_alert(return await _create()-async def test_retrieve_due_alerts_limits_each_schedule_run_without_starving_other_teams(- ateam: Team,-) -> None:- max_alerts_per_run = 2- for _ in range(max_alerts_per_run):- await _create_alert(ateam, calculation_interval=AlertCalculationInterval.REAL_TIME.value)-- other_team = await sync_to_async(Team.objects.create)(- organization_id=ateam.organization_id,- project_id=ateam.project_id,- name="Other team",- )- other_alert = await _create_alert(other_team)-- alerts = await ActivityEnvironment().run(- retrieve_due_alerts,- ScheduleDueAlertChecksWorkflowInputs(max_alerts_per_run=max_alerts_per_run),- )-- assert len(alerts) == max_alerts_per_run- assert str(other_alert.id) in {alert.alert_id for alert in alerts}--@pytest_asyncio.fixtureasync def alert(ateam):return await _create_alert(ateam)diff --git a/posthog/temporal/tests/test_alerts_retrieval.py b/posthog/temporal/tests/test_alerts_retrieval.pynew file mode 100644index 000000000000..a99c2d91b976--- /dev/null+++ b/posthog/temporal/tests/test_alerts_retrieval.py@@ -0,0 +1,200 @@+from datetime import UTC, datetime++import pytest+import time_machine+from unittest.mock import MagicMock, call, patch++from asgiref.sync import sync_to_async+from temporalio.common import MetricCounter, MetricMeter+from temporalio.testing import ActivityEnvironment++from posthog.schema import AlertCalculationInterval++from posthog.models import Team+from posthog.temporal.alerts.activities import _RetrievedAlerts, retrieve_due_alerts+from posthog.temporal.alerts.types import AlertInfo, ScheduleDueAlertChecksWorkflowInputs+from posthog.temporal.tests.test_alerts_activities import _create_alert++[email protected]_db(transaction=True)+ ("alerts_per_team", "max_alerts_per_run", "expected_team_order"),+ [+ pytest.param([3, 1], 4, [0, 1, 0, 0], id="unused-rounds-return-to-busy-team"),+ pytest.param([3, 3, 3], 5, [0, 1, 2, 0, 1], id="cap-truncates-a-rank-round"),+ pytest.param([4, 1, 1, 1], 4, [0, 1, 2, 3], id="each-team-gets-first-rank-before-second"),+ pytest.param([1, 1, 1], 2, [0, 1], id="cap-smaller-than-due-team-count"),+ ],+)+async def test_retrieve_due_alerts_uses_team_rank_rounds(+ ateam: Team,+ alerts_per_team: list[int],+ max_alerts_per_run: int,+ expected_team_order: list[int],+) -> None:+ teams = [ateam]+ for team_index in range(1, len(alerts_per_team)):+ teams.append(+ await sync_to_async(Team.objects.create)(+ organization_id=ateam.organization_id,+ project_id=ateam.project_id,+ name=f"Team {team_index}",+ )+ )++ for team, alert_count in zip(teams, alerts_per_team, strict=True):+ for _ in range(alert_count):+ await _create_alert(team)++ alerts = await ActivityEnvironment().run(+ retrieve_due_alerts,+ ScheduleDueAlertChecksWorkflowInputs(max_alerts_per_run=max_alerts_per_run),+ )++ team_index_by_id = {team.id: index for index, team in enumerate(teams)}+ assert [team_index_by_id[alert.team_id] for alert in alerts] == expected_team_order++[email protected]_db(transaction=True)+async def test_retrieve_due_alerts_excludes_future_checks_and_applies_the_documented_order_within_each_team(+ ateam: Team,+) -> None:+ alert_specs = [+ # These high-priority intervals would lead the result if due filtering regressed.+ ("future_real_time", AlertCalculationInterval.REAL_TIME, datetime(2026, 9, 10, 12, 1, tzinfo=UTC)),+ ("future_15_minutes", AlertCalculationInterval.EVERY_15_MINUTES, datetime(2026, 9, 10, 12, 15, tzinfo=UTC)),+ ("fresh_daily_tie_lower_id", AlertCalculationInterval.DAILY, datetime(2026, 9, 10, 11, 47, tzinfo=UTC)),+ ("fresh_hourly", AlertCalculationInterval.HOURLY, datetime(2026, 9, 10, 11, 46, tzinfo=UTC)),+ ("aged_real_time", AlertCalculationInterval.REAL_TIME, datetime(2026, 9, 10, 11, 40, tzinfo=UTC)),+ ("fresh_daily_earlier", AlertCalculationInterval.DAILY, datetime(2026, 9, 10, 11, 46, tzinfo=UTC)),+ ("never_checked", AlertCalculationInterval.DAILY, None),+ ("fresh_real_time", AlertCalculationInterval.REAL_TIME, datetime(2026, 9, 10, 11, 59, tzinfo=UTC)),+ ("aged_daily_oldest", AlertCalculationInterval.DAILY, datetime(2026, 9, 10, 11, 30, tzinfo=UTC)),+ ("fresh_15_minutes", AlertCalculationInterval.EVERY_15_MINUTES, datetime(2026, 9, 10, 11, 58, tzinfo=UTC)),+ ("fresh_daily_tie_higher_id", AlertCalculationInterval.DAILY, datetime(2026, 9, 10, 11, 47, tzinfo=UTC)),+ ]+ alert_label_by_id: dict[str, str] = {}+ for label, calculation_interval, next_check_at in alert_specs:+ alert = await _create_alert(+ ateam,+ calculation_interval=calculation_interval.value,+ next_check_at=next_check_at,+ )+ alert_label_by_id[str(alert.id)] = label++ with time_machine.travel("2026-09-10T12:00:00Z", tick=False):+ alerts = await ActivityEnvironment().run(+ retrieve_due_alerts,+ ScheduleDueAlertChecksWorkflowInputs(max_alerts_per_run=len(alert_specs)),+ )++ assert [alert_label_by_id[alert.alert_id] for alert in alerts] == [+ "aged_real_time",+ "fresh_real_time",+ "fresh_15_minutes",+ "fresh_hourly",+ "never_checked",+ "aged_daily_oldest",+ "fresh_daily_earlier",+ "fresh_daily_tie_lower_id",+ "fresh_daily_tie_higher_id",+ ]++[email protected]_db(transaction=True)+async def test_retrieve_due_alerts_reselects_the_same_oldest_due_alerts_until_checks_advance(ateam: Team) -> None:+ due_alerts = [+ await _create_alert(+ ateam,+ next_check_at=datetime(2026, 9, 10, 11, minute, tzinfo=UTC),+ )+ for minute in range(3)+ ]+ inputs = ScheduleDueAlertChecksWorkflowInputs(max_alerts_per_run=2)++ with time_machine.travel("2026-09-10T12:00:00Z", tick=False):+ first_sweep = await ActivityEnvironment().run(retrieve_due_alerts, inputs)+ second_sweep = await ActivityEnvironment().run(retrieve_due_alerts, inputs)++ oldest_due_alert_ids = [str(alert.id) for alert in due_alerts[:2]]+ first_sweep_ids = [alert.alert_id for alert in first_sweep]+ second_sweep_ids = [alert.alert_id for alert in second_sweep]++ assert first_sweep_ids == oldest_due_alert_ids+ # Retrieval does not advance next_check_at, so the next sweep sees the same oldest cohort as due.+ assert second_sweep_ids == first_sweep_ids+++async def test_retrieve_due_alerts_records_capacity_and_selected_alert_counters() -> None:+ meter = MagicMock(spec=MetricMeter)+ capacity_counter = MagicMock(spec=MetricCounter)+ selected_counter = MagicMock(spec=MetricCounter)+ counters = {+ "insight_alert_scheduler_capacity": capacity_counter,+ "insight_alert_scheduler_alerts_selected": selected_counter,+ }+ meter.create_counter.side_effect = lambda name, description: counters[name]+ environment = ActivityEnvironment()+ polled_at = datetime(2026, 9, 10, 12, 0, tzinfo=UTC)++ async def fake_get_alerts() -> _RetrievedAlerts:+ return _RetrievedAlerts(alerts=[MagicMock()] * 9, due_count=42, oldest_due_at=None, polled_at=polled_at)++ with (+ patch(+ "posthog.temporal.alerts.activities.database_sync_to_async",+ return_value=MagicMock(return_value=fake_get_alerts),+ ),+ patch("posthog.temporal.alerts.activities.record_due_insight_alert_metrics"),+ patch("posthog.temporal.alerts.activities.get_metric_meter", return_value=meter),+ ):+ for max_alerts_per_run in (11, 10, 9):+ await environment.run(+ retrieve_due_alerts,+ ScheduleDueAlertChecksWorkflowInputs(max_alerts_per_run=max_alerts_per_run),+ )++ expected_counter_names = [+ "insight_alert_scheduler_capacity",+ "insight_alert_scheduler_alerts_selected",+ ] * 3+ created_counter_names = [metric_call.args[0] for metric_call in meter.create_counter.call_args_list]+ assert created_counter_names == expected_counter_names+ assert capacity_counter.add.call_args_list == [call(11), call(10), call(9)]+ assert selected_counter.add.call_args_list == [call(9), call(9), call(9)]+ meter.with_additional_attributes.assert_not_called()++[email protected]("failing_metrics", ["due_alerts", "scheduler_counters"])+async def test_retrieve_due_alerts_succeeds_when_metric_recording_fails(failing_metrics: str) -> None:+ expected_alerts: list[AlertInfo] = [MagicMock()]+ polled_at = datetime(2026, 9, 10, 12, 0, tzinfo=UTC)++ async def fake_get_alerts() -> _RetrievedAlerts:+ return _RetrievedAlerts(alerts=expected_alerts, due_count=1, oldest_due_at=None, polled_at=polled_at)++ record_due_metrics = MagicMock()+ get_metric_meter = MagicMock(return_value=MagicMock(spec=MetricMeter))+ if failing_metrics == "due_alerts":+ record_due_metrics.side_effect = RuntimeError("due alert metrics backend unavailable")+ else:+ get_metric_meter.side_effect = RuntimeError("scheduler metrics backend unavailable")++ with (+ patch(+ "posthog.temporal.alerts.activities.database_sync_to_async",+ return_value=MagicMock(return_value=fake_get_alerts),+ ),+ patch("posthog.temporal.alerts.activities.record_due_insight_alert_metrics", new=record_due_metrics),+ patch("posthog.temporal.alerts.activities.get_metric_meter", new=get_metric_meter),+ ):+ alerts = await ActivityEnvironment().run(retrieve_due_alerts)++ assert alerts == expected_alerts+ record_due_metrics.assert_called_once_with(1, None, polled_at)+ get_metric_meter.assert_called_once_with()diff --git a/posthog/temporal/tests/test_alerts_workflows.py b/posthog/temporal/tests/test_alerts_workflows.pyindex 5f3718d29125..29c0518452c9 100644--- a/posthog/temporal/tests/test_alerts_workflows.py+++ b/posthog/temporal/tests/test_alerts_workflows.py@@ -34,6 +34,7 @@ from posthog.temporal.alerts.activities import evaluate_alert, notify_alert, prefrom posthog.temporal.alerts.retry_policy import ALERT_EVALUATE_RETRY_POLICYfrom posthog.temporal.alerts.schedule import create_schedule_due_alert_checks_schedulefrom posthog.temporal.alerts.types import (+ DEFAULT_MAX_DUE_ALERTS_PER_SCHEDULE_RUN,AlertInfo,CheckAlertWorkflowInputs,ScheduleDueAlertChecksWorkflowInputs,@@ -70,7 +71,7 @@ async def test_schedule_due_alert_checks_passes_configured_limit_to_retrieval()@pytest.mark.asyncio-async def test_schedule_due_alert_checks_defaults_to_three_hundred_alerts() -> None:+async def test_schedule_due_alert_checks_uses_default_alert_limit() -> None:create_schedule = AsyncMock()with (@@ -87,7 +88,9 @@ async def test_schedule_due_alert_checks_defaults_to_three_hundred_alerts() -> Nassert create_schedule.await_args is not Noneschedule = create_schedule.await_args.args[2]- assert schedule.action.args == [ScheduleDueAlertChecksWorkflowInputs(max_alerts_per_run=300)]+ assert schedule.action.args == [+ ScheduleDueAlertChecksWorkflowInputs(max_alerts_per_run=DEFAULT_MAX_DUE_ALERTS_PER_SCHEDULE_RUN)+ ]@pytest.mark.asyncio