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): return a real error when the service filter query fails (#92109)

posthog[bot]0fd350b0372d
diff --git a/products/logs/backend/presentation/views/api.py b/products/logs/backend/presentation/views/api.py
index 42b3fbbc3f3d..a6bdf57fdfd2 100644
--- a/products/logs/backend/presentation/views/api.py
+++ b/products/logs/backend/presentation/views/api.py
@@ -25,6 +25,7 @@ from posthog.api.mixins import PydanticModelMixin
from posthog.api.property_value_metrics import PROPERTY_VALUES_DURATION
from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.clickhouse.query_tagging import Feature, Product, tag_queries
+from posthog.errors import ExposedCHQueryError
from posthog.event_usage import get_request_analytics_properties, report_user_action
from posthog.hogql_queries.query_runner import ExecutionMode
from posthog.hogql_queries.utils.time_sliced_query import time_sliced_results
@@ -1713,7 +1714,11 @@ class LogsViewSet(TeamAndOrgViewSetMixin, PydanticModelMixin, viewsets.ViewSet):
runner = LogAttributesQueryRunner(team=self.team, query=query)
- result = runner.calculate()
+ try:
+ result = runner.calculate()
+ except (QueryError, ExposedCHQueryError) as e:
+ # A user query error (HogQL or ClickHouse) becomes a clean 400 the filter can show.
+ return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)
return Response(
{
"results": [r.model_dump(exclude_none=True) for r in result.results],
@@ -1788,7 +1793,11 @@ class LogsViewSet(TeamAndOrgViewSetMixin, PydanticModelMixin, viewsets.ViewSet):
runner = LogValuesQueryRunner(team=self.team, query=query)
- result = runner.calculate()
+ try:
+ result = runner.calculate()
+ except (QueryError, ExposedCHQueryError) as e:
+ # A user query error (HogQL or ClickHouse) becomes a clean 400 the filter can show.
+ return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)
span.set_attribute("result_count", len(result.results))
return Response(
{"results": [r.model_dump() for r in result.results], "refreshing": False},
diff --git a/products/logs/backend/test/test_log_values_attributes.py b/products/logs/backend/test/test_log_values_attributes.py
index 35ead64ec90d..988fd262e3b1 100644
--- a/products/logs/backend/test/test_log_values_attributes.py
+++ b/products/logs/backend/test/test_log_values_attributes.py
@@ -2,11 +2,15 @@ import os
import json
from posthog.test.base import APIBaseTest, ClickhouseTestMixin
+from unittest.mock import patch
from parameterized import parameterized
from rest_framework import status
+from posthog.hogql.errors import QueryError
+
from posthog.clickhouse.client import sync_execute
+from posthog.errors import ExposedCHQueryError
class TestLogValuesAttributesTimezones(ClickhouseTestMixin, APIBaseTest):
@@ -339,6 +343,33 @@ class TestLogValuesAttributesTimezones(ClickhouseTestMixin, APIBaseTest):
self.assertLess(brokers_id_index, pid_index, "brokers.0.id should appear before pid when searching for 'id'")
+class TestLogValuesAttributesQueryErrors(APIBaseTest):
+ @parameterized.expand(
+ [
+ (
+ "values",
+ "products.logs.backend.presentation.views.api.LogValuesQueryRunner.calculate",
+ {"key": "service.name", "attribute_type": "resource"},
+ ),
+ (
+ "attributes",
+ "products.logs.backend.presentation.views.api.LogAttributesQueryRunner.calculate",
+ {"attribute_type": "resource"},
+ ),
+ ]
+ )
+ def test_query_error_returns_400_with_message(self, endpoint, runner_path, params):
+ # HogQL and ClickHouse failures are disjoint exception hierarchies; both must reach the
+ # service filter as a clean 400, not an opaque 500.
+ for error in (QueryError("bad log query"), ExposedCHQueryError("bad clickhouse query", code=43)):
+ with self.subTest(error=type(error).__name__):
+ with patch(runner_path, side_effect=error):
+ response = self.client.get(f"/api/projects/{self.team.pk}/logs/{endpoint}", params)
+
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertEqual(response.json()["error"], str(error))
+
+
class TestLogAttributesIlikeEscaping(ClickhouseTestMixin, APIBaseTest):
CLASS_DATA_LEVEL_SETUP = True
diff --git a/products/logs/frontend/components/LogsViewer/Filters/ServiceFilter.tsx b/products/logs/frontend/components/LogsViewer/Filters/ServiceFilter.tsx
index 05850e0f6db9..bfbaadf0eafa 100644
--- a/products/logs/frontend/components/LogsViewer/Filters/ServiceFilter.tsx
+++ b/products/logs/frontend/components/LogsViewer/Filters/ServiceFilter.tsx
@@ -91,7 +91,8 @@ function ServiceFilterInner({
selectionMode: 'multi' | 'single'
emptyButtonLabel: string
}): JSX.Element {
- const { serviceNames, allServiceNames, allServiceNamesLoading, search } = useValues(serviceFilterLogic)
+ const { serviceNames, allServiceNames, allServiceNamesLoading, search, serviceNamesError } =
+ useValues(serviceFilterLogic)
const { setSearch } = useActions(serviceFilterLogic)
const selected = value ?? []
@@ -139,6 +140,8 @@ function ServiceFilterInner({
<div>
{allServiceNamesLoading && allServiceNames.length === 0 ? (
<div className="p-2 text-muted text-center text-xs">Loading...</div>
+ ) : serviceNamesError && allServiceNames.length === 0 ? (
+ <div className="p-2 text-danger text-center text-xs">{serviceNamesError}</div>
) : serviceNames.length === 0 ? (
<div className="p-2 text-muted text-center text-xs">
{search ? 'No matching services' : 'No services found'}
diff --git a/products/logs/frontend/components/LogsViewer/Filters/serviceFilterLogic.ts b/products/logs/frontend/components/LogsViewer/Filters/serviceFilterLogic.ts
index ee7253950887..a910fa00d294 100644
--- a/products/logs/frontend/components/LogsViewer/Filters/serviceFilterLogic.ts
+++ b/products/logs/frontend/components/LogsViewer/Filters/serviceFilterLogic.ts
@@ -31,6 +31,7 @@ export interface serviceFilterLogicValues {
allServiceNamesLoading: boolean
search: string
serviceNames: string[]
+ serviceNamesError: string | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -85,6 +86,14 @@ export const serviceFilterLogic = kea<serviceFilterLogicType>([
reducers({
search: ['' as string, { setSearch: (_, { search }) => search }],
+ serviceNamesError: [
+ null as string | null,
+ {
+ loadServiceNames: () => null,
+ loadServiceNamesSuccess: () => null,
+ loadServiceNamesFailure: (_, { error }) => error || 'Could not load services',
+ },
+ ],
}),
loaders(({ values, props: logicProps }) => ({