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(web-analytics): resolve authorized_urls health issue on team save (#58333)

Javier Bahamondes054b1b2d43f5
diff --git a/posthog/models/team/team.py b/posthog/models/team/team.py
index 619560711570..e73fecb807bc 100644
--- a/posthog/models/team/team.py
+++ b/posthog/models/team/team.py
@@ -1091,6 +1091,18 @@ def delete_team_in_cache_on_delete(sender, instance: Team, **kwargs):
set_team_in_cache(instance.api_token, None)
+@mutable_receiver(post_save, sender=Team)
+def reevaluate_authorized_urls_health(sender, instance: Team, **kwargs):
+ update_fields = kwargs.get("update_fields")
+ if update_fields is not None and "app_urls" not in update_fields:
+ return
+
+ from posthog.tasks.health_checks import evaluate_health_check_for_team
+
+ team_id = instance.id
+ transaction.on_commit(lambda: evaluate_health_check_for_team.delay("authorized_urls", team_id))
+
+
def check_is_feature_available_for_team(team_id: int, feature_key: str, current_usage: Optional[int] = None):
available_product_features: Optional[list[dict[str, str]]] = (
Team.objects.select_related("organization")
diff --git a/posthog/tasks/__init__.py b/posthog/tasks/__init__.py
index 4d30c1a1f566..c02d0e9ba529 100644
--- a/posthog/tasks/__init__.py
+++ b/posthog/tasks/__init__.py
@@ -10,6 +10,7 @@ from . import (
email,
exporter,
feature_flags,
+ health_checks,
heatmap_screenshot,
hog_flows,
hog_functions,
@@ -39,6 +40,7 @@ __all__ = [
"email",
"exporter",
"feature_flags",
+ "health_checks",
"heatmap_screenshot",
"hog_flows",
"hog_functions",
diff --git a/posthog/tasks/health_checks.py b/posthog/tasks/health_checks.py
new file mode 100644
index 000000000000..dae5732448aa
--- /dev/null
+++ b/posthog/tasks/health_checks.py
@@ -0,0 +1,22 @@
+from celery import shared_task
+from structlog import get_logger
+
+from posthog.tasks.utils import CeleryQueue
+
+logger = get_logger(__name__)
+
+
+@shared_task(ignore_result=True, queue=CeleryQueue.DEFAULT.value)
+def evaluate_health_check_for_team(kind: str, team_id: int) -> None:
+ # Deferred: posthog.dags.__init__ calls django.setup() and would re-enter if loaded during boot.
+ from posthog.temporal.health_checks.processing import _process_batch_detection
+ from posthog.temporal.health_checks.registry import ensure_registry_loaded, get_detect_fn
+
+ ensure_registry_loaded()
+ try:
+ detect_fn = get_detect_fn(kind)
+ except KeyError:
+ logger.warning("evaluate_health_check_for_team.unknown_kind", kind=kind, team_id=team_id)
+ return
+
+ _process_batch_detection(team_ids=[team_id], kind=kind, detect_fn=detect_fn)
diff --git a/posthog/tasks/test/__snapshots__/test_task_registration.ambr b/posthog/tasks/test/__snapshots__/test_task_registration.ambr
index b4c77e96944e..535e4f14d7d4 100644
--- a/posthog/tasks/test/__snapshots__/test_task_registration.ambr
+++ b/posthog/tasks/test/__snapshots__/test_task_registration.ambr
@@ -70,6 +70,7 @@
'posthog.tasks.feature_flags.refresh_expiring_flags_cache_entries',
'posthog.tasks.feature_flags.update_team_flags_cache',
'posthog.tasks.feature_flags.update_team_service_flags_cache',
+ 'posthog.tasks.health_checks.evaluate_health_check_for_team',
'posthog.tasks.heatmap_screenshot.generate_heatmap_screenshot',
'posthog.tasks.hog_flows.refresh_affected_hog_flows',
'posthog.tasks.hog_functions.refresh_affected_hog_functions',
diff --git a/products/web_analytics/backend/test/test_authorized_urls.py b/products/web_analytics/backend/test/test_authorized_urls.py
new file mode 100644
index 000000000000..26e5eec07314
--- /dev/null
+++ b/products/web_analytics/backend/test/test_authorized_urls.py
@@ -0,0 +1,98 @@
+from posthog.test.base import BaseTest
+from unittest.mock import patch
+
+from posthog.models.health_issue import HealthIssue
+from posthog.tasks.health_checks import evaluate_health_check_for_team
+
+from products.web_analytics.backend.temporal.health_checks.authorized_urls import AuthorizedUrlsCheck
+
+
+class TestAuthorizedUrlsEvaluation(BaseTest):
+ def test_creates_issue_when_team_has_no_app_urls(self):
+ self.team.app_urls = []
+ self.team.save()
+ HealthIssue.objects.filter(team_id=self.team.id, kind=AuthorizedUrlsCheck.kind).delete()
+
+ evaluate_health_check_for_team(AuthorizedUrlsCheck.kind, self.team.id)
+
+ issues = HealthIssue.objects.filter(
+ team_id=self.team.id,
+ kind=AuthorizedUrlsCheck.kind,
+ status=HealthIssue.Status.ACTIVE,
+ )
+ self.assertEqual(issues.count(), 1)
+ self.assertEqual(issues.get().severity, HealthIssue.Severity.WARNING)
+
+ def test_resolves_active_issue_when_team_has_app_urls(self):
+ self.team.app_urls = []
+ self.team.save()
+ evaluate_health_check_for_team(AuthorizedUrlsCheck.kind, self.team.id)
+ self.assertEqual(
+ HealthIssue.objects.filter(
+ team_id=self.team.id, kind=AuthorizedUrlsCheck.kind, status=HealthIssue.Status.ACTIVE
+ ).count(),
+ 1,
+ )
+
+ self.team.app_urls = ["https://example.com"]
+ self.team.save()
+ evaluate_health_check_for_team(AuthorizedUrlsCheck.kind, self.team.id)
+
+ self.assertEqual(
+ HealthIssue.objects.filter(
+ team_id=self.team.id, kind=AuthorizedUrlsCheck.kind, status=HealthIssue.Status.ACTIVE
+ ).count(),
+ 0,
+ )
+ self.assertEqual(
+ HealthIssue.objects.filter(
+ team_id=self.team.id, kind=AuthorizedUrlsCheck.kind, status=HealthIssue.Status.RESOLVED
+ ).count(),
+ 1,
+ )
+
+ def test_idempotent_for_healthy_team(self):
+ self.team.app_urls = ["https://example.com"]
+ self.team.save()
+
+ evaluate_health_check_for_team(AuthorizedUrlsCheck.kind, self.team.id)
+ evaluate_health_check_for_team(AuthorizedUrlsCheck.kind, self.team.id)
+
+ self.assertEqual(
+ HealthIssue.objects.filter(team_id=self.team.id, kind=AuthorizedUrlsCheck.kind).count(),
+ 0,
+ )
+
+
+class TestAuthorizedUrlsSignal(BaseTest):
+ @patch("posthog.tasks.health_checks.evaluate_health_check_for_team")
+ def test_team_save_dispatches_task_on_commit(self, mock_task):
+ with self.captureOnCommitCallbacks(execute=True):
+ self.team.app_urls = ["https://example.com"]
+ self.team.save()
+
+ mock_task.delay.assert_called_with("authorized_urls", self.team.id)
+
+ @patch("posthog.tasks.health_checks.evaluate_health_check_for_team")
+ def test_task_not_dispatched_before_commit(self, mock_task):
+ with self.captureOnCommitCallbacks(execute=False):
+ self.team.app_urls = ["https://example.com"]
+ self.team.save()
+
+ mock_task.delay.assert_not_called()
+
+ @patch("posthog.tasks.health_checks.evaluate_health_check_for_team")
+ def test_task_not_dispatched_when_app_urls_not_in_update_fields(self, mock_task):
+ with self.captureOnCommitCallbacks(execute=True):
+ self.team.name = "renamed"
+ self.team.save(update_fields=["name"])
+
+ mock_task.delay.assert_not_called()
+
+ @patch("posthog.tasks.health_checks.evaluate_health_check_for_team")
+ def test_task_dispatched_when_app_urls_in_update_fields(self, mock_task):
+ with self.captureOnCommitCallbacks(execute=True):
+ self.team.app_urls = ["https://example.com"]
+ self.team.save(update_fields=["app_urls"])
+
+ mock_task.delay.assert_called_with("authorized_urls", self.team.id)