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(replay): skip the session logs scan for teams that send no logs (#126162)

Jon McCalluma88d76a61f31
diff --git a/products/logs/backend/has_logs_query_runner.py b/products/logs/backend/has_logs_query_runner.py
index f282653ea37b..723f2127447d 100644
--- a/products/logs/backend/has_logs_query_runner.py
+++ b/products/logs/backend/has_logs_query_runner.py
@@ -10,6 +10,8 @@ from posthog.clickhouse.client.connection import Workload
from posthog.models import Team
HAS_LOGS_CACHE_TTL = int(dt.timedelta(days=7).total_seconds())
+# Short, so a team that starts sending logs sees them in replay within minutes.
+HAS_NO_LOGS_CACHE_TTL = int(dt.timedelta(minutes=5).total_seconds())
class HasLogsQueryRunner:
@@ -30,12 +32,15 @@ class HasLogsQueryRunner:
return len(response.results) > 0
-def team_has_logs(team: Team) -> bool:
+def team_has_logs(team: Team, cache_negative: bool = False) -> bool:
cache_key = f"team:{team.id}:has_logs"
- if cache.get(cache_key) is True:
- return True
+ cached = cache.get(cache_key)
+ if cached is True or (cache_negative and cached is False):
+ return cached
has_logs = HasLogsQueryRunner(team).run()
if has_logs:
cache.set(cache_key, True, HAS_LOGS_CACHE_TTL)
+ elif cache_negative:
+ cache.set(cache_key, False, HAS_NO_LOGS_CACHE_TTL)
return has_logs
diff --git a/products/logs/backend/presentation/views/api.py b/products/logs/backend/presentation/views/api.py
index 146a2ddaa3a2..9534d5ada73c 100644
--- a/products/logs/backend/presentation/views/api.py
+++ b/products/logs/backend/presentation/views/api.py
@@ -1633,7 +1633,10 @@ class LogsViewSet(TeamAndOrgViewSetMixin, PydanticModelMixin, viewsets.ViewSet):
# Note: cursor pagination no longer skips time-slicing because we narrow the date range
# to end at the cursor timestamp, allowing time-slicing to work on the remaining range.
try:
- if live_logs_checkpoint:
+ # Replay asks for session logs on every recording open, and most of those teams send no logs.
+ if query.sessionId and not team_has_logs(self.team, cache_negative=True):
+ results: list[Any] = []
+ elif live_logs_checkpoint:
response = LogsQueryRunner(query, self.team).run(
ExecutionMode.CALCULATE_BLOCKING_ALWAYS, analytics_props=analytics_props
)
diff --git a/products/logs/backend/test/test_has_logs_query_runner.py b/products/logs/backend/test/test_has_logs_query_runner.py
index 4b73904dd6f6..74e67d3d174e 100644
--- a/products/logs/backend/test/test_has_logs_query_runner.py
+++ b/products/logs/backend/test/test_has_logs_query_runner.py
@@ -144,3 +144,25 @@ class TestHasLogsAPI(ClickhouseTestMixin, APIBaseTest):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json(), {"hasLogs": False})
self.assertEqual(mock_runner.return_value.run.call_count, 2)
+
+ def test_session_query_skips_scan_for_team_without_logs(self):
+ cache.clear()
+ payload = {"query": {"dateRange": {"date_from": "-1h"}, "sessionId": "some-session"}}
+
+ with (
+ patch("products.logs.backend.has_logs_query_runner.HasLogsQueryRunner") as mock_has_logs,
+ patch("products.logs.backend.presentation.views.api.LogsQueryRunner") as mock_logs_runner,
+ ):
+ mock_has_logs.return_value.run.return_value = False
+
+ for _ in range(2):
+ response = self.client.post(f"/api/projects/{self.team.id}/logs/query", payload, format="json")
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ self.assertEqual(response.json()["results"], [])
+
+ mock_logs_runner.assert_not_called()
+ self.assertEqual(mock_has_logs.return_value.run.call_count, 1)
+
+ # The empty-state poll ignores the negative cache, so it notices the first log straight away.
+ self.client.get(f"/api/projects/{self.team.id}/logs/has_logs")
+ self.assertEqual(mock_has_logs.return_value.run.call_count, 2)