: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(tasks): stop reporting UndefinedTable noise from TaskRun metrics (#65840)
posthog[bot]8ea33f1fa01a
diff --git a/posthog/tasks/tasks.py b/posthog/tasks/tasks.pyindex 1e4210aeea9e..dfe22556c245 100644--- a/posthog/tasks/tasks.py+++ b/posthog/tasks/tasks.py@@ -7,7 +7,7 @@ if TYPE_CHECKING:from uuid import UUIDfrom django.conf import settings-from django.db import connection, transaction+from django.db import ProgrammingError, connection, transactionfrom django.utils import timezoneimport requests@@ -649,6 +649,11 @@ def capture_task_run_state_metrics() -> None:run_environment=row.environment,).set(row.value)+ except ProgrammingError as err:+ # The tasks-product table isn't present in every environment/database. When the migration+ # hasn't been applied the COUNT query raises UndefinedTable — a benign, expected condition,+ # not an error worth reporting every minute.+ logger.debug("capture_task_run_state_metrics_missing_table", exception=err)except Exception as err:logger.exception("capture_task_run_state_metrics", exception=err)capture_exception(err)diff --git a/posthog/tasks/test/test_task_run_state_metrics.py b/posthog/tasks/test/test_task_run_state_metrics.pyindex 821ef4896b09..62f1498cc4ca 100644--- a/posthog/tasks/test/test_task_run_state_metrics.py+++ b/posthog/tasks/test/test_task_run_state_metrics.py@@ -4,6 +4,7 @@ from typing import TYPE_CHECKING, ClassVar, Optionalfrom unittest.mock import MagicMock, patchfrom django.apps import apps+from django.db import ProgrammingErrorfrom django.test import TestCasefrom django.utils import timezone@@ -199,6 +200,30 @@ class TestCaptureTaskRunStateMetrics(TestCase):== 1)+ def test_missing_table_is_suppressed_without_reporting(self) -> None:+ with (+ patch(+ "products.tasks.backend.facade.api.collect_task_run_state_metrics",+ side_effect=ProgrammingError('relation "posthog_task_run" does not exist'),+ ),+ patch("posthog.tasks.tasks.capture_exception") as capture_mock,+ ):+ self._run_with_registry()++ capture_mock.assert_not_called()++ def test_unexpected_errors_are_still_reported(self) -> None:+ with (+ patch(+ "products.tasks.backend.facade.api.collect_task_run_state_metrics",+ side_effect=ValueError("boom"),+ ),+ patch("posthog.tasks.tasks.capture_exception") as capture_mock,+ ):+ self._run_with_registry()++ capture_mock.assert_called_once()+def test_age_gauge_only_covers_queued_and_in_progress(self) -> None:Task = apps.get_model("tasks", "Task")TaskRun = apps.get_model("tasks", "TaskRun")