Files
wehub-resource-sync 91e75e620b
CI: cua-driver distro-compat matrix / Resolve release version (push) Waiting to run
CI: cua-driver distro-compat matrix / debian:12 (glibc 2.36) (push) Blocked by required conditions
CI: cua-driver distro-compat matrix / fedora:41 (glibc 2.40) (push) Blocked by required conditions
CI: cua-driver distro-compat matrix / rockylinux:9 (glibc 2.34) (push) Blocked by required conditions
CI: cua-driver distro-compat matrix / ubuntu:22.04 (glibc 2.35) (push) Blocked by required conditions
CI: cua-driver distro-compat matrix / ubuntu:24.04 (glibc 2.39) (push) Blocked by required conditions
CI: cua-driver distro-compat matrix / Distro compat summary (push) Blocked by required conditions
CI: Nix Linux Rust source / Nix / compositor build (push) Waiting to run
CI: Nix Linux Rust source / Nix / driver package (push) Waiting to run
CI: Nix Linux Rust source / Nix / Rust unit tests (push) Waiting to run
CI: Rust Linux unit / Rust Linux unit and compile (push) Waiting to run
CI: Rust Windows unit / Rust Windows unit and compile (push) Waiting to run
CI: SPDX Headers / Check SPDX headers (warn-only) (push) Waiting to run
CD: Docs MCP Server / build (linux/amd64) (push) Waiting to run
CD: Docs MCP Server / build (linux/arm64) (push) Waiting to run
CD: Docs MCP Server / merge (push) Blocked by required conditions
chore: import upstream snapshot with attribution
2026-07-13 13:03:19 +08:00

213 lines
6.5 KiB
Python

"""
OpenTelemetry callback handler for Computer-Use Agent (cua-agent).
Instruments agent operations for the Four Golden Signals:
- Latency: Operation duration
- Traffic: Operation counts
- Errors: Error counts
- Saturation: Concurrent operations
"""
import time
from typing import Any, Dict, List, Optional
from .base import AsyncCallbackHandler
# Import OTEL functions - these are available when cua-core[telemetry] is installed
try:
from cua_core.telemetry import (
create_span,
is_otel_enabled,
record_error,
record_operation,
record_tokens,
track_concurrent,
)
OTEL_AVAILABLE = True
except ImportError:
OTEL_AVAILABLE = False
def is_otel_enabled() -> bool:
return False
class OtelCallback(AsyncCallbackHandler):
"""
OpenTelemetry callback handler for instrumentation.
Tracks:
- Agent session lifecycle (start/end)
- Agent run lifecycle (start/end with duration)
- Individual steps (with duration)
- Computer actions (with duration)
- Token usage
- Errors
"""
def __init__(self, agent: Any):
"""
Initialize OTEL callback.
Args:
agent: The ComputerAgent instance
"""
self.agent = agent
self.model = getattr(agent, "model", "unknown")
# Timing state
self.run_start_time: Optional[float] = None
self.step_start_time: Optional[float] = None
self.step_count = 0
# Span management
self._session_span: Optional[Any] = None
self._run_span: Optional[Any] = None
# Track concurrent sessions
self._concurrent_tracker: Optional[Any] = None
def _get_agent_type(self) -> str:
"""Get the agent loop type name."""
if hasattr(self.agent, "agent_loop") and self.agent.agent_loop is not None:
return type(self.agent.agent_loop).__name__
return "unknown"
async def on_run_start(self, kwargs: Dict[str, Any], old_items: List[Dict[str, Any]]) -> None:
"""Called at the start of an agent run loop."""
if not OTEL_AVAILABLE or not is_otel_enabled():
return
self.run_start_time = time.perf_counter()
self.step_start_time = self.run_start_time
self.step_count = 0
async def on_run_end(
self,
kwargs: Dict[str, Any],
old_items: List[Dict[str, Any]],
new_items: List[Dict[str, Any]],
) -> None:
"""Called at the end of an agent run loop."""
if not OTEL_AVAILABLE or not is_otel_enabled():
return
if self.run_start_time is not None:
duration = time.perf_counter() - self.run_start_time
# Record run metrics
record_operation(
operation="agent.run",
duration_seconds=duration,
status="success",
model=self.model,
steps=self.step_count,
)
self.run_start_time = None
async def on_responses(self, kwargs: Dict[str, Any], responses: Dict[str, Any]) -> None:
"""Called when responses are received (each step)."""
if not OTEL_AVAILABLE or not is_otel_enabled():
return
self.step_count += 1
current_time = time.perf_counter()
# Calculate step duration if we have a start time
if self.step_start_time is not None:
step_duration = current_time - self.step_start_time
record_operation(
operation="agent.step",
duration_seconds=step_duration,
status="success",
model=self.model,
step_number=self.step_count,
)
# Start timing next step
self.step_start_time = current_time
async def on_usage(self, usage: Dict[str, Any]) -> None:
"""Called when usage information is received."""
if not OTEL_AVAILABLE or not is_otel_enabled():
return
prompt_tokens = usage.get("prompt_tokens", 0)
completion_tokens = usage.get("completion_tokens", 0)
if prompt_tokens > 0 or completion_tokens > 0:
record_tokens(
prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
model=self.model,
)
async def on_computer_call_start(self, item: Dict[str, Any]) -> None:
"""Called when a computer call is about to start."""
if not OTEL_AVAILABLE or not is_otel_enabled():
return
async def on_computer_call_end(
self, item: Dict[str, Any], result: List[Dict[str, Any]]
) -> None:
"""Called when a computer call has completed."""
if not OTEL_AVAILABLE or not is_otel_enabled():
return
action = item.get("action", {})
action_type = action.get("type", "unknown")
# Record computer action metric
# Note: We don't have precise timing here, so we record with 0 duration
# The actual timing should be done in the computer module
record_operation(
operation=f"computer.action.{action_type}",
duration_seconds=0, # Timing handled elsewhere
status="success",
model=self.model,
)
async def on_api_start(self, kwargs: Dict[str, Any]) -> None:
"""Called when an LLM API call is about to start."""
if not OTEL_AVAILABLE or not is_otel_enabled():
return
async def on_api_end(self, kwargs: Dict[str, Any], result: Any) -> None:
"""Called when an LLM API call has completed."""
if not OTEL_AVAILABLE or not is_otel_enabled():
return
class OtelErrorCallback(AsyncCallbackHandler):
"""
Callback that captures errors and sends them to OTEL.
Should be added early in the callback chain to catch all errors.
"""
def __init__(self, agent: Any):
"""
Initialize error callback.
Args:
agent: The ComputerAgent instance
"""
self.agent = agent
self.model = getattr(agent, "model", "unknown")
async def on_error(self, error: Exception, context: Dict[str, Any]) -> None:
"""Called when an error occurs during agent execution."""
if not OTEL_AVAILABLE or not is_otel_enabled():
return
error_type = type(error).__name__
operation = context.get("operation", "unknown")
# Record error metric
record_error(
error_type=error_type,
operation=operation,
model=self.model,
)