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(logs): cut a 20s query from the services tab and unclip pagination (#82454)

Jon McCallumd302675411f8
diff --git a/products/logs/backend/services_query_runner.py b/products/logs/backend/services_query_runner.py
index 955c4363839f..4cb7498bb240 100644
--- a/products/logs/backend/services_query_runner.py
+++ b/products/logs/backend/services_query_runner.py
@@ -224,24 +224,9 @@ class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunn
sparkline_rows = sparkline_response.results
# True distinct-service count, unaffected by SERVICES_LIMIT — the UI
- # uses it to disclose truncation. An aggregates result under the cap is
- # already the complete distinct set, so only run the extra count when
- # the cap was actually hit.
- if len(aggregates_response.results) < SERVICES_LIMIT:
- total_services = len(aggregates_response.results)
- else:
- total_response = execute_hogql_query(
- query_type="LogsQuery",
- query=self._total_services_query(),
- modifiers=self.modifiers,
- team=self.team,
- workload=Workload.LOGS,
- timings=self.timings,
- limit_context=self.limit_context,
- filters=self.query_date_range.to_hogql_filters(),
- settings=self.settings,
- )
- total_services = int(total_response.results[0][0]) if total_response.results else 0
+ # uses it to disclose truncation. The window function counts the groups
+ # before LIMIT applies, so every row carries the same total.
+ total_services = int(aggregates_response.results[0][6]) if aggregates_response.results else 0
enabled_rules = list(
LogsExclusionRule.objects.filter(team_id=self.team.pk, enabled=True).order_by("priority", "created_at")
@@ -335,14 +320,6 @@ class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunn
)
return ast.And(exprs=[base, search])
- def _total_services_query(self) -> ast.SelectQuery:
- query = parse_select(
- "SELECT uniq(service_name) FROM logs WHERE {where}",
- placeholders={"where": self._where_with_search},
- )
- assert isinstance(query, ast.SelectQuery)
- return query
-
def _aggregates_query(self) -> ast.SelectQuery:
query = parse_select(
"""
@@ -352,7 +329,10 @@ class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunn
sumIf(cnt, in(severity_text, tuple('error', 'fatal'))) AS error_count,
sumIf(cnt, in(severity_text, tuple('trace', 'debug'))) AS severity_debug,
sumIf(cnt, severity_text = 'info') AS severity_info,
- sumIf(cnt, in(severity_text, tuple('warn', 'warning'))) AS severity_warn
+ sumIf(cnt, in(severity_text, tuple('warn', 'warning'))) AS severity_warn,
+ -- Counts the grouped rows before LIMIT, so the caller learns the
+ -- true service count without a second scan of the window.
+ count() OVER () AS total_services
FROM (
SELECT
service_name,
diff --git a/products/logs/backend/test/test_services_query_runner.py b/products/logs/backend/test/test_services_query_runner.py
index 0d2ae850b726..dc9520745595 100644
--- a/products/logs/backend/test/test_services_query_runner.py
+++ b/products/logs/backend/test/test_services_query_runner.py
@@ -252,10 +252,18 @@ class TestServicesQueryDateRange(ClickhouseTestMixin, APIBaseTest):
# whose date subclasses cannot be built while freezegun has datetime.date patched.
def test_cap_limits_services_but_not_total_count(self):
with patch.object(services_query_runner, "SERVICES_LIMIT", 5):
- result = self._services("2025-12-16T00:00:00Z", "2025-12-16T23:59:59Z")
+ with patch.object(
+ services_query_runner,
+ "execute_hogql_query",
+ wraps=services_query_runner.execute_hogql_query,
+ ) as execute_spy:
+ result = self._services("2025-12-16T00:00:00Z", "2025-12-16T23:59:59Z")
self.assertEqual(len(result["services"]), 5)
self.assertEqual(result["total_services"], 12)
+ # Counting the services with a second, uncapped scan of the window instead
+ # of the aggregates query's window function added 20s on a large project.
+ self.assertEqual(execute_spy.call_count, 2, "expected only the aggregates and sparkline queries")
# The cap keeps the highest-volume services: every kept fixture service has
# 97+ rows, so the smallest two (11 and 3 rows) are the ones cut.
self.assertTrue(all(s["log_count"] >= 97 for s in result["services"]))
diff --git a/products/logs/frontend/components/LogsServices/LogsServices.tsx b/products/logs/frontend/components/LogsServices/LogsServices.tsx
index dc8b7c5ed811..3359f590635d 100644
--- a/products/logs/frontend/components/LogsServices/LogsServices.tsx
+++ b/products/logs/frontend/components/LogsServices/LogsServices.tsx
@@ -345,29 +345,33 @@ export function LogsServices(): JSX.Element {
/>
</div>
</div>
- {/* Pagination and sorting are controlled by the logic (which passes in the
- pre-sorted page slice) so it knows which rows are visible and can lazy-load
- their sparklines; the backend only sparklines the top rows per request. */}
- <LemonTable
- columns={columns}
- dataSource={pageRows}
- loading={servicesDataLoading}
- sorting={sorting}
- onSort={(newSorting) => setSorting(newSorting)}
- useURLForSorting={false}
- pagination={{
- controlled: true,
- pageSize: SERVICES_PAGE_SIZE,
- currentPage: page,
- entryCount: services.length,
- onForward: () => setPage(page + 1),
- onBackward: () => setPage(page - 1),
- useUrl: false,
- }}
- emptyState={searchTerm ? 'No services match your search' : 'No services found in this time range'}
- rowKey="service_name"
- size="small"
- />
+ {/* The scene container is a fixed height, so this region scrolls. Without it the
+ table is squeezed and clips its own last rows and the pagination control. */}
+ <div className="flex-1 min-h-0 overflow-y-auto">
+ {/* Pagination and sorting are controlled by the logic (which passes in the
+ pre-sorted page slice) so it knows which rows are visible and can lazy-load
+ their sparklines; the backend only sparklines the top rows per request. */}
+ <LemonTable
+ columns={columns}
+ dataSource={pageRows}
+ loading={servicesDataLoading}
+ sorting={sorting}
+ onSort={(newSorting) => setSorting(newSorting)}
+ useURLForSorting={false}
+ pagination={{
+ controlled: true,
+ pageSize: SERVICES_PAGE_SIZE,
+ currentPage: page,
+ entryCount: services.length,
+ onForward: () => setPage(page + 1),
+ onBackward: () => setPage(page - 1),
+ useUrl: false,
+ }}
+ emptyState={searchTerm ? 'No services match your search' : 'No services found in this time range'}
+ rowKey="service_name"
+ size="small"
+ />
+ </div>
</div>
)
}