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(clickhouse): route API-key async queries to the offline cluster (#76559)

Paweł Szczur0c1012a224fd
diff --git a/posthog/clickhouse/client/execute.py b/posthog/clickhouse/client/execute.py
index d27183198caa..a96f1d1f96dc 100644
--- a/posthog/clickhouse/client/execute.py
+++ b/posthog/clickhouse/client/execute.py
@@ -355,14 +355,16 @@ def sync_execute(
if workload == Workload.DEFAULT and (is_api_key_auth or tags.kind == "celery"):
workload = Workload.OFFLINE
- # Make sure we always have process_query_task on the online cluster.
+ # Make sure we always have app traffic through process_query_task on the online cluster.
+ # API-key traffic stays offline here too, so an async query lands on the same cluster its
+ # synchronous counterpart would.
# Workload.LOGS is exempt: it pins queries to the dedicated logs cluster, which is the
# only place the logs tables exist, so overriding it would send the query to a cluster
# that cannot answer it.
tags_id: str = tags.id or ""
if tags_id == "posthog.tasks.tasks.process_query_task":
if workload != Workload.LOGS:
- workload = Workload.ONLINE
+ workload = Workload.OFFLINE if is_api_key_auth else Workload.ONLINE
ch_user = ClickHouseUser.API if is_api_key_auth else ClickHouseUser.APP
if tags.workload == Workload.ENDPOINTS and workload != Workload.LOGS:
diff --git a/posthog/clickhouse/client/test/test_execute.py b/posthog/clickhouse/client/test/test_execute.py
index 0f91acae0f19..8a1935cccf95 100644
--- a/posthog/clickhouse/client/test/test_execute.py
+++ b/posthog/clickhouse/client/test/test_execute.py
@@ -4,7 +4,7 @@ from unittest.mock import MagicMock, patch
from posthog.clickhouse.client.connection import ClickHouseUser, Workload
from posthog.clickhouse.client.execute import sync_execute
from posthog.clickhouse.client.limit import RateLimit, get_llm_analytics_rate_limiter
-from posthog.clickhouse.query_tagging import Product, tags_context
+from posthog.clickhouse.query_tagging import AccessMethod, Product, tags_context
@pytest.fixture
@@ -26,22 +26,29 @@ def llm_analytics_slots():
@pytest.mark.parametrize(
- "workload,expected_workload",
+ "workload,access_method,expected_workload,expected_ch_user",
[
- (Workload.DEFAULT, Workload.ONLINE),
- (Workload.OFFLINE, Workload.ONLINE),
- (Workload.LOGS, Workload.LOGS),
+ (Workload.DEFAULT, None, Workload.ONLINE, ClickHouseUser.APP),
+ (Workload.OFFLINE, None, Workload.ONLINE, ClickHouseUser.APP),
+ (Workload.LOGS, None, Workload.LOGS, ClickHouseUser.APP),
+ (Workload.DEFAULT, AccessMethod.OAUTH, Workload.ONLINE, ClickHouseUser.APP),
+ (Workload.DEFAULT, AccessMethod.PERSONAL_API_KEY, Workload.OFFLINE, ClickHouseUser.API),
+ (Workload.ONLINE, AccessMethod.PROJECT_SECRET_API_KEY, Workload.OFFLINE, ClickHouseUser.API),
+ (Workload.LOGS, AccessMethod.PERSONAL_API_KEY, Workload.LOGS, ClickHouseUser.API),
],
)
-def test_process_query_task_workload_routing(client_from_pool, workload, expected_workload):
- # The async query worker forces queries onto the online cluster, but must not override
+def test_process_query_task_workload_routing(
+ client_from_pool, workload, access_method, expected_workload, expected_ch_user
+):
+ # The async query worker forces app traffic onto the online cluster while API-key traffic
+ # keeps the offline routing it gets when run synchronously. Neither may override
# cluster-pinned workloads: LOGS-workload tables only exist on the logs cluster.
- with tags_context(kind="celery", id="posthog.tasks.tasks.process_query_task"):
+ with tags_context(kind="celery", id="posthog.tasks.tasks.process_query_task", access_method=access_method):
sync_execute("SELECT 1", workload=workload, flush=False)
called_workload, _, _, called_ch_user = client_from_pool.call_args[0]
assert called_workload == expected_workload
- assert called_ch_user == ClickHouseUser.APP
+ assert called_ch_user == expected_ch_user
@pytest.mark.parametrize(