: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(logs): retry errored alert notification after kafka enqueue failure (#70573)
Matt Pua0b6bf401c561
diff --git a/products/logs/backend/temporal/activities.py b/products/logs/backend/temporal/activities.pyindex 175c9ea192ec..593205233764 100644--- a/products/logs/backend/temporal/activities.py+++ b/products/logs/backend/temporal/activities.py@@ -260,8 +260,15 @@ class _DispatchedAlert:"""Phase 2 output: notification dispatched, ready for the cohort bulk save.`notification_failed` is the source of truth for state rollback: if True,- the state machine's `new_state` is replaced with the alert's existing state- so the next cycle re-tries the notification.+ the state machine's `new_state` is reset to the alert's pre-check value so+ the next cycle re-evaluates and re-tries the notification, and+ `consecutive_failures` may heal downward but never advance. The counter+ matters as much as the state: the error notification fires on the 0 -> 1+ failure-counter edge, so persisting an advanced counter after a failed+ enqueue would silence the retry forever. A successful evaluation's counter+ reset is kept, though — only delivery failed, and voiding the evidence that+ evaluation recovered would let a later error skip its notify edge and drag+ a stale streak toward BROKEN.`produce_result` is the pending Kafka delivery for this alert'snotification (None when no notification was attempted or the enqueue itself@@ -281,6 +288,10 @@ class _DispatchedAlert:return dataclasses.replace(self.evaluation.outcome,new_state=AlertState(self.evaluation.alert.state),+ consecutive_failures=min(+ self.evaluation.alert.consecutive_failures,+ self.evaluation.outcome.consecutive_failures,+ ),)return self.evaluation.outcomediff --git a/products/logs/backend/test/test_logs_alerting_activities.py b/products/logs/backend/test/test_logs_alerting_activities.pyindex 89458d7f88dd..6a2e56286ec3 100644--- a/products/logs/backend/test/test_logs_alerting_activities.py+++ b/products/logs/backend/test/test_logs_alerting_activities.py@@ -1264,6 +1264,77 @@ class TestEvaluateSingleAlert(APIBaseTest):assert len(errored_calls) == 1assert errored_calls[0].kwargs["event"].properties["consecutive_failures"] == 1+ @freeze_time("2025-01-01T00:01:00Z")+ @patch("products.logs.backend.temporal.activities.AlertCheckQuery")+ @patch("products.logs.backend.temporal.activities.capture_exception")+ def test_errored_notification_retried_after_kafka_failure(self, _mock_capture, mock_query_cls):+ mock_query_cls.return_value.execute_rolling_checks.side_effect = Exception("CH down")+ alert = self._make_alert()+ now1 = datetime(2025, 1, 1, 0, 1, 0, tzinfo=UTC)+ now2 = datetime(2025, 1, 1, 0, 6, 0, tzinfo=UTC)++ # Non-transient classification so the failure notifies at all; the enqueue+ # failure must then roll back the failure counter, or the second check+ # never sees the 0 -> 1 notify edge and the notification is lost.+ with (+ patch("products.logs.backend.temporal.activities.produce_internal_event") as mock_produce,+ patch(+ "products.logs.backend.alert_error_classifier.classify_query_error",+ return_value=QueryErrorCategory.QUERY_PERFORMANCE_ERROR,+ ),+ ):+ mock_produce.side_effect = Exception("Kafka down")+ _evaluate_and_save_one(alert, now1, _make_stats())++ mock_produce.side_effect = None+ mock_produce.reset_mock()+ _evaluate_and_save_one(alert, now2, _make_stats())++ errored_calls = [+ c+ for c in mock_produce.call_args_list+ if c.kwargs.get("event") and c.kwargs["event"].event == "$logs_alert_errored"+ ]+ assert len(errored_calls) == 1++ @freeze_time("2025-01-01T00:01:00Z")+ @patch("products.logs.backend.temporal.activities.AlertCheckQuery")+ @patch("products.logs.backend.temporal.activities.capture_exception")+ def test_enqueue_failure_keeps_successful_checks_counter_reset(self, _mock_capture, mock_query_cls):+ # The rollback may heal the counter downward but never restore a stale+ # streak: a successful evaluation whose FIRE enqueue failed still proves+ # the alert works, so its counter reset must survive the rollback and a+ # later error must see a fresh 0 -> 1 notify edge.+ _mock_buckets(mock_query_cls, [50])+ alert = self._make_alert(state=LogsAlertConfiguration.State.ERRORED, consecutive_failures=3)+ now1 = datetime(2025, 1, 1, 0, 1, 0, tzinfo=UTC)+ now2 = datetime(2025, 1, 1, 0, 6, 0, tzinfo=UTC)++ with (+ patch("products.logs.backend.temporal.activities.produce_internal_event") as mock_produce,+ patch(+ "products.logs.backend.alert_error_classifier.classify_query_error",+ return_value=QueryErrorCategory.QUERY_PERFORMANCE_ERROR,+ ),+ ):+ mock_produce.side_effect = Exception("Kafka down")+ _evaluate_and_save_one(alert, now1, _make_stats())++ alert.refresh_from_db()+ assert alert.consecutive_failures == 0++ mock_produce.side_effect = None+ mock_produce.reset_mock()+ mock_query_cls.return_value.execute_rolling_checks.side_effect = Exception("CH down")+ _evaluate_and_save_one(alert, now2, _make_stats())++ errored_calls = [+ c+ for c in mock_produce.call_args_list+ if c.kwargs.get("event") and c.kwargs["event"].event == "$logs_alert_errored"+ ]+ assert len(errored_calls) == 1+@freeze_time("2025-01-01T00:01:00Z")@patch("products.logs.backend.temporal.activities.AlertCheckQuery")@patch("products.logs.backend.temporal.activities.capture_exception")