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(dashboards): evaluate customization flags remotely (#87129)

Matt Pua0e6b39c15094
diff --git a/posthog/api/test/dashboards/test_dashboard.py b/posthog/api/test/dashboards/test_dashboard.py
index 8ff011867e46..9f2ad55eeab0 100644
--- a/posthog/api/test/dashboards/test_dashboard.py
+++ b/posthog/api/test/dashboards/test_dashboard.py
@@ -4,8 +4,9 @@ import datetime
from freezegun import freeze_time
from posthog.test.base import APIBaseTest, FuzzyInt, QueryMatchingTest, snapshot_postgres_queries
from unittest import mock
-from unittest.mock import ANY, MagicMock, patch
+from unittest.mock import ANY, MagicMock, call, patch
+from django.conf import settings
from django.core.cache import cache
from django.test import override_settings
from django.utils.timezone import now
@@ -674,6 +675,51 @@ class TestDashboard(APIBaseTest, QueryMatchingTest):
Dashboard.objects.get(id=copied_id).customization, {"show_legend": False, "tile_spacing": "wide"}
)
+ @patch(
+ "products.dashboards.backend.feature_flags.get_flags_from_service",
+ return_value={"flags": {"dashboard-customization": {"enabled": True}}},
+ )
+ def test_dashboard_customization_uses_remote_flag_evaluation(self, _mock_get_flags: MagicMock) -> None:
+ dashboard_id, _ = self.dashboard_api.create_dashboard({"name": "dashboard"})
+
+ _, updated = self.dashboard_api.update_dashboard(
+ dashboard_id,
+ {"grid_spacing": "condensed", "layout_compaction": "horizontal"},
+ )
+
+ self.assertEqual(
+ updated["customization"],
+ {"tile_spacing": "condensed", "layout_compaction": "horizontal"},
+ )
+ expected_flag_call = call(
+ self.team.api_token,
+ self.user.distinct_id,
+ groups={"organization": str(self.team.organization_id), "project": str(self.team.id)},
+ flag_keys=["dashboard-customization"],
+ internal_request_token=settings.INTERNAL_REQUEST_TOKEN,
+ evaluation_runtime="all",
+ )
+ self.assertGreater(len(_mock_get_flags.call_args_list), 0)
+ self.assertTrue(all(flag_call == expected_flag_call for flag_call in _mock_get_flags.call_args_list))
+
+ @patch(
+ "products.dashboards.backend.feature_flags.get_flags_from_service",
+ side_effect=ConnectionError,
+ )
+ def test_dashboard_customization_fails_closed_when_remote_evaluation_fails(
+ self, _mock_get_flags: MagicMock
+ ) -> None:
+ dashboard_id, _ = self.dashboard_api.create_dashboard({"name": "dashboard"})
+
+ _, response = self.dashboard_api.update_dashboard(
+ dashboard_id,
+ {"grid_spacing": "condensed"},
+ expected_status=status.HTTP_400_BAD_REQUEST,
+ )
+
+ self.assertEqual(response["attr"], "grid_spacing")
+ self.assertEqual(response["detail"], "Tile density isn't available.")
+
@parameterized.expand([("horizontal",), ("stable",)])
@patch("products.dashboards.backend.api.dashboard.dashboard_customization_enabled", return_value=True)
def test_dashboard_layout_compaction_is_saved_and_duplicated(
diff --git a/products/dashboards/backend/feature_flags.py b/products/dashboards/backend/feature_flags.py
index 384f0afcf79c..5f8cf24c16ed 100644
--- a/products/dashboards/backend/feature_flags.py
+++ b/products/dashboards/backend/feature_flags.py
@@ -2,8 +2,9 @@ from __future__ import annotations
from typing import TYPE_CHECKING
-import posthoganalytics
+from django.conf import settings
+from posthog.api.services.flags_service import get_flags_from_service
from posthog.permissions import _FORCE_ENABLED_FLAGS
if TYPE_CHECKING:
@@ -23,16 +24,19 @@ def widget_flag_enabled(flag: str, *, team: Team, user: User | None = None) -> b
organization_id = str(team.organization_id)
project_id = str(team.id)
- return bool(
- posthoganalytics.feature_enabled(
- flag,
+ try:
+ # Cohort targeting needs the remote evaluator; process-local definitions do not include membership.
+ result = get_flags_from_service(
+ team.api_token,
distinct_id,
groups={"organization": organization_id, "project": project_id},
- group_properties={"organization": {"id": organization_id}, "project": {"id": project_id}},
- only_evaluate_locally=False,
- send_feature_flag_events=False,
+ flag_keys=[flag],
+ internal_request_token=settings.INTERNAL_REQUEST_TOKEN,
+ evaluation_runtime="all",
)
- )
+ except Exception:
+ return False
+ return bool(result.get("flags", {}).get(flag, {}).get("enabled"))
def dashboard_widgets_enabled(*, team: Team, user: User | None = None) -> bool: