add Langfuse tracing to agent runs and tool calls
This commit is contained in:
@@ -18,17 +18,8 @@ async def run_fce(
|
||||
flight_id: str,
|
||||
mcp: MCPMultiClient,
|
||||
on_event: Any = None,
|
||||
trace: Any = None,
|
||||
) -> dict:
|
||||
"""Run the FCE agent for a single flight.
|
||||
|
||||
Args:
|
||||
flight_id: The flight to generate a notification for.
|
||||
mcp: Connected MCPMultiClient (shared + passenger).
|
||||
on_event: Optional async callback for real-time events.
|
||||
|
||||
Returns:
|
||||
Notification result dict.
|
||||
"""
|
||||
run_start = time.time()
|
||||
errors = []
|
||||
tool_calls = []
|
||||
@@ -76,20 +67,27 @@ async def run_fce(
|
||||
|
||||
async def _call(server, tool, args, is_live=False, timeout=15.0):
|
||||
t = time.time()
|
||||
span = trace.span(name=tool, input=args, metadata={"server": server, "is_live": is_live}) if trace else None
|
||||
try:
|
||||
result = await asyncio.wait_for(
|
||||
mcp.call_tool(server, tool, args), timeout=timeout,
|
||||
)
|
||||
lat = int((time.time() - t) * 1000)
|
||||
if span:
|
||||
span.end(output=result, metadata={"latency_ms": lat})
|
||||
await emit("tool_call_end", tool=tool, latency_ms=lat, is_live=is_live)
|
||||
return result
|
||||
except asyncio.TimeoutError:
|
||||
lat = int((time.time() - t) * 1000)
|
||||
if span:
|
||||
span.end(output={"error": "timeout"}, level="ERROR")
|
||||
await emit("tool_call_error", tool=tool, error="timeout", latency_ms=lat)
|
||||
errors.append(f"{tool}: timeout after {timeout}s")
|
||||
return None
|
||||
except Exception as e:
|
||||
lat = int((time.time() - t) * 1000)
|
||||
if span:
|
||||
span.end(output={"error": str(e)}, level="ERROR")
|
||||
await emit("tool_call_error", tool=tool, error=str(e), latency_ms=lat)
|
||||
errors.append(f"{tool}: {e}")
|
||||
return None
|
||||
|
||||
@@ -26,17 +26,8 @@ async def run_handover(
|
||||
hubs: list[str] | None = None,
|
||||
mcp: MCPMultiClient | None = None,
|
||||
on_event: Any = None,
|
||||
trace: Any = None,
|
||||
) -> dict:
|
||||
"""Run the Shift Handover agent.
|
||||
|
||||
Args:
|
||||
hubs: Hubs to cover (default: all 5).
|
||||
mcp: Connected MCPMultiClient (shared + ops).
|
||||
on_event: Optional async callback for real-time events.
|
||||
|
||||
Returns:
|
||||
Handover brief result dict.
|
||||
"""
|
||||
target_hubs = hubs or ALL_HUBS
|
||||
run_start = time.time()
|
||||
errors = []
|
||||
@@ -54,20 +45,27 @@ async def run_handover(
|
||||
|
||||
async def _call(server, tool, args, is_live=False, timeout=15.0):
|
||||
t = time.time()
|
||||
span = trace.span(name=tool, input=args, metadata={"server": server, "is_live": is_live}) if trace else None
|
||||
try:
|
||||
result = await asyncio.wait_for(
|
||||
mcp.call_tool(server, tool, args), timeout=timeout,
|
||||
)
|
||||
lat = int((time.time() - t) * 1000)
|
||||
if span:
|
||||
span.end(output=result, metadata={"latency_ms": lat})
|
||||
await emit("tool_call_end", tool=tool, latency_ms=lat, is_live=is_live)
|
||||
return result
|
||||
except asyncio.TimeoutError:
|
||||
lat = int((time.time() - t) * 1000)
|
||||
if span:
|
||||
span.end(output={"error": "timeout"}, level="ERROR")
|
||||
await emit("tool_call_error", tool=tool, error="timeout", latency_ms=lat)
|
||||
errors.append(f"{tool}: timeout after {timeout}s")
|
||||
return None
|
||||
except Exception as e:
|
||||
lat = int((time.time() - t) * 1000)
|
||||
if span:
|
||||
span.end(output={"error": str(e)}, level="ERROR")
|
||||
await emit("tool_call_error", tool=tool, error=str(e), latency_ms=lat)
|
||||
errors.append(f"{tool}: {e}")
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user