监控与可观测性
RelayCore 提供 Prometheus 原生指标、结构化审计日志、SSE 实时事件流和状态快照,开箱即接入现有监控体系。
指标端点
| 端点 | 格式 | 说明 |
GET /api/v1/metrics | JSON | 结构化指标快照 |
GET /api/v1/metrics/prometheus | Prometheus text | 标准 Prometheus 采集格式 |
/_relay/metrics | JSON | 代理路径内嵌指标(无需 API 端口) |
/_relay/metrics/prometheus | Prometheus text | 内嵌 Prometheus 格式 |
核心指标
流量指标
| 指标 | 类型 | 说明 |
relaycore_flows_total | Counter | 累计处理的流量数 |
relaycore_flows_in_memory | Gauge | 当前内存中的流量数 |
relaycore_flows_dropped | Counter | 因背压丢弃的流量数 |
relaycore_flow_events_lagged_total | Counter | 事件广播通道滞后次数 |
拦截指标
relaycore_intercepts_pending | Gauge | 等待前端响应的拦截数 |
relaycore_oldest_intercept_age_ms | Gauge | 最旧未处理拦截的等待时间 |
relaycore_ws_pending_messages | Gauge | WebSocket 待处理消息数 |
规则指标
relaycore_rule_exec_errors | Counter | 规则执行错误次数 |
脚本指标
script_hook_duration_us{hook} | Histogram | 单次钩子执行耗时(微秒) |
script_hook_invocations_total{hook} | Counter | 钩子累计调用次数 |
script_hook_errors_total{hook} | Counter | 钩子累计错误次数 |
script_fetch_total{target,status} | Counter | relay.fetch 调用计数 |
script_env_access_total{key} | Counter | relay.env 访问计数 |
审计指标
relaycore_audit_events_total | Counter | 审计事件总数 |
relaycore_audit_events_failed | Counter | 审计事件记录失败数 |
relaycore_audit_events_lagged_total | Counter | 审计广播通道滞后次数 |
Prometheus 配置示例
# prometheus.yml
scrape_configs:
- job_name: "relaycore"
static_configs:
- targets: ["127.0.0.1:8082"]
metrics_path: "/api/v1/metrics/prometheus"
scrape_interval: 15s
健康检查
# 状态快照
curl http://127.0.0.1:8082/api/v1/status
# 响应示例
{
"phase": "Running",
"is_running": true,
"port": 8080,
"uptime_seconds": 3600,
"last_error": null
}
生命周期状态:Created → Starting → Running → Stopping → Stopped(Failed 表示异常退出)
SSE 实时事件流
GET /api/v1/events 通过 Server-Sent Events 推送实时更新:
| 事件类型 | 说明 |
flow | 新流量产生或更新 |
ws-message | WebSocket 消息更新 |
http-body | HTTP 请求/响应体更新 |
audit | 审计事件(规则变更、拦截处理等) |
lifecycle | 代理生命周期变化 |
lagged | 事件通道滞后警告 |
# 命令行订阅事件流
curl -N http://127.0.0.1:8082/api/v1/events
# JavaScript EventSource
const es = new EventSource("http://127.0.0.1:8082/api/v1/events");
es.addEventListener("flow", (e) => console.log(JSON.parse(e.data)));
审计系统
审计日志记录所有控制面操作,不可禁用:
| 事件类型 | 触发操作 |
rule_changed | 规则增/删/改 |
intercept_resolved | 拦截被处理(继续/丢弃/修改) |
script_reloaded | 脚本加载/重载 |
policy_updated | 策略变更(脱敏配置等) |
审计参与者标签:runtime / http / tauri / probe / cli
# 查询最近 50 条审计事件
curl "http://127.0.0.1:8082/api/v1/audit?limit=50"
# 按类型过滤
curl "http://127.0.0.1:8082/api/v1/audit?kind=rule_changed&limit=20"
Grafana 集成
仓库 benchmarks/grafana/ 提供了开箱即用的 Grafana Dashboard JSON。配合 Prometheus 即可搭建监控面板。