: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(hogql): stop capturing user query errors as error-tracking issues (#65297)
George Munyoroa4dcdc37a177
diff --git a/posthog/hogql_queries/query_runner.py b/posthog/hogql_queries/query_runner.pyindex 31210afe3c5d..b98949a559c1 100644--- a/posthog/hogql_queries/query_runner.py+++ b/posthog/hogql_queries/query_runner.py@@ -1584,7 +1584,10 @@ class QueryRunner(ABC, Generic[Q, R, CR]):self.query_id = query_id or self.query_idself._cache_age_override = cache_age_seconds- with posthoganalytics.new_context():+ # capture_exceptions=False: we capture explicitly at the except boundary below, so benign+ # user-input query errors (USER_ERROR / cancelled / rate-limited) are returned to the user+ # as 4xx without also polluting error tracking with server-side exception noise.+ with posthoganalytics.new_context(capture_exceptions=False):query_type = getattr(self.query, "kind", "Other")distinct_id = str(user.distinct_id) if user else str(self.team.uuid)@@ -1763,6 +1766,13 @@ class QueryRunner(ABC, Generic[Q, R, CR]):slo.succeed(error_category=category.value)else:slo.fail(error_category=category.value)+ # Capture only what classifies as a FAILURE outcome. User-input query errors+ # (USER_ERROR / cancelled / rate-limited) classify as SUCCESS above and are+ # deliberately not captured — they're returned to the user as 4xx. Note this+ # gate is the SLO outcome, not a strict platform-vs-user split:+ # QUERY_PERFORMANCE_ERROR is FAILURE (so captured) even though a minority of+ # those are user-input limits — see _classify_error_for_slo.+ capture_exception(exc)raisedef _execute_and_cache_blocking(diff --git a/posthog/hogql_queries/test/test_query_runner.py b/posthog/hogql_queries/test/test_query_runner.pyindex 330d226e3a24..7bd01d5740e9 100644--- a/posthog/hogql_queries/test/test_query_runner.py+++ b/posthog/hogql_queries/test/test_query_runner.py@@ -42,6 +42,7 @@ from posthog.schema import (from posthog.hogql import astfrom posthog.hogql.constants import LimitContextfrom posthog.hogql.database.database import Database+from posthog.hogql.errors import QueryErrorfrom posthog.clickhouse.client.limit import ConcurrencyLimitExceededfrom posthog.constants import AvailableFeature@@ -618,13 +619,21 @@ class TestQueryRunner(BaseTest):lambda: UserAccessControlError("query", "viewer", None),SloOutcome.SUCCESS,"user_error",+ False,),- ("concurrency_limit_exceeded", ConcurrencyLimitExceeded, SloOutcome.SUCCESS, "rate_limited"),- ("unclassified_value_error", ValueError, SloOutcome.FAILURE, "error"),+ ("concurrency_limit_exceeded", ConcurrencyLimitExceeded, SloOutcome.SUCCESS, "rate_limited", False),+ (+ "user_hogql_query_error",+ lambda: QueryError("Can't select a table when a column is expected: postgres_waitlist_entries"),+ SloOutcome.SUCCESS,+ "user_error",+ False,+ ),+ ("unclassified_value_error", ValueError, SloOutcome.FAILURE, "error", True),])def test_run_classifies_slo_error_at_except_boundary(- self, _name, exception_factory, expected_outcome, expected_error_category+ self, _name, exception_factory, expected_outcome, expected_error_category, expected_captured):TestQueryRunner = self.setup_test_query_runner_class()raised_exc = exception_factory()@@ -635,7 +644,10 @@ class TestQueryRunner(BaseTest):TestQueryRunner.calculate = calculate_raisesrunner = TestQueryRunner(query={"some_attr": "bla"}, team=self.team)- with mock.patch("posthog.slo.context.emit_slo_completed") as mock_emit_slo_completed:+ with (+ mock.patch("posthog.slo.context.emit_slo_completed") as mock_emit_slo_completed,+ mock.patch("posthog.hogql_queries.query_runner.capture_exception") as mock_capture_exception,+ ):with pytest.raises(type(raised_exc)):runner.run(execution_mode=ExecutionMode.CALCULATE_BLOCKING_ALWAYS)@@ -644,6 +656,11 @@ class TestQueryRunner(BaseTest):assert completed_kwargs["properties"].outcome == expected_outcomeassert completed_kwargs["extra_properties"]["error_category"] == expected_error_category+ # User-input errors (SUCCESS outcome) must not be captured to error tracking; only FAILURE+ # outcomes are. This is what keeps benign HogQL query errors out of error tracking.+ captured = any(call.args and call.args[0] is raised_exc for call in mock_capture_exception.call_args_list)+ assert captured == expected_captured+def test_query_execution_metrics_not_recorded_on_cache_hit(self):from posthog.clickhouse.query_tagging import reset_query_tagsfrom posthog.hogql_queries.query_runner import QUERY_EXECUTION_DURATION, QUERY_EXECUTION_TOTAL