Skip to content

: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.

fix(tasks): demote transient DB errors in task run metrics task (#68442)

posthog[bot]2eae671cbb9e
diff --git a/posthog/tasks/tasks.py b/posthog/tasks/tasks.py
index c17a30d6a36f..a851440181a0 100644
--- a/posthog/tasks/tasks.py
+++ b/posthog/tasks/tasks.py
@@ -7,7 +7,7 @@ if TYPE_CHECKING:
from uuid import UUID
from django.conf import settings
-from django.db import ProgrammingError, connection
+from django.db import OperationalError, ProgrammingError, connection
from django.utils import timezone
import requests
@@ -752,6 +752,11 @@ def capture_task_run_state_metrics() -> None:
# 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 OperationalError as err:
+ # Transient Postgres connection blips (connection-acquisition timeouts, dropped connections)
+ # on this every-60s gauge task are infra noise, not a real failure — demote to a warning so a
+ # momentary DB hiccup doesn't masquerade as a genuine error in error tracking.
+ logger.warning("capture_task_run_state_metrics_transient_db_error", 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.py
index 62f1498cc4ca..d74d00f7b378 100644
--- a/posthog/tasks/test/test_task_run_state_metrics.py
+++ b/posthog/tasks/test/test_task_run_state_metrics.py
@@ -4,7 +4,7 @@ from typing import TYPE_CHECKING, ClassVar, Optional
from unittest.mock import MagicMock, patch
from django.apps import apps
-from django.db import ProgrammingError
+from django.db import OperationalError, ProgrammingError
from django.test import TestCase
from django.utils import timezone
@@ -200,11 +200,17 @@ class TestCaptureTaskRunStateMetrics(TestCase):
== 1
)
- def test_missing_table_is_suppressed_without_reporting(self) -> None:
+ @parameterized.expand(
+ [
+ ("missing_table", ProgrammingError('relation "posthog_task_run" does not exist')),
+ ("transient_connection_timeout", OperationalError("connection timeout expired")),
+ ]
+ )
+ def test_transient_db_errors_are_suppressed_without_reporting(self, _name: str, error: Exception) -> None:
with (
patch(
"products.tasks.backend.facade.api.collect_task_run_state_metrics",
- side_effect=ProgrammingError('relation "posthog_task_run" does not exist'),
+ side_effect=error,
),
patch("posthog.tasks.tasks.capture_exception") as capture_mock,
):