: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(logs): scope services sparkline scan to displayed services (#65959)
Daniel Visca3126bcc8b1c1
diff --git a/products/logs/backend/services_query_runner.py b/products/logs/backend/services_query_runner.pyindex 42af3ef7986d..1dad490e2940 100644--- a/products/logs/backend/services_query_runner.py+++ b/products/logs/backend/services_query_runner.py@@ -174,17 +174,26 @@ class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunnsettings=self.settings,)- sparkline_response = execute_hogql_query(- query_type="LogsQuery",- query=self._sparkline_query(),- modifiers=self.modifiers,- team=self.team,- workload=Workload.LOGS,- timings=self.timings,- limit_context=self.limit_context,- filters=HogQLFilters(dateRange=self.query.dateRange),- settings=self.settings,- )+ # The table only renders sparklines for the services in the aggregates+ # result (top 25 by volume), so scope the sparkline scan to those names+ # rather than every service in the window: service_name is in the table's+ # sort key, so the IN filter prunes the scan, and it removes the chance of+ # the row LIMIT truncating a displayed service's trend.+ top_service_names = [row[0] for row in aggregates_response.results]+ sparkline_rows: list[Any] = []+ if top_service_names:+ sparkline_response = execute_hogql_query(+ query_type="LogsQuery",+ query=self._sparkline_query(top_service_names),+ modifiers=self.modifiers,+ team=self.team,+ workload=Workload.LOGS,+ timings=self.timings,+ limit_context=self.limit_context,+ filters=HogQLFilters(dateRange=self.query.dateRange),+ settings=self.settings,+ )+ sparkline_rows = sparkline_response.resultsenabled_rules = list(LogsExclusionRule.objects.filter(team_id=self.team.pk, enabled=True).order_by("priority", "created_at")@@ -243,7 +252,7 @@ class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunntop_share = round(sum(float(s["volume_share_pct"]) for s in services[:top_n]), 2) if services else 0.0sparkline = []- for row in sparkline_response.results:+ for row in sparkline_rows:sparkline.append({"time": row[0],@@ -296,7 +305,7 @@ class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunnassert isinstance(query, ast.SelectQuery)return query- def _sparkline_query(self) -> ast.SelectQuery:+ def _sparkline_query(self, service_names: list[str]) -> ast.SelectQuery:query = parse_select("""SELECT@@ -304,7 +313,7 @@ class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunnservice_name,count() AS event_countFROM logs- WHERE {where}+ WHERE {where} AND service_name IN {service_names}GROUP BY service_name, timeORDER BY time ASC, service_name ASCLIMIT 10000@@ -315,6 +324,7 @@ class ServicesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunnif self.query_date_range.interval_name != "second"else ast.Field(chain=["timestamp"]),"where": self.where(),+ "service_names": ast.Tuple(exprs=[ast.Constant(value=str(sn)) for sn in service_names]),},)assert isinstance(query, ast.SelectQuery)diff --git a/products/logs/backend/test/test_services_query_runner.py b/products/logs/backend/test/test_services_query_runner.pyindex 3c5dc387144e..0f811f9584eb 100644--- a/products/logs/backend/test/test_services_query_runner.py+++ b/products/logs/backend/test/test_services_query_runner.py@@ -178,7 +178,7 @@ class TestServicesQueryDateRange(ClickhouseTestMixin, APIBaseTest):{sql}""")- def _services(self, date_from: str, date_to: str) -> list[dict]:+ def _services(self, date_from: str, date_to: str) -> dict:response = self.client.post(f"/api/projects/{self.team.id}/logs/services",data={@@ -191,7 +191,7 @@ class TestServicesQueryDateRange(ClickhouseTestMixin, APIBaseTest):},)self.assertEqual(response.status_code, status.HTTP_200_OK)- return response.json()["services"]+ return response.json()@freeze_time("2025-12-16T10:33:00Z")def test_services_honors_sub_day_date_range(self):@@ -202,10 +202,22 @@ class TestServicesQueryDateRange(ClickhouseTestMixin, APIBaseTest):full_day = self._services("2025-12-16T00:00:00Z", "2025-12-16T23:59:59Z")windowed = self._services("2025-12-16T10:24:00Z", "2025-12-16T10:33:00Z")- self.assertEqual(sum(s["log_count"] for s in full_day), 1003)- self.assertEqual(len(windowed), 1)- self.assertEqual(windowed[0]["service_name"], "cdp-legacy-events-consumer")- self.assertEqual(windowed[0]["log_count"], 100)+ self.assertEqual(sum(s["log_count"] for s in full_day["services"]), 1003)+ self.assertEqual(len(windowed["services"]), 1)+ self.assertEqual(windowed["services"][0]["service_name"], "cdp-legacy-events-consumer")+ self.assertEqual(windowed["services"][0]["log_count"], 100)++ @freeze_time("2025-12-16T10:33:00Z")+ def test_sparkline_covers_exactly_the_returned_services(self):+ # The sparkline must line up exactly with the aggregates result: never a+ # service the table won't render (extra), and never a displayed service+ # missing its trend (the row-cap truncation the scoping fix removes).+ result = self._services("2025-12-16T00:00:00Z", "2025-12-16T23:59:59Z")+ service_names = {s["service_name"] for s in result["services"]}+ sparkline_services = {row["service_name"] for row in result["sparkline"]}++ self.assertTrue(service_names)+ self.assertEqual(sparkline_services, service_names)if __name__ == "__main__":