: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
perf(replay-vision): index replay observations by team and session (#92375)
posthog[bot]88c8b7bf0d52
diff --git a/products/replay_vision/backend/api/observations.py b/products/replay_vision/backend/api/observations.pyindex 132de975bb09..ac4f3d3dd3df 100644--- a/products/replay_vision/backend/api/observations.py+++ b/products/replay_vision/backend/api/observations.py@@ -1211,20 +1211,18 @@ class SessionReplayObservationViewSet(ReplayObservationViewSet):# targeting included), then gate each row against the experiment in its own snapshot so a# retargeted scanner can't surface historical rows the caller can't access.readable_scanner_ids = readable_observation_scanner_ids(self.user_access_control, self.team_id)- queryset = hydrate_for_serialization(- accessible_observations(- self.user_access_control,- self.team_id,- queryset.filter(team_id=self.team_id, scanner_id__in=readable_scanner_ids),- )- ).order_by("-created_at", "id")- # A bare list would scan the whole team's observation history; the replay page always has a session.+ queryset = queryset.filter(team_id=self.team_id, scanner_id__in=readable_scanner_ids)+ # Scope to the session before the experiment gate runs: the replay page always has a session, so+ # filtering session_id first keeps the access lookup off the team's whole observation history.+ # A bare list would scan that history.if self.action == "list":session_id = self.request.query_params.get("session_id")if not session_id:raise ValidationError("The `session_id` query parameter is required.")queryset = queryset.filter(session_id=session_id)- return queryset+ return hydrate_for_serialization(+ accessible_observations(self.user_access_control, self.team_id, queryset)+ ).order_by("-created_at", "id")# Hide `stats/` on the session-scoped viewset — it has no `parent_lookup_scanner_id` to dispatch on.def stats(self, request: Request, **kwargs: Any) -> Response: # type: ignore[override]diff --git a/products/replay_vision/backend/tests/test_access_control.py b/products/replay_vision/backend/tests/test_access_control.pyindex a255da9b9cb1..37edf9f2293c 100644--- a/products/replay_vision/backend/tests/test_access_control.py+++ b/products/replay_vision/backend/tests/test_access_control.py@@ -405,3 +405,25 @@ class TestReplayScannerAccessControl(_AccessControlTestCase):with CaptureQueriesContext(connection) as six:self.assertEqual(self.client.get(dock_url).status_code, 200)self.assertEqual(len(one.captured_queries), len(six.captured_queries))++ def test_dock_read_scopes_every_observation_query_to_the_session(self) -> None:+ # The experiment-access lookup runs before the row read, so it must carry the session_id+ # predicate too. If it doesn't, it scans the team's whole observation history.+ # Assert every observation-table read is session-scoped.+ self._set_resource_default("replay_scanner", "editor")+ self._set_resource_default("session_recording", "editor")+ scanner = self._create_scanner(name="dock")+ ReplayObservation.objects.create(scanner=scanner, session_id="sess-1", scanner_snapshot=snapshot_for(scanner))+ dock_url = f"/api/environments/{self.team.id}/vision/observations/?session_id=sess-1"++ self.client.force_login(self.other_user)+ self.client.get(dock_url) # warm request-scoped caches so the capture is the read itself.+ with CaptureQueriesContext(connection) as queries:+ self.assertEqual(self.client.get(dock_url).status_code, 200)++ observation_reads = [+ q["sql"] for q in queries.captured_queries if 'FROM "replay_vision_replayobservation"' in q["sql"]+ ]+ self.assertTrue(observation_reads, "expected the dock read to query the observation table")+ for sql in observation_reads:+ self.assertIn("session_id", sql, sql)