: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): count terminal runs gauge by completed_at, not row touches (#108246)
Alessandro Pogliaghiba81ecf8f5bc
diff --git a/posthog/tasks/tasks.py b/posthog/tasks/tasks.pyindex ee6a45c27053..099785d57591 100644--- a/posthog/tasks/tasks.py+++ b/posthog/tasks/tasks.py@@ -845,8 +845,7 @@ def capture_task_run_state_metrics() -> None:labelnames=["status", "origin_product", "run_environment"],)- # Terminal runs are approximated by updated_at since completed_at can be null for- # FAILED/CANCELLED paths that didn't take the happy-path write.+ # Terminal runs count by completed_at, falling back to updated_at where FAILED/CANCELLED paths leave it null.metrics = tasks_facade.collect_task_run_state_metrics(open_statuses=_TASKS_RUN_OPEN_STATUSES,age_statuses=_TASKS_RUN_AGE_STATUSES,diff --git a/posthog/tasks/test/test_task_run_state_metrics.py b/posthog/tasks/test/test_task_run_state_metrics.pyindex c0249a1e4c76..a2acfbc56bec 100644--- a/posthog/tasks/test/test_task_run_state_metrics.py+++ b/posthog/tasks/test/test_task_run_state_metrics.py@@ -219,12 +219,14 @@ class TestCaptureTaskRunStateMetrics(TestCase):def test_emits_runs_terminal_1h_grouped_by_status_and_origin(self) -> None:Task = apps.get_model("tasks", "Task")TaskRun = apps.get_model("tasks", "TaskRun")- # 2 completed Slack runs + 1 failed User run in the last hour, plus 1 old completed run (outside 1h).+ # 2 completed Slack runs + 1 failed User run in the last hour, plus 2 old completed runs (outside 1h).self._make_task_run(origin=Task.OriginProduct.SLACK, status=TaskRun.Status.COMPLETED)self._make_task_run(origin=Task.OriginProduct.SLACK, status=TaskRun.Status.COMPLETED)self._make_task_run(origin=Task.OriginProduct.USER_CREATED, status=TaskRun.Status.FAILED)old = self._make_task_run(origin=Task.OriginProduct.SLACK, status=TaskRun.Status.COMPLETED)TaskRun.objects.filter(pk=old.pk).update(updated_at=timezone.now() - timedelta(hours=2))+ touched = self._make_task_run(origin=Task.OriginProduct.SLACK, status=TaskRun.Status.COMPLETED)+ TaskRun.objects.filter(pk=touched.pk).update(completed_at=timezone.now() - timedelta(hours=2))registry = self._run_with_registry()diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.pyindex e553cfb9f175..98a5d418cd65 100644--- a/products/tasks/backend/facade/api.py+++ b/products/tasks/backend/facade/api.py@@ -1683,7 +1683,9 @@ def collect_task_run_state_metrics(with_status=False,),terminal_recently=_gauge_rows(- TaskRun.objects.filter(status__in=terminal_statuses, updated_at__gte=window_start)+ TaskRun.objects.filter(status__in=terminal_statuses)+ .annotate(terminal_at=Coalesce(F("completed_at"), F("updated_at"), output_field=DateTimeField()))+ .filter(terminal_at__gte=window_start).values("status", "environment", "origin_product").annotate(count=Count("id")),"count",