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(warehouse): log trino readiness failure reasons (#101953)

Eric Duong10473314ef95
diff --git a/docs/internal/hogql-trino-compiler.md b/docs/internal/hogql-trino-compiler.md
index 0f39ec8cf065..612bcc8105dd 100644
--- a/docs/internal/hogql-trino-compiler.md
+++ b/docs/internal/hogql-trino-compiler.md
@@ -163,6 +163,13 @@ Source metadata describes what HogQL means. Target mappings describe where the c
Creating a job through Django admin starts the Temporal workflow after the database transaction commits. Provisioning does not create or start these jobs. A job can snapshot every eligible view in the organization or an explicit set of saved-query UUIDs. The workflow validates selected views against the organization and its control-plane-enabled teams, then compiles each represented team independently on the DuckLake task queue.
+If preparation fails because the Trino target is not ready, search worker logs by the job's `organization_id` for `trino_target_not_ready` or `refusing_trino_catalog_for_mismatched_organization`.
+The `reason` field distinguishes `http_error`, `invalid_response`, `invalid_enabled`, `not_enabled`, `invalid_status`, `state_not_ready`, `organization_mismatch`, and `invalid_catalog`.
+Each event includes `status_code`, with the readiness state or invalid field's type where relevant.
+Disabled and pending targets log at info level; request errors and invalid responses log at warning level.
+These readiness logs omit response bodies, upstream error text, and connection credentials.
+Response organization strings are limited to 128 characters; non-string values log only their type.
+
Compilation is best effort per view. Unsupported HogQL records a failed result and processing continues. A definition changed after the snapshot records a stale result. The workflow stores generated SQL and named values directly from activities so large SQL strings do not cross the Temporal workflow payload boundary. It never executes the SQL, creates Trino relations, or updates `DataWarehouseSavedQuery.query`.
The result admin can retry selected failed or stale rows. A retry creates a new selected-view job linked to the source job, preserving the original job and results as an immutable audit record.
diff --git a/products/managed_warehouse/backend/tests/test_trino_compiler.py b/products/managed_warehouse/backend/tests/test_trino_compiler.py
index e4f5f91bb635..69cb2f23e152 100644
--- a/products/managed_warehouse/backend/tests/test_trino_compiler.py
+++ b/products/managed_warehouse/backend/tests/test_trino_compiler.py
@@ -1,9 +1,12 @@
+import logging
from uuid import UUID
import pytest
from unittest import mock
+import structlog
from rest_framework.response import Response
+from structlog.testing import capture_logs
from posthog.schema import HogQLQuery
@@ -56,6 +59,13 @@ def _team() -> Team:
class TestReadyTrinoCatalogName:
+ @pytest.fixture(autouse=True)
+ def isolated_logger(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(
+ "products.managed_warehouse.backend.trino_compiler.logger",
+ structlog.wrap_logger(logging.getLogger(__name__), context_class=dict),
+ )
+
@pytest.mark.parametrize("catalog_key", ["trino_catalog_name", "catalog"])
def test_reads_ready_catalog_and_supports_rolling_deploys(self, catalog_key: str) -> None:
body = {
@@ -63,32 +73,130 @@ class TestReadyTrinoCatalogName:
"status": {
"org": "org-1",
"state": "ready",
- catalog_key: "org_catalog",
+ catalog_key: " org_catalog ",
},
}
- with mock.patch(
- "products.managed_warehouse.backend.presentation.views._request",
- return_value=Response(body, status=200),
- ) as request:
+ with (
+ mock.patch(
+ "products.managed_warehouse.backend.presentation.views._request",
+ return_value=Response(body, status=200),
+ ) as request,
+ capture_logs() as logs,
+ ):
assert get_ready_trino_catalog_name("org-1") == "org_catalog"
request.assert_called_once_with("GET", "org-1", "/trino", require_enabled=False)
+ assert logs == []
@pytest.mark.parametrize(
- "body",
+ "body, status_code, expected_log",
[
- {"enabled": False},
- {"enabled": True, "status": {"org": "org-1", "state": "pending", "trino_catalog_name": "cat"}},
- {"enabled": True, "status": {"org": "another-org", "state": "ready", "trino_catalog_name": "cat"}},
- {"enabled": True, "status": {"org": "org-1", "state": "ready", "trino_catalog_name": ""}},
+ ({"error": "example upstream failure"}, 503, {"reason": "http_error", "log_level": "warning"}),
+ ([], 200, {"reason": "invalid_response", "response_type": "list", "log_level": "warning"}),
+ (
+ {"enabled": False},
+ 200,
+ {"reason": "not_enabled", "enabled": False, "enabled_type": "bool", "log_level": "info"},
+ ),
+ (
+ {},
+ 200,
+ {"reason": "invalid_enabled", "enabled_type": "NoneType", "log_level": "warning"},
+ ),
+ (
+ {"enabled": "true"},
+ 200,
+ {"reason": "invalid_enabled", "enabled_type": "str", "log_level": "warning"},
+ ),
+ (
+ {"enabled": True},
+ 200,
+ {"reason": "invalid_status", "status_type": "NoneType", "log_level": "warning"},
+ ),
+ (
+ {"enabled": True, "status": []},
+ 200,
+ {"reason": "invalid_status", "status_type": "list", "log_level": "warning"},
+ ),
+ (
+ {"enabled": True, "status": {"org": "org-1", "state": "pending", "trino_catalog_name": "cat"}},
+ 200,
+ {"reason": "state_not_ready", "state": "pending", "state_type": "str", "log_level": "info"},
+ ),
+ (
+ {"enabled": True, "status": {}},
+ 200,
+ {"reason": "state_not_ready", "state": None, "state_type": "NoneType", "log_level": "info"},
+ ),
+ (
+ {"enabled": True, "status": {"org": "another-org", "state": "ready", "trino_catalog_name": "cat"}},
+ 200,
+ {
+ "event": "refusing_trino_catalog_for_mismatched_organization",
+ "reason": "organization_mismatch",
+ "requested_organization_id": "org-1",
+ "response_organization_id": "another-org",
+ "response_organization_id_type": "str",
+ "log_level": "warning",
+ },
+ ),
+ (
+ {"enabled": True, "status": {"org": {"token": "example-secret"}, "state": "ready"}},
+ 200,
+ {
+ "event": "refusing_trino_catalog_for_mismatched_organization",
+ "reason": "organization_mismatch",
+ "requested_organization_id": "org-1",
+ "response_organization_id": None,
+ "response_organization_id_type": "dict",
+ "log_level": "warning",
+ },
+ ),
+ (
+ {"enabled": True, "status": {"org": "x" * 256, "state": "ready"}},
+ 200,
+ {
+ "event": "refusing_trino_catalog_for_mismatched_organization",
+ "reason": "organization_mismatch",
+ "requested_organization_id": "org-1",
+ "response_organization_id": "x" * 128,
+ "response_organization_id_type": "str",
+ "log_level": "warning",
+ },
+ ),
+ (
+ {"enabled": True, "status": {"org": "org-1", "state": "ready", "trino_catalog_name": " "}},
+ 200,
+ {"reason": "invalid_catalog", "catalog_type": "str", "log_level": "warning"},
+ ),
+ (
+ {"enabled": True, "status": {"org": "org-1", "state": "ready"}},
+ 200,
+ {"reason": "invalid_catalog", "catalog_type": "NoneType", "log_level": "warning"},
+ ),
],
)
- def test_rejects_an_unusable_target(self, body: dict[str, object]) -> None:
- with mock.patch(
- "products.managed_warehouse.backend.presentation.views._request",
- return_value=Response(body, status=200),
+ def test_rejects_an_unusable_target(self, body: object, status_code: int, expected_log: dict[str, object]) -> None:
+ if isinstance(body, dict):
+ body = {**body, "error": "example upstream failure", "token": "example-secret"}
+ if isinstance(body.get("status"), dict):
+ body["status"] = {**body["status"], "connection": {"password": "example-secret"}}
+ with (
+ mock.patch(
+ "products.managed_warehouse.backend.presentation.views._request",
+ return_value=Response(body, status=status_code),
+ ),
+ capture_logs() as logs,
):
assert get_ready_trino_catalog_name("org-1") is None
+ assert logs == [
+ {
+ "event": "trino_target_not_ready",
+ "organization_id": "org-1",
+ "status_code": status_code,
+ **expected_log,
+ }
+ ]
class TestCompileHogQLToTrinoSQL:
diff --git a/products/managed_warehouse/backend/trino_compiler.py b/products/managed_warehouse/backend/trino_compiler.py
index d2499a05d77e..e5fac11dc372 100644
--- a/products/managed_warehouse/backend/trino_compiler.py
+++ b/products/managed_warehouse/backend/trino_compiler.py
@@ -76,25 +76,63 @@ def get_ready_trino_catalog_name(organization_id: str) -> str | None:
from products.managed_warehouse.backend.presentation.views import _request # noqa: PLC0415
response = _request("GET", organization_id, "/trino", require_enabled=False)
- if not status.is_success(response.status_code) or not isinstance(response.data, dict):
+ readiness_logger = logger.bind(organization_id=str(organization_id), status_code=response.status_code)
+ if not status.is_success(response.status_code):
+ readiness_logger.warning("trino_target_not_ready", reason="http_error")
return None
- if response.data.get("enabled") is not True:
+ if not isinstance(response.data, dict):
+ readiness_logger.warning(
+ "trino_target_not_ready", reason="invalid_response", response_type=type(response.data).__name__
+ )
+ return None
+ enabled = response.data.get("enabled")
+ if not isinstance(enabled, bool):
+ readiness_logger.warning(
+ "trino_target_not_ready", reason="invalid_enabled", enabled_type=type(enabled).__name__
+ )
+ return None
+ if not enabled:
+ readiness_logger.info(
+ "trino_target_not_ready",
+ reason="not_enabled",
+ enabled=enabled,
+ enabled_type=type(enabled).__name__,
+ )
return None
trino_status = response.data.get("status")
- if not isinstance(trino_status, dict) or trino_status.get("state") != "ready":
+ if not isinstance(trino_status, dict):
+ readiness_logger.warning(
+ "trino_target_not_ready", reason="invalid_status", status_type=type(trino_status).__name__
+ )
+ return None
+ state = trino_status.get("state")
+ if state != "ready":
+ readiness_logger.info(
+ "trino_target_not_ready",
+ reason="state_not_ready",
+ state=state[:128] if isinstance(state, str) else None,
+ state_type=type(state).__name__,
+ )
return None
response_org = trino_status.get("org")
if response_org is not None and str(response_org) != str(organization_id):
- logger.warning(
+ readiness_logger.warning(
"refusing_trino_catalog_for_mismatched_organization",
+ reason="organization_mismatch",
requested_organization_id=str(organization_id),
- response_organization_id=str(response_org),
+ response_organization_id=response_org[:128] if isinstance(response_org, str) else None,
+ response_organization_id_type=type(response_org).__name__,
)
return None
catalog_name = trino_status.get("trino_catalog_name") or trino_status.get("catalog")
- return catalog_name.strip() if isinstance(catalog_name, str) and catalog_name.strip() else None
+ if not isinstance(catalog_name, str) or not catalog_name.strip():
+ readiness_logger.warning(
+ "trino_target_not_ready", reason="invalid_catalog", catalog_type=type(catalog_name).__name__
+ )
+ return None
+ return catalog_name.strip()
def prepare_hogql_to_trino_compiler(