: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
fix(trino): explain a host that does not answer as Trino (#130727)
Tom Owersa92c3b000444
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/trino/tests/test_trino.py b/products/warehouse_sources/backend/temporal/data_imports/sources/trino/tests/test_trino.pyindex b2e810af3e43..28b8e9cb8942 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/trino/tests/test_trino.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/trino/tests/test_trino.py@@ -13,7 +13,14 @@ from unittest.mock import MagicMock, patchimport pyarrow as pafrom requests.exceptions import ConnectTimeout, ReadTimeout-from trino.exceptions import HttpError, TrinoConnectionError, TrinoExternalError, TrinoQueryError, TrinoUserError+from trino.exceptions import (+ Http503Error,+ HttpError,+ TrinoConnectionError,+ TrinoExternalError,+ TrinoQueryError,+ TrinoUserError,+)from trino.types import NamedRowTuplefrom products.warehouse_sources.backend.presentation.views.external_data_source.helpers import (@@ -38,6 +45,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.trino.trinTRINO_CONNECT_TIMEOUT_ERROR,TRINO_CREDENTIALS_REQUIRE_TLS_VERIFICATION_ERROR,TRINO_DISCOVERY_TIMEOUT_ERROR,+ TRINO_NOT_A_TRINO_ENDPOINT_ERROR,TRINO_READ_TIMEOUT_ERROR,DiscoveredTrinoTable,TrinoColumn,@@ -312,6 +320,41 @@ def test_trino_error_to_message_preserves_tls_verification_action() -> None:assert trino_error_to_message(error) == TRINO_CREDENTIALS_REQUIRE_TLS_VERIFICATION_ERROR+ ("error", "expected"),+ [+ (HttpError("error 405"), TRINO_NOT_A_TRINO_ENDPOINT_ERROR),+ (HttpError("error 405: b'<html>Method Not Allowed</html>'"), TRINO_NOT_A_TRINO_ENDPOINT_ERROR),+ (HttpError("error 404: b'<html>Not Found</html>'"), TRINO_NOT_A_TRINO_ENDPOINT_ERROR),+ # The page body must not pull the answer into the authentication branch.+ (HttpError("error 405: b'Authentication is not supported here'"), TRINO_NOT_A_TRINO_ENDPOINT_ERROR),+ (HttpError("error 401: b'Unauthorized'"), TRINO_AUTHENTICATION_ERROR),+ # A 404 for a purged query is a transient Trino answer, not a wrong endpoint.+ (+ HttpError("error 404: b'Error 404 Not Found: Query not found'"),+ "error 404: b'Error 404 Not Found: Query not found'",+ ),+ (HttpError("error 500: b'Internal Server Error'"), "error 500: b'Internal Server Error'"),+ (Http503Error("error 503: service unavailable"), "error 503: service unavailable"),+ # A 403 can be a real Trino permission answer, so it is left alone.+ (HttpError("error 403: b'Forbidden'"), "error 403: b'Forbidden'"),+ ],+ ids=[+ "405_bare",+ "405_with_body",+ "404_with_body",+ "405_body_mentions_authentication",+ "401_still_authentication",+ "404_query_not_found_unchanged",+ "500_unchanged",+ "503_unchanged",+ "403_unchanged",+ ],+)+def test_trino_error_to_message_explains_a_host_that_is_not_trino(error: Exception, expected: str) -> None:+ assert trino_error_to_message(error) == expected++@pytest.mark.parametrize(("raw_error", "expected"),[@@ -1011,6 +1054,9 @@ def _trino_user_error(name: str, message: str) -> TrinoUserError:# Deliberately retryable, not a regression: this depends on a worker's free disk at that# moment, shared by every query Trino schedules there, so it can clear on its own.(_trino_query_error("No free or healthy space available for spill"), False),+ # A wrong endpoint is worded for the person who sets the source up, but a sync keeps retrying.+ (HttpError("error 405: b'Method Not Allowed'"), False),+ (HttpError("error 500: b'Internal Server Error'"), False),],ids=["access_denied",@@ -1021,6 +1067,8 @@ def _trino_user_error(name: str, message: str) -> TrinoUserError:"connection_reset","query_memory_limit","spill_space_exhausted",+ "not_a_trino_endpoint",+ "server_error",],)def test_sync_failures_are_classified_for_retry(error: Exception, is_permanent: bool) -> None:diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/trino/trino.py b/products/warehouse_sources/backend/temporal/data_imports/sources/trino/trino.pyindex b297164a7f63..36cfe577d113 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/trino/trino.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/trino/trino.py@@ -85,6 +85,10 @@ TRINO_TLS_CERTIFICATE_ERROR = ("Check the certificate or turn off certificate verification.")TRINO_CONNECTION_ERROR = "PostHog could not connect to Trino."+TRINO_NOT_A_TRINO_ENDPOINT_ERROR = (+ "The host and port you entered didn't answer as a Trino server. "+ "Check that they point to your Trino coordinator and that the HTTPS setting is right."+)TRINO_CONNECTION_DESTINATION_ERROR = "Trino returned an unsupported connection destination. Check the connection host."TRINO_CONNECT_TIMEOUT_ERROR = ("Connection to Trino timed out. Check the host and port, and that PostHog's egress IP addresses are "@@ -152,6 +156,7 @@ TRINO_KNOWN_ERROR_MESSAGES = (TRINO_ACCESS_CONTROL_UNAVAILABLE_ERROR,TRINO_TLS_CERTIFICATE_ERROR,TRINO_CONNECTION_ERROR,+ TRINO_NOT_A_TRINO_ENDPOINT_ERROR,TRINO_CONNECT_TIMEOUT_ERROR,TRINO_READ_TIMEOUT_ERROR,TRINO_DISCOVERY_TIMEOUT_ERROR,@@ -286,6 +291,9 @@ def trino_failures_as_discovery_error() -> Iterator[None]:raise TrinoSchemaDiscoveryError(trino_error_to_message(exc)) from exc+_DRIVER_NOT_A_TRINO_ENDPOINT = re.compile(r"error (?:404|405)(?::|$)")++def trino_error_to_message(error: Exception) -> str:message = str(error).strip()lowered = message.lower()@@ -297,6 +305,11 @@ def trino_error_to_message(error: Exception) -> str:return TRINO_CONNECT_TIMEOUT_ERRORif timeout is not None:return TRINO_READ_TIMEOUT_ERROR+ # A web server, load balancer or UI port answers the driver's first request with a bare 404 or 405.+ # The body of that page is unrelated to Trino, so this runs before the keyword matches below. A 404+ # for a query the coordinator purged is a transient Trino answer, not a wrong endpoint.+ if _DRIVER_NOT_A_TRINO_ENDPOINT.match(lowered) and TRINO_QUERY_NOT_FOUND_ERROR.lower() not in lowered:+ return TRINO_NOT_A_TRINO_ENDPOINT_ERROR# Match the driver's HTTP 401 form ("error 401: ..."), not a bare "401": a Trino query error's# text carries a query ID whose time part can hold "401" and would derail this classification.if "authentication" in lowered or "unauthorized" in lowered or "error 401" in lowered: