📡 OpenClaw Agent 监控与可观测性指南

构建智能运维体系 - 确保你的 Agent 7×24 稳定运行

🎯 为什么需要 Agent 监控?

随着 AI Agent 在企业中的广泛应用,监控和可观测性变得至关重要。一个没有监控的 Agent 就像一辆没有仪表盘的汽车——你不知道它是否正常运行,何时会出问题。

可观测性 (Observability) 的三大支柱:

📝 日志管理

完善的日志系统是监控的基础:

1. 日志配置

# openclaw.yaml 日志配置 logging: level: INFO # DEBUG, INFO, WARN, ERROR format: json # json, text outputs: - type: console colorize: true - type: file path: "/var/log/openclaw/agent.log" rotation: "daily" retention: "30 days" max_size: "100MB" - type: remote endpoint: "https://logs.your-server.com/api/logs" batch_size: 100 flush_interval: "10s" # 结构化日志字段 fields: agent_id: "{{ agent.id }}" agent_name: "{{ agent.name }}" session_id: "{{ session.id }}" user_id: "{{ user.id }}"

2. 日志查询与分析

# 查询最近的错误日志 openclaw logs query \ --level ERROR \ --since "1 hour ago" \ --limit 50 # 按关键词搜索日志 openclaw logs search \ --keyword "timeout" \ --since "24 hours ago" # 日志统计分析 openclaw logs analyze \ --metric "error_rate" \ --period "1d" \ --group-by "agent_name"

📊 性能指标监控

关键性能指标 (KPIs) 帮助你了解 Agent 的运行状态:

核心指标

指标 说明 健康范围
响应时间 (Response Time) Agent 处理请求的时间 < 2 秒
成功率 (Success Rate) 成功完成任务的比例 > 99%
Token 使用量 LLM Token 消耗量 在预算范围内
并发数 (Concurrency) 同时处理的请求数 在系统容量内
错误率 (Error Rate) 发生错误的比例 < 1%

指标收集配置

# 指标收集配置 metrics: enabled: true collection_interval: "15s" # 自定义指标 custom_metrics: - name: "task_completion_time" type: histogram description: "任务完成时间分布" labels: ["task_type", "agent_name"] - name: "token_usage" type: counter description: "Token 使用量" labels: ["model", "agent_name"] - name: "active_sessions" type: gauge description: "活跃会话数" # 指标导出 exporters: - type: prometheus endpoint: "/metrics" port: 9090 - type: datadog api_key: "{{ DATADOG_API_KEY }}" site: "datadoghq.com"

🔗 分布式追踪

追踪帮助你了解请求在复杂系统中的完整路径:

# 追踪配置 tracing: enabled: true sampler: type: probabilistic rate: 0.1 # 采样 10% 的请求 # 追踪传播 propagation: format: "w3c" # w3c, b3, jaeger # 追踪导出 exporters: - type: jaeger endpoint: "http://jaeger:14268/api/traces" - type: zipkin endpoint: "http://zipkin:9411/api/v2/spans" # 自动追踪 auto_instrumentation: - http_requests - database_queries - mcp_calls - llm_calls
💡 妙趣说: 追踪就像给每个请求装上 GPS,你可以看到它经过了哪些地方,花了多长时间,在哪里遇到了问题。调试复杂问题时,追踪是你的最佳帮手!

🚨 智能告警

及时发现问题并采取行动是监控的最终目标:

告警规则配置

# 告警规则配置 alerts: rules: - name: "高错误率告警" condition: "error_rate > 5%" duration: "5m" severity: critical channels: - type: feishu chat_id: "oc_xxx" message: "🚨 Agent 错误率超过 5%,请立即检查!" - type: email to: "oncall@company.com" subject: "[紧急] Agent 高错误率告警" - name: "响应时间过长" condition: "avg_response_time > 10s" duration: "10m" severity: warning channels: - type: feishu chat_id: "oc_xxx" - name: "Token 使用量超限" condition: "daily_token_usage > 1000000" severity: warning channels: - type: feishu chat_id: "oc_xxx" - name: "Agent 心跳丢失" condition: "heartbeat_missing > 5m" severity: critical channels: - type: feishu chat_id: "oc_xxx" - type: pagerduty service_id: "xxx"

告警管理

# 查看当前告警 openclaw alerts list --status active # 确认告警 openclaw alerts acknowledge --alert-id xxx # 静默告警(临时) openclaw alerts silence --alert-name "高错误率告警" --duration "1h" # 告警历史 openclaw alerts history --since "7d"

📊 监控仪表盘

一个直观的仪表盘能让你快速了解系统状态:

推荐仪表盘配置

# Grafana 仪表盘配置 dashboards: - name: "Agent 总览" panels: - title: "活跃 Agent 数" type: stat query: "count(active_agents)" - title: "请求速率" type: graph query: "rate(requests_total[5m])" - title: "错误率" type: gauge query: "rate(errors_total[5m]) / rate(requests_total[5m])" thresholds: - value: 0.01 color: green - value: 0.05 color: yellow - value: 0.1 color: red - title: "响应时间分布" type: heatmap query: "histogram_quantile(0.95, response_time_bucket)" - name: "Token 使用量" panels: - title: "每日 Token 消耗" type: stat query: "sum(daily_token_usage)" - title: "Token 消耗趋势" type: graph query: "sum(rate(token_usage[1h])) by (model)"

🔧 故障排查流程

当告警触发时,按照以下流程进行排查:

1. 快速诊断

# 检查 Agent 状态 openclaw agent status --all # 检查系统资源 openclaw system resources # 检查最近的错误 openclaw logs query --level ERROR --since "30m"

2. 深入分析

# 查看详细追踪 openclaw traces query --trace-id xxx # 性能瓶颈分析 openclaw performance analyze --since "1h" # 依赖服务检查 openclaw dependencies check

3. 恢复操作

# 重启问题 Agent openclaw agent restart --name "problem-agent" # 回滚到稳定版本 openclaw agent rollback --name "problem-agent" --version "v1.2.0" # 扩容处理 openclaw agent scale --name "problem-agent" --replicas 5

🚀 最佳实践