: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
feat(experiments): run the health segment query for impersonating staff (#126814)
Marcel Poelker10712ee32490
diff --git a/products/experiments/backend/health/rollout.py b/products/experiments/backend/health/rollout.pyindex d03d1afe4281..5e736f152ff3 100644--- a/products/experiments/backend/health/rollout.py+++ b/products/experiments/backend/health/rollout.py@@ -1,5 +1,6 @@import posthoganalytics+from posthog.clickhouse.query_tagging import get_query_tagsfrom posthog.models.team.team import Teamfrom posthog.models.user import Userfrom posthog.utils import flag_identity@@ -8,12 +9,24 @@ from posthog.utils import flag_identityHEALTH_FINDINGS_FLAG = "experiment-health-findings"+def runs_for_impersonated_session() -> bool:+ """+ Whether a staff member started this run in an impersonated session. A query runner has no session, so this reads+ the tag of the request, which follows an async run into the worker. The tag reads only the session cookie, so an+ OAuth token that was minted during impersonation is not an impersonated session here.+ """+ return bool(get_query_tags().is_impersonated)++def shows_health_findings(user: User, team: Team) -> bool:"""- Whether the web app shows the health findings to the user in the team. It evaluates the app's flag locally, from- the person and group inputs that the app sends to /flags, so it never waits on the network. An evaluation that- is undecided or fails is False.+ Whether the web app shows the health findings to the user in the team. An impersonated session always shows them,+ because the user here is the customer, whom the flag can leave out. Otherwise it evaluates the app's flag locally,+ from the person and group inputs that the app sends to /flags, so it never waits on the network. An evaluation+ that is undecided or fails is False."""+ if runs_for_impersonated_session():+ return Trueidentity = flag_identity(user, team, team.organization)if identity is None:return Falsediff --git a/products/experiments/backend/hogql_queries/experiment_exposures_query_runner.py b/products/experiments/backend/hogql_queries/experiment_exposures_query_runner.pyindex f63b2317f18d..c8ce296ca346 100644--- a/products/experiments/backend/hogql_queries/experiment_exposures_query_runner.py+++ b/products/experiments/backend/hogql_queries/experiment_exposures_query_runner.py@@ -67,7 +67,7 @@ from products.experiments.backend.health.registry import (EXPOSURE_HEALTH_CHECKS,evaluate as evaluate_health,)-from products.experiments.backend.health.rollout import shows_health_findings+from products.experiments.backend.health.rollout import runs_for_impersonated_session, shows_health_findingsfrom products.experiments.backend.hogql_queries import MULTIPLE_VARIANT_KEYfrom products.experiments.backend.hogql_queries.base_query_utils import analysis_window, analysis_window_endfrom products.experiments.backend.hogql_queries.error_handling import experiment_error_handler@@ -336,8 +336,8 @@ class ExperimentExposuresQueryRunner(ExperimentResultsCacheMixin, QueryRunner):def _runs_segments_query(self) -> bool:# The segments only explain a finding and the query scans the exposure events, so it runs only for a reader- # who sees the findings. A userless run sees none. The answer stays out of the cache key: an async run computes- # the key again in a worker, from that worker's flag definitions, and a key that disagrees with the web+ # who sees the findings. A userless run sees none. The flag's answer stays out of the cache key: an async run+ # computes the key again in a worker, from that worker's flag definitions, and a key that disagrees with the web# request's makes every page load miss the cache.if not isinstance(self.user, User):return False@@ -620,4 +620,8 @@ class ExperimentExposuresQueryRunner(ExperimentResultsCacheMixin, QueryRunner):# The test account filter finding asks the user to edit these filters, so an edit must not serve the old answer.if applies_test_account_filters(self.team, self.exposure_criteria):payload["test_account_filters"] = self.team.test_account_filters+ # An impersonated session reads the segments whether or not the customer has the flag, so it must not get the+ # customer's cached answer without them. The query tag follows an async run, so a worker computes the same key.+ if runs_for_impersonated_session():+ payload["impersonated_session"] = Truereturn payloaddiff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/test_experiment_exposures_query_runner.py b/products/experiments/backend/hogql_queries/test/experiment_query_runner/test_experiment_exposures_query_runner.pyindex 59610266c322..126e42048432 100644--- a/products/experiments/backend/hogql_queries/test/experiment_query_runner/test_experiment_exposures_query_runner.py+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/test_experiment_exposures_query_runner.py@@ -15,6 +15,7 @@ from rest_framework.exceptions import ValidationErrorfrom posthog.schema import (ActionsNode,+ CachedExperimentExposureQueryResponse,ExperimentEventExposureConfig,ExperimentExposureCriteria,ExperimentExposureHealthFindingCode,@@ -26,13 +27,15 @@ from posthog.schema import (from posthog.hogql.errors import ExposedHogQLErrorfrom posthog.hogql.query import execute_hogql_query-from posthog.clickhouse.query_tagging import AccessMethod, tags_context+from posthog.clickhouse.query_tagging import AccessMethod, reset_query_tags, tags_contextfrom posthog.constants import AvailableFeaturefrom posthog.exceptions import ClickHouseQueryTimeOut+from posthog.hogql_queries.query_runner import ExecutionModefrom posthog.hogql_queries.utils.breakdowns import BREAKDOWN_OTHER_STRING_LABELfrom posthog.models import PropertyDefinitionfrom posthog.models.activity_logging.activity_log import ActivityLogfrom posthog.models.user import User+from posthog.tasks.tasks import process_query_taskfrom posthog.test.persons import create_group_type_mappingfrom posthog.test.test_journeys import journeys_for@@ -1948,6 +1951,81 @@ class TestExperimentExposuresQueryRunner(ExperimentQueryRunnerBaseTest):],)+ def _expose_a_mismatch_located_in_one_library_version(self) -> None:+ ff_property = f"$feature/{self.feature_flag.key}"+ journeys_for(+ {+ f"user_{library}_{variant}_{i}": [+ {+ "event": "$feature_flag_called",+ "timestamp": "2024-01-02",+ "properties": {+ "$feature_flag_response": variant,+ ff_property: variant,+ "$feature_flag": self.feature_flag.key,+ "$lib": library,+ "$lib_version": "1.0.0",+ },+ }+ ]+ for library, counts in (+ ("web", (("control", 60), ("test", 60))),+ ("posthog-node", (("control", 15), ("test", 105))),+ )+ for variant, count in counts+ for i in range(count)+ },+ self.team,+ )+ flush_persons_and_events()++ @time_machine.travel("2024-01-07T12:00:00Z", tick=False)+ def test_impersonated_session_reads_the_segments_and_keeps_its_answer_apart_from_the_customers(self) -> None:+ self._expose_a_mismatch_located_in_one_library_version()+ query = self._running_exposure_query()++ def cached_and_reason() -> tuple[bool | None, str | None]:+ response = ExperimentExposuresQueryRunner(team=self.team, query=query, user=self.user).run(+ ExecutionMode.RECENT_CACHE_CALCULATE_BLOCKING_IF_STALE+ )+ assert isinstance(response, CachedExperimentExposureQueryResponse)+ [finding] = response.health_findings or []+ return response.is_cached, finding.reason++ customer = cached_and_reason()+ with tags_context(is_impersonated=True):+ impersonated = cached_and_reason()+ customer_again = cached_and_reason()++ self.assertEqual(+ [customer, impersonated, customer_again],+ [(False, None), (False, "library_version"), (True, None)],+ )++ @time_machine.travel("2024-01-07T12:00:00Z", tick=False)+ def test_async_run_of_an_impersonated_session_fills_the_entry_the_web_request_reads(self) -> None:+ self._expose_a_mismatch_located_in_one_library_version()+ query = self._running_exposure_query()++ def run(execution_mode: ExecutionMode) -> Any:+ return ExperimentExposuresQueryRunner(team=self.team, query=query, user=self.user).run(+ execution_mode, user=self.user+ )++ with tags_context(is_impersonated=True), patch("posthog.tasks.tasks.process_query_task.si") as queued_task:+ run(ExecutionMode.CALCULATE_ASYNC_ALWAYS)++ # A worker starts without the tags of the request, so only what the queued task carries reaches it.+ with tags_context():+ reset_query_tags()+ process_query_task.apply(args=queued_task.call_args.args, kwargs=queued_task.call_args.kwargs).get()++ with tags_context(is_impersonated=True):+ response = run(ExecutionMode.CACHE_ONLY_NEVER_CALCULATE)++ [finding] = response.health_findings or []+ self.assertEqual(finding.reason, "library_version")+def test_cache_key_keeps_the_flag_history_from_a_reader_who_cannot_view_the_flag(self) -> None:self.organization.available_product_features = [{"key": AvailableFeature.ACCESS_CONTROL, "name": AvailableFeature.ACCESS_CONTROL}