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(metrics): gate the Fundamentals tab behind its own flag (#88305)

Jon McCallum4373e4cbc357
diff --git a/frontend/src/lib/constants.tsx b/frontend/src/lib/constants.tsx
index 909044e17e67..a4ddafc081f2 100644
--- a/frontend/src/lib/constants.tsx
+++ b/frontend/src/lib/constants.tsx
@@ -404,6 +404,7 @@ export const FEATURE_FLAGS = {
MESSAGING_SES: 'messaging-ses', // owner #team-workflows
METRICS: 'metrics', // owner: #team-apm (@jonmcwest, @frankh)
METRICS_ERROR_OVERLAYS: 'metrics-error-overlays', // owner: #team-apm — gates the error-spike overlay PoC on metrics charts
+ METRICS_FUNDAMENTALS: 'metrics-fundamentals', // owner: #team-apm (@jonmcwest, @frankh), gates the Fundamentals tab and the explain API behind it, which check the metrics viewer's own reductions
NEW_TAB_PROJECT_EXPLORER: 'new-tab-project-explorer', // owner: #team-platform-ux
NEW_TEAM_CORE_EVENTS: 'new-team-core-events', // owner: @jabahamondes #team-web-analytics
NOTEBOOK_GENERATED_WIDGETS: 'notebook-generated-widgets', // owner: #team-data-tools
diff --git a/products/metrics/backend/facade/contracts.py b/products/metrics/backend/facade/contracts.py
index 44851ff9e2ed..f4a8cce3bfdb 100644
--- a/products/metrics/backend/facade/contracts.py
+++ b/products/metrics/backend/facade/contracts.py
@@ -36,6 +36,12 @@ METRICS_FEATURE_FLAG = "metrics"
# Staff-only while it is a proof of concept.
METRICS_ERROR_OVERLAYS_FEATURE_FLAG = "metrics-error-overlays"
+# Fundamentals recomputes a chart point from its raw samples so the viewer's own
+# reductions can be checked. That makes it a tool for the people who build the
+# viewer, not a feature for the teams on the alpha, so it needs a gate of its own
+# on top of METRICS_FEATURE_FLAG.
+METRICS_FUNDAMENTALS_FEATURE_FLAG = "metrics-fundamentals"
+
@dataclass(frozen=True, slots=True)
class MetricFilter:
diff --git a/products/metrics/backend/presentation/api.py b/products/metrics/backend/presentation/api.py
index 48f2a46661dc..2e9b3fab58ab 100644
--- a/products/metrics/backend/presentation/api.py
+++ b/products/metrics/backend/presentation/api.py
@@ -42,6 +42,7 @@ from products.metrics.backend.facade.contracts import (
MAX_CLAUSES_PER_QUERY,
METRICS_ERROR_OVERLAYS_FEATURE_FLAG,
METRICS_FEATURE_FLAG,
+ METRICS_FUNDAMENTALS_FEATURE_FLAG,
MetricFilter,
MetricGroupBy,
MetricQueryClause,
@@ -1103,6 +1104,20 @@ class MetricsViewSet(TeamAndOrgViewSetMixin, viewsets.ViewSet):
"""Take one chart point apart into the series and samples behind it,
and recompute it independently so the plotted number can be checked
rather than trusted."""
+ # The class-level gate admits every team on the metrics alpha, which is wider
+ # than this action should be. Fundamentals is a correctness tool for the people
+ # who build the viewer, so it carries its own flag. Without this check the tab
+ # is hidden in the UI but the data behind it stays one POST away.
+ if not posthog_feature_flag_enabled(
+ METRICS_FUNDAMENTALS_FEATURE_FLAG,
+ str(cast(User, request.user).distinct_id),
+ organization_id=self.team.organization_id,
+ team_id=self.team.pk,
+ ):
+ raise PermissionDenied(
+ f"This action requires feature flag {METRICS_FUNDAMENTALS_FEATURE_FLAG!r} to be enabled for your organization."
+ )
+
tag_queries(product=Product.METRICS, feature=Feature.QUERY)
body = _MetricExplainRequestSerializer(data=request.data)
diff --git a/products/metrics/backend/tests/conftest.py b/products/metrics/backend/tests/conftest.py
index 9f3bbde359a7..f1f7afba5c31 100644
--- a/products/metrics/backend/tests/conftest.py
+++ b/products/metrics/backend/tests/conftest.py
@@ -3,15 +3,18 @@ from collections.abc import Iterator
import pytest
from unittest.mock import patch
+from products.metrics.backend.facade.contracts import METRICS_FUNDAMENTALS_FEATURE_FLAG
+
@pytest.fixture(autouse=True)
def enable_metrics_feature_flag() -> Iterator[None]:
# MetricsViewSet is gated behind the `metrics` feature flag (private alpha), so every
- # endpoint test in this package would 403 without it. Enable only the metrics flag and
- # leave all others at their default (False), matching production for non-alpha teams.
- # The dedicated gate test overrides this to assert the 403.
+ # endpoint test in this package would 403 without it. The explain action carries a
+ # second flag on top. Enable both and leave all others at their default (False),
+ # matching production for alpha teams. The dedicated gate tests override this to
+ # assert the 403.
def _feature_enabled(flag_key: str, *args: object, **kwargs: object) -> bool:
- return flag_key == "metrics"
+ return flag_key in ("metrics", METRICS_FUNDAMENTALS_FEATURE_FLAG)
with patch("posthoganalytics.feature_enabled", side_effect=_feature_enabled):
yield
diff --git a/products/metrics/backend/tests/test_api.py b/products/metrics/backend/tests/test_api.py
index 646ca4f4d90f..7b8b37f9d273 100644
--- a/products/metrics/backend/tests/test_api.py
+++ b/products/metrics/backend/tests/test_api.py
@@ -23,6 +23,7 @@ from products.access_control.backend.facade.user_access_control import (
)
from products.access_control.backend.models.access_control import AccessControl
from products.error_tracking.backend.facade.testing import create_issue, create_spike_event
+from products.metrics.backend.facade.contracts import METRICS_FUNDAMENTALS_FEATURE_FLAG
def test_metrics_app_is_installed():
@@ -122,6 +123,23 @@ class TestMetricsFeatureFlagGate(APIBaseTest):
assert response.status_code == expected_status
+ @parameterized.expand(
+ [
+ ("enabled", True, status.HTTP_400_BAD_REQUEST),
+ ("disabled", False, status.HTTP_403_FORBIDDEN),
+ ]
+ )
+ def test_fundamentals_flag_gates_the_explain_action(
+ self, _name: str, fundamentals_enabled: bool, expected_status: int
+ ) -> None:
+ def feature_enabled(flag: str, *args: object, **kwargs: object) -> bool:
+ return fundamentals_enabled if flag == METRICS_FUNDAMENTALS_FEATURE_FLAG else True
+
+ with patch("posthoganalytics.feature_enabled", side_effect=feature_enabled):
+ response = self.client.post(f"/api/projects/{self.team.id}/metrics/explain/", {}, format="json")
+
+ assert response.status_code == expected_status
+
@pytest.mark.ee
class TestMetricsAccessControl(APIBaseTest):
diff --git a/products/metrics/frontend/MetricsScene.tsx b/products/metrics/frontend/MetricsScene.tsx
index 77a83e261a13..901ce9eaa2c0 100644
--- a/products/metrics/frontend/MetricsScene.tsx
+++ b/products/metrics/frontend/MetricsScene.tsx
@@ -3,6 +3,7 @@ import posthog from 'posthog-js'
import { LemonBanner, LemonButton, LemonTabs } from '@posthog/lemon-ui'
+import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
import { IconFeedback } from 'lib/lemon-ui/icons'
import { getAccessControlDisabledReason } from 'lib/utils/accessControlUtils'
import { sceneConfigurations } from 'scenes/scenes'
@@ -22,7 +23,7 @@ import { metricsUsageTrackingLogic } from './components/metricsUsageTrackingLogi
import { MetricsViewer } from './components/MetricsViewer'
import { metricsEmptyState } from './emptyState/metricsEmptyState'
import { metricsFeaturePreviewGate } from './featurePreviewGate'
-import { MetricsSceneActiveTab, metricsSceneLogic } from './metricsSceneLogic'
+import { DEFAULT_ACTIVE_TAB, MetricsSceneActiveTab, metricsSceneLogic } from './metricsSceneLogic'
export const METRICS_LOGIC_KEY = 'metrics'
@@ -55,6 +56,13 @@ export function MetricsScene(): JSX.Element {
const MetricsSceneContent = (): JSX.Element => {
const { activeTab } = useValues(metricsSceneLogic)
const { setActiveTab } = useActions(metricsSceneLogic)
+ // Fundamentals checks the viewer's own reductions against the raw samples, so it is
+ // built for the people who work on the viewer rather than for the teams on the alpha.
+ const fundamentalsEnabled = useFeatureFlag('METRICS_FUNDAMENTALS')
+ const visibleTabs = fundamentalsEnabled ? TABS : TABS.filter((tab) => tab.key !== 'fundamentals')
+ // A guessed ?activeTab=fundamentals must not render the tab either, so fall back to the
+ // default tab instead of leaving the scene with no visible content.
+ const effectiveTab = activeTab === 'fundamentals' && !fundamentalsEnabled ? DEFAULT_ACTIVE_TAB : activeTab
const metricsViewerDisabledReason = getAccessControlDisabledReason(
AccessControlResourceType.Metrics,
AccessControlLevel.Viewer
@@ -106,23 +114,23 @@ const MetricsSceneContent = (): JSX.Element => {
Metrics is in alpha. Please share feedback on how to improve the product.
</LemonBanner>
<LemonTabs<MetricsSceneActiveTab>
- activeKey={activeTab}
+ activeKey={effectiveTab}
onChange={(tab) => {
if (!tabDisabledReasons[tab]) {
setActiveTab(tab)
}
}}
- tabs={TABS.map((tab) => ({
+ tabs={visibleTabs.map((tab) => ({
...tab,
disabledReason: tabDisabledReasons[tab.key] ?? undefined,
}))}
sceneInset
/>
<div className="flex flex-col gap-2 py-2 flex-1 min-h-0">
- {activeTab === 'overview' && <MetricsOverview />}
- {activeTab === 'viewer' && <MetricsViewer />}
- {activeTab === 'sql' && <MetricsSqlEditor />}
- {activeTab === 'fundamentals' && <MetricsFundamentals />}
+ {effectiveTab === 'overview' && <MetricsOverview />}
+ {effectiveTab === 'viewer' && <MetricsViewer />}
+ {effectiveTab === 'sql' && <MetricsSqlEditor />}
+ {effectiveTab === 'fundamentals' && <MetricsFundamentals />}
</div>
</>
)