监控与可观测性

RelayCore 提供 Prometheus 原生指标、结构化审计日志、SSE 实时事件流和状态快照,开箱即接入现有监控体系。

指标端点

端点格式说明
GET /api/v1/metricsJSON结构化指标快照
GET /api/v1/metrics/prometheusPrometheus text标准 Prometheus 采集格式
/_relay/metricsJSON代理路径内嵌指标(无需 API 端口)
/_relay/metrics/prometheusPrometheus text内嵌 Prometheus 格式

核心指标

流量指标

指标类型说明
relaycore_flows_totalCounter累计处理的流量数
relaycore_flows_in_memoryGauge当前内存中的流量数
relaycore_flows_droppedCounter因背压丢弃的流量数
relaycore_flow_events_lagged_totalCounter事件广播通道滞后次数

拦截指标

relaycore_intercepts_pendingGauge等待前端响应的拦截数
relaycore_oldest_intercept_age_msGauge最旧未处理拦截的等待时间
relaycore_ws_pending_messagesGaugeWebSocket 待处理消息数

规则指标

relaycore_rule_exec_errorsCounter规则执行错误次数

脚本指标

script_hook_duration_us{hook}Histogram单次钩子执行耗时(微秒)
script_hook_invocations_total{hook}Counter钩子累计调用次数
script_hook_errors_total{hook}Counter钩子累计错误次数
script_fetch_total{target,status}Counterrelay.fetch 调用计数
script_env_access_total{key}Counterrelay.env 访问计数

审计指标

relaycore_audit_events_totalCounter审计事件总数
relaycore_audit_events_failedCounter审计事件记录失败数
relaycore_audit_events_lagged_totalCounter审计广播通道滞后次数

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
}

生命周期状态:CreatedStartingRunningStoppingStoppedFailed 表示异常退出)

SSE 实时事件流

GET /api/v1/events 通过 Server-Sent Events 推送实时更新:

事件类型说明
flow新流量产生或更新
ws-messageWebSocket 消息更新
http-bodyHTTP 请求/响应体更新
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 即可搭建监控面板。