: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(cohorts): stop logging stats-collection retries as failures (#115864)
posthog[bot]dcc5d2e85db2
diff --git a/posthog/tasks/calculate_cohort.py b/posthog/tasks/calculate_cohort.pyindex d6b76cc9cade..fa2e89a34663 100644--- a/posthog/tasks/calculate_cohort.py+++ b/posthog/tasks/calculate_cohort.py@@ -12,7 +12,7 @@ import grpcimport structlogimport posthoganalyticsfrom celery import Task, chain, shared_task-from celery.exceptions import Retry, SoftTimeLimitExceeded+from celery.exceptions import MaxRetriesExceededError, Retry, SoftTimeLimitExceededfrom celery.utils.time import get_exponential_backoff_intervalfrom dateutil.relativedelta import relativedeltafrom prometheus_client import Counter, Gauge, Histogram@@ -1248,6 +1248,15 @@ def collect_cohort_query_stats(# This runs even if the worker OOM'd, since this task was scheduled before the calculation_collect_cohort_calculation_metrics(history, start_time)+ except Retry:+ raise+ except MaxRetriesExceededError:+ logger.warning(+ "Gave up collecting delayed cohort query stats",+ tag_matcher=tag_matcher,+ cohort_id=cohort_id,+ history_id=history_id,+ )except Exception as e:logger.exception("Failed to collect delayed cohort query stats",diff --git a/posthog/tasks/test/test_calculate_cohort.py b/posthog/tasks/test/test_calculate_cohort.pyindex 555ca3bc5ef6..040c45c7855b 100644--- a/posthog/tasks/test/test_calculate_cohort.py+++ b/posthog/tasks/test/test_calculate_cohort.py@@ -1,4 +1,5 @@from collections.abc import Callable+from contextlib import nullcontextfrom typing import Optionalimport time_machine@@ -32,6 +33,7 @@ from posthog.tasks.calculate_cohort import (STATIC_POPULATION_MAX_RETRIES,calculate_cohort_ch,calculate_cohort_from_list,+ collect_cohort_query_stats,enqueue_cohorts_to_calculate,increment_version_and_enqueue_calculate_cohort,insert_cohort_from_filters,@@ -2248,3 +2250,32 @@ class TestCalculateCohortFromListRetries(APIBaseTest):self.assertFalse(cohort.is_calculating)self.assertEqual(cohort.errors_calculating, 0)self.assertEqual(count_cohort_members(cohort.team_id, cohort.pk), 1)+++class TestCollectCohortQueryStats(APIBaseTest):+ @parameterized.expand(+ [+ ("retries_remain", 0, Retry),+ ("retries_exhausted", 3, None),+ ]+ )+ def test_missing_stats_are_not_logged_as_a_failure(+ self, _name: str, retries: int, expected_exception: type[Exception] | None+ ) -> None:+ cohort = Cohort.objects.create(team=self.team, name="cohort")+ history = CohortCalculationHistory.objects.create(+ team=self.team, cohort=cohort, filters={}, started_at=timezone.now()+ )+ task = collect_cohort_query_stats+ task.push_request(retries=retries, called_directly=False, is_eager=True)+ try:+ with (+ patch("posthog.tasks.calculate_cohort.get_clickhouse_query_stats", return_value=None),+ patch("posthog.tasks.calculate_cohort.logger") as mock_logger,+ self.assertRaises(expected_exception) if expected_exception else nullcontext(),+ ):+ task.run("cohort_calc:abc", cohort.pk, timezone.now().isoformat(), str(history.id), "SELECT 1")+ finally:+ task.pop_request()++ mock_logger.exception.assert_not_called()