add Langfuse tracing to agent runs and tool calls

This commit is contained in:
2026-04-15 23:19:48 -03:00
parent 2793a9d309
commit f03e47d9ba
5 changed files with 66 additions and 22 deletions

View File

@@ -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