: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): don't report invalid cohort queries to error tracking (#71030)
posthog[bot]9d365a8af1db
diff --git a/posthog/tasks/calculate_cohort.py b/posthog/tasks/calculate_cohort.pyindex d0bbb4af6aa5..daec83b0b700 100644--- a/posthog/tasks/calculate_cohort.py+++ b/posthog/tasks/calculate_cohort.py@@ -12,6 +12,8 @@ from celery import Task, chain, current_task, shared_taskfrom dateutil.relativedelta import relativedeltafrom prometheus_client import Counter, Gauge, Histogram+from posthog.hogql.errors import ExposedHogQLError+from posthog.api.monitoring import Featurefrom posthog.clickhouse import query_taggingfrom posthog.clickhouse.query_tagging import QueryTags, update_tags@@ -633,7 +635,11 @@ def insert_cohort_from_query(cohort_id: int, team_id: Optional[int] = None) -> Nteam_id=team_id,error=str(err),)- capture_exception()+ # ExposedHogQLError means the user's query is invalid — that's a validation+ # error, not a system bug, so don't report it to error tracking. The failure is+ # still recorded on the cohort's errors_calculating state in the finally block.+ if not isinstance(err, ExposedHogQLError):+ capture_exception()if settings.DEBUG:raisefinally:diff --git a/posthog/tasks/test/test_calculate_cohort.py b/posthog/tasks/test/test_calculate_cohort.pyindex b800b9f3e101..a8adb16076a7 100644--- a/posthog/tasks/test/test_calculate_cohort.py+++ b/posthog/tasks/test/test_calculate_cohort.py@@ -11,6 +11,8 @@ from celery.exceptions import Retryfrom dateutil.relativedelta import relativedeltafrom parameterized import parameterized+from posthog.hogql.errors import QueryError+from posthog.exceptions import ClickHouseAtCapacityfrom posthog.tasks.calculate_cohort import (COHORT_STUCK_COUNT_GAUGE,@@ -1265,6 +1267,39 @@ class TestCohortCalculationTasks(APIBaseTest):self.assertFalse(cohort.is_calculating, "Cohort should not be in calculating state")self.assertGreater(cohort.errors_calculating, 0, "Should have recorded the processing error")+ @parameterized.expand(+ [+ # (exception raised, expected capture_exception call count)+ ("system_error", Exception("Simulated query processing error"), 1),+ ("user_query_error", QueryError("Unable to resolve field: distinct_ids"), 0),+ ]+ )+ def test_insert_cohort_from_query_only_captures_system_errors(+ self, _name: str, raised: Exception, expected_capture_calls: int+ ) -> None:+ from posthog.tasks.calculate_cohort import insert_cohort_from_query++ cohort = Cohort.objects.create(+ team_id=self.team.pk,+ name="test_query_cohort",+ is_static=True,+ count=0,+ query={"kind": "HogQLQuery", "query": "SELECT distinct_ids FROM persons LIMIT 10"},+ )++ with (+ patch("products.cohorts.backend.models.util.insert_cohort_query_actors_into_ch") as mock_insert_ch,+ patch("posthog.tasks.calculate_cohort.capture_exception") as mock_capture,+ ):+ mock_insert_ch.side_effect = raised++ insert_cohort_from_query(cohort.id, self.team.pk)++ self.assertEqual(mock_capture.call_count, expected_capture_calls)+ cohort.refresh_from_db()+ self.assertFalse(cohort.is_calculating, "Cohort should not be in calculating state")+ self.assertGreater(cohort.errors_calculating, 0, "Failure should be recorded regardless of error type")+def test_insert_cohort_from_filters_count_updated_on_exception(self) -> None:cohort = Cohort.objects.create(team_id=self.team.pk,