Skip to content

The agent that grows with you

refactor(connectors): execute logs through the shared connector outcome line

Siddharth Balyan5044ae8cb2ca
diff --git a/tests/tools/test_connectors_execute_status.py b/tests/tools/test_connectors_execute_status.py
index 0066bd782dc..5fa74e91e8d 100644
--- a/tests/tools/test_connectors_execute_status.py
+++ b/tests/tools/test_connectors_execute_status.py
@@ -3,6 +3,7 @@ shape stays what the array envelope produced, and an older gateway still works t
"""
import json
+import logging
import pytest
@@ -110,6 +111,14 @@ def test_an_unknown_code_with_a_family_still_renders():
assert entry["error"] == {"code": "SOMETHING_NEW", "message": "new failure", "family": "backend"}
+def test_a_failed_call_logs_its_code_and_the_gateways_request_id(caplog):
+ caplog.set_level(logging.DEBUG)
+ make_client(FakeTransport(failure("TOOL_NOT_ALLOWED", 403))).execute(planned())
+ lines = [r for r in caplog.records if r.getMessage().startswith("connector connectors.execute")]
+ assert [(r.levelno, r.getMessage()) for r in lines] == [
+ (logging.WARNING, "connector connectors.execute 403 TOOL_NOT_ALLOWED request_id=req_9 dispatch=-")]
+
+
def test_success_returns_the_result_data():
body = {"result": {"connector": "gmail", "tool": "GMAIL_SEND_EMAIL", "data": {"id": "m1"}, "account": "work"}}
entry = model_entry(FakeTransport(FakeResponse(200, body)))
diff --git a/tools/connectors/gateway/bridge.py b/tools/connectors/gateway/bridge.py
index 384dec8882c..23ca1f1d3a8 100644
--- a/tools/connectors/gateway/bridge.py
+++ b/tools/connectors/gateway/bridge.py
@@ -166,7 +166,6 @@ def run_remote(
# A CONNECTION_REQUIRED link minted by this call is the user's next click, so the call names
# the surface that browser should come back to.
remote_results = client.execute(wire_planned, **return_to_args())
- _log_call_failures(remote_results, dispatch_id)
entries = splice_remote_results(planned, remote_results)
except ToolGatewayError as exc:
logger.warning(
@@ -206,7 +205,6 @@ def run_remote(
# Retry confirmed misses once without disturbing successful sibling slots.
try:
fallback_results = client.execute(fallback_planned, **return_to_args())
- _log_call_failures(fallback_results, dispatch_id)
fallback_entries = splice_remote_results(fallback_planned, fallback_results)
except ToolGatewayError as exc:
logger.warning(
@@ -231,13 +229,3 @@ def run_remote(
entries[slot] = fallback_entry
return entries
-
-def _log_call_failures(results: Any, dispatch_id: Optional[str]) -> None:
- """One WARNING per failed call, carrying the gateway's request id so the line joins its trace."""
- for result in results if isinstance(results, list) else ():
- error = result.get("error") if isinstance(result, dict) else None
- if isinstance(error, dict):
- logger.warning(
- "connector execute %s %s request_id=%s dispatch=%s",
- error.get("status", 200), error.get("code"), error.get("request_id", "-"), dispatch_id,
- )
diff --git a/tools/connectors/gateway/client.py b/tools/connectors/gateway/client.py
index feef66f0f72..09ae551f894 100644
--- a/tools/connectors/gateway/client.py
+++ b/tools/connectors/gateway/client.py
@@ -44,6 +44,7 @@ _RETRY_ONCE_STATUSES = frozenset({502, 503, 504})
_MAX_RETRY_AFTER_SECONDS = 10.0
# Gateway origins that rejected the single-call execute form; they get the array form for the process.
_LEGACY_EXECUTE_ORIGINS: set[str] = set()
+_EXECUTE_ROUTE = "connectors.execute"
@dataclass(frozen=True)
@@ -243,16 +244,21 @@ class ConnectorClient:
continue
status = int(getattr(response, "status_code", 0))
payload = _safe_json(response)
+ request_id = response_request_id(response)
if 200 <= status < 300:
+ log_connector_outcome(_EXECUTE_ROUTE, status, request_id=request_id, call=self._call)
parsed = self._parse(wire.ConnectorExecuteSingleResponse, payload, "connector execute result")
entry = {"data": parsed.result.data, "error": None}
if parsed.result.account:
entry["account"] = parsed.result.account
return entry
headers = getattr(response, "headers", None)
+ failure = _call_failure(payload)
+ code = failure.code if failure else parse_gateway_error(status, payload, headers).code
+ log_connector_outcome(_EXECUTE_ROUTE, status, code=code,
+ request_id=(failure.request_id if failure else None) or request_id, call=self._call)
if status == 400 and _rejects_single_call_form(payload):
return None
- failure = _call_failure(payload)
wait = _retry_wait(status, headers) if attempt < _MAX_RETRIES else None
if wait is not None:
logger.debug("Connector execute attempt %d got %d; retrying with same key", attempt + 1, status)
@@ -261,7 +267,7 @@ class ConnectorClient:
continue
if failure is None:
raise parse_gateway_error(status, payload, headers)
- return {"data": None, "error": _error_dict(failure, status=status, headers=headers)}
+ return {"data": None, "error": _error_dict(failure, status=status, request_id=request_id)}
raise ToolGatewayError("connector execute retries exhausted", code="TRANSPORT_ERROR", retryable=True)
def _execute_legacy(
@@ -410,7 +416,7 @@ def _result_dict(result: wire.ConnectorExecuteResult) -> dict[str, Any]:
def _error_dict(
- failure: wire.ConnectorToolError, *, status: Optional[int] = None, headers: Optional[Mapping[str, Any]] = None,
+ failure: wire.ConnectorToolError, *, status: Optional[int] = None, request_id: Optional[str] = None,
) -> dict[str, Any]:
"""The plain error entry merge renders. ``status`` and ``request_id`` are for logs, never the model."""
error: dict[str, Any] = {"code": failure.code, "message": failure.message}
@@ -423,7 +429,7 @@ def _error_dict(
error[key] = value
if status is not None:
error["status"] = status
- request_id = getattr(failure, "request_id", None) or (headers or {}).get("x-request-id")
+ request_id = getattr(failure, "request_id", None) or request_id
if request_id:
error["request_id"] = str(request_id)
return error