: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(signals): show event-filtered hogql observation metrics (#122717)
Chris Volzerc70a92d2df2b
diff --git a/docs/internal/signals-pr-lifecycle.md b/docs/internal/signals-pr-lifecycle.mdindex 3736438ef68d..829e80e79e99 100644--- a/docs/internal/signals-pr-lifecycle.md+++ b/docs/internal/signals-pr-lifecycle.md@@ -52,6 +52,12 @@ The metric must match the finding's filters, population, exclusions, window, andWhen no useful supported metric can measure that claim, the summary states the specific limitation in one short sentence.Qualitative findings can omit metrics.+Observation metrics support HogQL filters that compare direct event fields or event properties with literal values.+Boolean operations, arithmetic, and literal lists are supported; other function calls and subqueries remain unreadable.+Report reads, research selection, and snapshot refresh use the same filter check.+Snapshot refresh still requires access to both the live query and the shared measurement.+A viewer with a property grant can read the live query while the team's default property restrictions keep the shared snapshot hidden.+Research writes measurable outcome goals as `metric_threshold` report checks and investigative goals as `agent` checks. A metric check stores a bounded live query, baseline, comparison, and soak window; the query and display format are copied from its report metric when it names one. The check waits for the report to resolve, then the coordinator runs it and records a verdict. Metric checks keep the configured soak separate from their query window and wait until that full window contains only post-resolution data. Each run pins absolute query bounds to avoid reusing a pre-resolution cached result. Reopening clears the measurement start; the next resolution starts it again. Existing active metric checks without a recorded start begin their window at the first coordinator tick. New metric checks validate count, rate, duration, baseline, and threshold values using the same rules as report metrics; existing configurations remain readable. A later research pass reviews every open check, preserves unchanged checks and their approvals, replaces changed checks, and retires omitted checks. A failed verification turn leaves existing checks alone. Verification can revise only Expected impact; other summary sections and their chart links must remain intact. The full replacement schedule, including its soak and measurement window, must finish before the 90-day horizon. The Follow-up checks sidebar shows schedules and results for both kinds. The Expected impact section uses the same metric checks to show goals and charts, behind the person-level `signals-expected-impact` display flag. A person can mark those measurements "Looks good" as a quality signal; approval does not control scheduling or execution. The "Suggest different metrics" action starts a discussion that can atomically replace relevant open metric checks while preserving unrelated checks. A failed replacement leaves the original running, and a successful replacement starts unapproved. Report observation metrics remain separate from these forward-looking checks.Reports awaiting human input also retain their generated checks, pending resolution. Pending sidebar rows show the minimum wait after resolution. Metric replacements require access to the query they schedule and preserve the remaining recurring runs and soak duration, including zero minutes. Replacement cannot override the soak. Creation and replacement share full schedule validation against the 90-day horizon. A replacement rejects a check moved by a concurrent report merge; reload the report and retry on the survivor. Units cannot contain null characters or unpaired Unicode surrogates. The Expected impact section shows finished verdicts and refreshes after agent tasks change checks.diff --git a/products/signals/backend/report_metric_query_access.py b/products/signals/backend/report_metric_query_access.pyindex 8ba54b4dc985..5a0b2e3ade0b 100644--- a/products/signals/backend/report_metric_query_access.py+++ b/products/signals/backend/report_metric_query_access.py@@ -2,8 +2,56 @@from collections.abc import Callable, Mapping+from posthog.hogql import ast+from posthog.hogql.errors import BaseHogQLError+ORDINARY_PROPERTY_FILTER_TYPES = frozenset({"element", "event", "group", "person", "person_metadata", "session"})MAX_PROPERTY_FILTER_DEPTH = 20+HOGQL_FILTER_EVENT_FIELDS = frozenset({"event", "timestamp", "distinct_id", "uuid", "elements_chain"})+++def _hogql_filter_allows_read(expression: object) -> bool:+ if not isinstance(expression, str) or not expression.strip():+ return False++ from posthog.hogql.parser import parse_expr # noqa: PLC0415 — keeps the query parser off startup++ try:+ pending = [(parse_expr(expression), 0)]+ except (BaseHogQLError, SyntaxError, ValueError, RecursionError):+ return False++ # Shared snapshots cannot carry access to resources hidden behind subqueries or function calls.+ while pending:+ node, depth = pending.pop()+ if depth > MAX_PROPERTY_FILTER_DEPTH:+ return False+ if isinstance(node, ast.And | ast.Or | ast.Tuple | ast.Array):+ pending.extend((child, depth + 1) for child in node.exprs)+ elif isinstance(node, ast.CompareOperation | ast.ArithmeticOperation):+ if isinstance(node, ast.CompareOperation) and node.op in {+ ast.CompareOperationOp.InCohort,+ ast.CompareOperationOp.NotInCohort,+ }:+ return False+ pending.extend(((node.left, depth + 1), (node.right, depth + 1)))+ elif isinstance(node, ast.Not):+ pending.append((node.expr, depth + 1))+ elif isinstance(node, ast.Call) and node.name.lower() in {"and", "or", "not"}:+ if node.params or node.distinct or node.order_by or node.within_group or node.filter_expr:+ return False+ pending.extend((child, depth + 1) for child in node.args)+ elif isinstance(node, ast.Field):+ if not (+ len(node.chain) == 1+ and node.chain[0] in HOGQL_FILTER_EVENT_FIELDS+ or len(node.chain) > 1+ and node.chain[0] == "properties"+ ):+ return False+ elif not isinstance(node, ast.Constant):+ return False+ return Truedef property_filters_allow_read(filters: object, *, may_read_cohort: Callable[[int], bool] | None = None) -> bool:@@ -37,6 +85,9 @@ def property_filters_allow_read(filters: object, *, may_read_cohort: Callable[[ior (may_read_cohort is not None and not may_read_cohort(cohort_id))):return False+ elif filter_type == "hogql":+ if not _hogql_filter_allows_read(item.get("key")):+ return Falseelif not isinstance(filter_type, str) or filter_type not in ORDINARY_PROPERTY_FILTER_TYPES:return Falsereturn Truediff --git a/products/signals/backend/test/test_report_metric_access.py b/products/signals/backend/test/test_report_metric_access.pyindex fdc2bff50267..1e637082a306 100644--- a/products/signals/backend/test/test_report_metric_access.py+++ b/products/signals/backend/test/test_report_metric_access.py@@ -4,9 +4,11 @@ from typing import castfrom posthog.test.base import APIBaseTestfrom unittest.mock import Mock, patch+from parameterized import parameterizedfrom rest_framework.request import Requestfrom products.signals.backend.report_metric_access import ReportMetricAccessPolicy+from products.signals.backend.report_metric_query_access import query_filter_shape_allows_readclass TestReportMetricAccessPolicy(APIBaseTest):@@ -52,16 +54,36 @@ class TestReportMetricAccessPolicy(APIBaseTest):}return {"query": {"kind": "InsightVizNode", "source": source}}- def test_allows_ordinary_filters_in_each_supported_location(self) -> None:- ordinary_filter = {"type": "event", "key": "$browser", "operator": "exact", "value": "Chrome"}+ @parameterized.expand(+ [+ ("event", {"type": "event", "key": "$browser", "operator": "exact", "value": "Chrome"}),+ (+ "hogql",+ {+ "type": "hogql",+ "key": "elements_chain ILIKE '%purchase-button%' AND properties.$current_url ILIKE '%/basket%'",+ },+ ),+ ("hogql_not", {"type": "hogql", "key": "NOT (properties.$browser = 'Chrome')"}),+ ("hogql_tuple", {"type": "hogql", "key": "properties.$browser IN ('Chrome', 'Firefox')"}),+ ("hogql_array", {"type": "hogql", "key": "properties.$browser IN ['Chrome', 'Firefox']"}),+ (+ "hogql_arithmetic",+ {"type": "hogql", "key": "event = '$pageview' OR properties.amount + 1 > 3"},+ ),+ ]+ )+ def test_allows_supported_filters_in_each_supported_location(+ self, _name: str, property_filter: dict[str, object]+ ) -> None:metric = self._metric(- properties={"type": "AND", "values": [{"type": "OR", "values": [ordinary_filter]}]},+ properties={"type": "AND", "values": [{"type": "OR", "values": [property_filter]}]},series=[{"kind": "EventsNode","event": "$pageview","math": "dau",- "properties": [ordinary_filter],+ "properties": [property_filter],"fixedProperties": [{"type": "person", "key": "plan", "value": "free"}],}],@@ -71,6 +93,7 @@ class TestReportMetricAccessPolicy(APIBaseTest):assert policy.may_read_query(metric)assert policy.may_read_snapshot(metric)+ assert query_filter_shape_allows_read(cast(dict[str, object], metric["query"]))def test_requires_scopes_and_object_access_for_nested_cohorts(self) -> None:metric = self._metric(@@ -128,7 +151,14 @@ class TestReportMetricAccessPolicy(APIBaseTest):{"type": "future_resource", "key": "id", "value": 12},{"type": "cohort", "key": "name", "value": 12},{"type": "AND", "values": "not-a-list"},- {"type": "hogql", "key": "properties.secret"},+ {"type": "hogql", "key": "matchesAction(12)"},+ {"type": "hogql", "key": "person.cohorts = 12"},+ {"type": "hogql", "key": "distinct_id IN COHORT 12"},+ {"type": "hogql", "key": "distinct_id NOT IN COHORT 12"},+ {"type": "hogql", "key": "person_id IN (SELECT person_id FROM cohortpeople WHERE cohort_id = 12)"},+ {"type": "hogql", "key": "properties.$browser ="},+ {"type": "hogql", "key": ""},+ {"type": "hogql", "key": None},{"type": [], "key": "id", "value": 12},{"type": {}, "key": "id", "value": 12},]@@ -136,7 +166,10 @@ class TestReportMetricAccessPolicy(APIBaseTest):policy = self._policy(["query:read", "event_definition:read", "cohort:read"])for property_filter in invalid_filters:with self.subTest(property_filter=property_filter):- assert not policy.may_read_query(self._metric(properties=[property_filter]))+ metric = self._metric(properties=[property_filter])+ assert not policy.may_read_query(metric)+ assert not policy.may_read_snapshot(metric)+ assert not query_filter_shape_allows_read(cast(dict[str, object], metric["query"]))assert not policy.may_read_query(self._metric(conversionGoal={"actionId": 42, "futureField": True}))assert not policy.may_read_query(self._metric(conversionGoal={"futureResourceId": 42}))diff --git a/products/signals/backend/test/test_report_metric_refresh.py b/products/signals/backend/test/test_report_metric_refresh.pyindex cb4f67d8eb56..3333ce1e2d48 100644--- a/products/signals/backend/test/test_report_metric_refresh.py+++ b/products/signals/backend/test/test_report_metric_refresh.py@@ -92,8 +92,20 @@ class TestReportMetricRefreshApi(APIBaseTest):def _refresh(self, *reports: SignalReport):return self.client.post(self._url(), {"report_ids": [str(report.id) for report in reports]}, format="json")- def test_refresh_replaces_only_the_numbers_and_returns_snapshots(self) -> None:- report = self._report()+ @parameterized.expand(+ [+ ("event", None, 17),+ ("hogql", "properties.$browser = 'Chrome'", 17),+ ("hogql_without_snapshot", "properties.$browser = 'Chrome'", None),+ ]+ )+ def test_refresh_replaces_only_the_numbers_and_returns_snapshots(+ self, _name: str, expression: str | None, value: float | None+ ) -> None:+ metric = _metric(value=value, value_at=None if value is None else "2026-08-29T12:00:00Z")+ if expression is not None:+ metric["query"]["source"]["series"][0]["properties"] = [{"type": "hogql", "key": expression}]+ report = self._report(metrics=[metric])before = SignalReport.objects.get(id=report.id)measured_at = timezone.now().replace(microsecond=0)@@ -115,9 +127,13 @@ class TestReportMetricRefreshApi(APIBaseTest):assert after.metrics[0]["query"] == before.metrics[0]["query"]assert (after.title, after.summary, after.updated_at) == (before.title, before.summary, before.updated_at)- def test_fresh_snapshot_is_served_without_a_query(self) -> None:+ @parameterized.expand([("event", None), ("hogql", "properties.$browser = 'Chrome'")])+ def test_fresh_snapshot_is_served_without_a_query(self, _name: str, expression: str | None) -> None:recent = (timezone.now() - timedelta(minutes=1)).isoformat()- report = self._report(metrics=[_metric(value_at=recent)])+ metric = _metric(value_at=recent)+ if expression is not None:+ metric["query"]["source"]["series"][0]["properties"] = [{"type": "hogql", "key": expression}]+ report = self._report(metrics=[metric])with patch(_MEASURE) as measure:response = self._refresh(report)@@ -169,7 +185,10 @@ class TestReportMetricRefreshApi(APIBaseTest):assert response.json()["reports"][0]["metrics"][0]["value"] == 17.0assert SignalReport.objects.get(id=report.id).metrics[0]["value"] == 17- def test_property_restricted_member_cannot_trigger_a_shared_refresh(self) -> None:+ @parameterized.expand([("event", None), ("hogql", "properties.secret_plan = 'enterprise'")])+ def test_property_restricted_member_cannot_trigger_a_shared_refresh(+ self, _name: str, expression: str | None+ ) -> None:self.organization.available_product_features = [{"key": AvailableFeature.PROPERTY_ACCESS_CONTROL, "name": AvailableFeature.PROPERTY_ACCESS_CONTROL}]@@ -183,7 +202,10 @@ class TestReportMetricRefreshApi(APIBaseTest):organization_member=self.organization_membership,access_level=PropertyAccessLevel.NONE.value,)- report = self._report()+ metric = _metric()+ if expression is not None:+ metric["query"]["source"]["series"][0]["properties"] = [{"type": "hogql", "key": expression}]+ report = self._report(metrics=[metric])with patch(_MEASURE) as measure:response = self._refresh(report)diff --git a/products/signals/backend/test/test_signal_report_api.py b/products/signals/backend/test/test_signal_report_api.pyindex 9d86c594bb7a..6bd396b2f27b 100644--- a/products/signals/backend/test/test_signal_report_api.py+++ b/products/signals/backend/test/test_signal_report_api.py@@ -371,7 +371,13 @@ class TestSignalReportListAPI(APIBaseTest):art.save()return art- def test_list_and_retrieve_include_typed_impact_metrics_without_running_them(self) -> None:+ @parameterized.expand([("event", None), ("hogql", "properties.$browser = 'Chrome'")])+ def test_list_and_retrieve_include_typed_impact_metrics_without_running_them(+ self, _name: str, expression: str | None+ ) -> None:+ query = trends_metric_query(series=[{"kind": "EventsNode", "event": "$exception", "math": "dau"}])+ if expression is not None:+ query["source"]["series"][0]["properties"] = [{"type": "hogql", "key": expression}]metric = {"metric_id": "affected-users","title": "Affected users",@@ -382,7 +388,7 @@ class TestSignalReportListAPI(APIBaseTest):"series": [3, 5, 9],"value_format": "count","unit": "users",- "query": trends_metric_query(series=[{"kind": "EventsNode", "event": "$exception", "math": "dau"}]),+ "query": query,"caption": "Unique people in the last 30 days.","comparison": None,}@@ -478,7 +484,10 @@ class TestSignalReportListAPI(APIBaseTest):assert "comparison" not in detail_metricassert detail_metric["query"] is None- def test_viewer_property_grant_keeps_live_query_but_hides_userless_snapshot(self) -> None:+ @parameterized.expand([("event", None), ("hogql", "properties.secret_plan = 'enterprise'")])+ def test_viewer_property_grant_keeps_live_query_but_hides_userless_snapshot(+ self, _name: str, expression: str | None+ ) -> None:self._enable_feature(AvailableFeature.PROPERTY_ACCESS_CONTROL)property_definition = PropertyDefinition.objects.create(team=self.team,@@ -498,6 +507,8 @@ class TestSignalReportListAPI(APIBaseTest):access_level=PropertyAccessLevel.READ.value,)metric = self._affected_users_metric(series={"kind": "EventsNode", "event": "$exception", "math": "dau"})+ if expression is not None:+ metric["query"]["source"]["series"][0]["properties"] = [{"type": "hogql", "key": expression}]report = self._create_report(metrics=[metric])list_response = self.client.get(self._list_url())