监控与链路追踪
网关的健康检查、Prometheus 指标与抓取令牌、指标列表与常用查询、Grafana 仪表盘,以及 OpenTelemetry 链路追踪的配置项。
网关自带 Prometheus 指标和 OpenTelemetry 链路追踪,不依赖额外组件。示例配置在安装包的 monitoring/ 目录。
健康检查
GET /healthz 在网关正常运行时返回 ok,不需要鉴权,可以用于负载均衡或进程监控。
Prometheus 指标
抓取地址为 http://<网关>:8080/metrics,请求头带 Authorization: Bearer <RST_FABRIC_METRICS_TOKEN>。install.sh 安装时已随机生成令牌,写在 .env 中。
未设置 RST_FABRIC_METRICS_TOKEN 时,/metrics 只接受网关本机的直接访问,经反向代理或从其他机器访问一律拒绝。指标包含模型名、项目 ID、上游地址和调用量,远程抓取时务必设置令牌,并把令牌放在 Prometheus 可读的文件中。令牌泄露后修改 .env 并执行 docker compose up -d 即可作废。
scrape_configs:
- job_name: rst-ai-fabric
static_configs: [{ targets: ["10.0.0.10:8080"] }]
authorization:
type: Bearer
credentials_file: /etc/prometheus/secrets/rst-fabric-metrics-token完整示例在 monitoring/prometheus/prometheus.yml。告警规则示例在 monitoring/prometheus/alerts.yml,覆盖抓取失败、5xx 比例过高、模型满载拒绝、上游摘除或全部不可用、p95 延迟、p95 首 Token 时间、排队积压、KV Cache 饱和、License 失效和链路追踪丢失 span。阈值按实际模型调整,可以用 promtool check rules alerts.yml 校验。
指标
标签取值都有界:model 只取控制台中配置的模型名,请求不存在的模型记为 unknown;endpoint 为 chat、completions、embeddings、rerank、score、messages;status 为 2xx、4xx、429、5xx;upstream 为上游的 主机:端口。指标不含 API Key 和客户端 IP。
| 指标 | 类型 | 标签 | 说明 |
|---|---|---|---|
fabric_requests_total | counter | model, endpoint, project, status | 请求数 |
fabric_errors_total | counter | model, endpoint, code | 失败请求数,按错误码分 |
fabric_tokens_total | counter | model, type | Token 数,type 为 prompt 或 completion |
fabric_cost_total | counter | model | 计费金额(元) |
fabric_pii_masked_total | counter | direction, kind | 已脱敏的个人信息条数,direction 为 input 或 output,kind 为 phone、id_card、bank_card、email |
fabric_pii_blocked_total | counter | 因含个人信息被拦截的请求数 | |
fabric_request_duration_seconds | histogram | model, endpoint, status | 端到端耗时,流式请求计到最后一个分片 |
fabric_ttft_seconds | histogram | model, endpoint | 流式首 Token 时间 |
fabric_queue_seconds | histogram | model, endpoint | 等待模型并发槽位的时间 |
fabric_upstream_request_duration_seconds | histogram | upstream, status | 每次上游调用的耗时 |
fabric_upstream_attempts_total | counter | upstream | 上游调用次数,含重试 |
fabric_upstream_failures_total | counter | upstream, reason | 上游调用失败次数,reason 为 transport、http_429、http_5xx |
fabric_inflight_requests | gauge | model | 每个模型正在处理的请求 |
fabric_model_queue_depth | gauge | model | 排队等待槽位的请求 |
fabric_upstream_up | gauge | model, upstream | 上游健康为 1,被摘除为 0 |
fabric_upstream_inflight | gauge | model, upstream | 每个上游正在处理的请求 |
fabric_engine_running、fabric_engine_waiting | gauge | model, upstream | vLLM 运行中和排队的请求数 |
fabric_engine_kv_cache_usage_ratio | gauge | model, upstream | vLLM KV Cache 使用率,0 至 1 |
fabric_license_valid | gauge | License 可用为 1 | |
fabric_build_info | gauge | version, goversion | 版本信息,值恒为 1 |
另外输出 Go 运行时指标(go_*)、进程指标(process_*),以及启用链路追踪时的导出统计(fabric_trace_*)。
常用查询:
# 每个模型的 p95 延迟(成功请求)
histogram_quantile(0.95, sum by (le, model) (rate(fabric_request_duration_seconds_bucket{status="2xx"}[5m])))
# 5xx 比例
sum(rate(fabric_requests_total{status="5xx"}[5m])) / sum(rate(fabric_requests_total[5m]))
# 按错误码的失败速率
sum by (code) (rate(fabric_errors_total[5m]))Grafana
在 Grafana 中选择 Dashboards > New > Import,上传 monitoring/grafana/rst-ai-fabric.json,选择抓取网关的 Prometheus 数据源。面板包括请求速率、5xx 比例、p50 / p95 / p99 延迟、首 Token 时间、排队时间与深度、按错误码的失败、上游健康与耗时、vLLM 运行与 KV Cache、Token 与每小时成本、运行时与进程,顶部可以按 job 和 model 过滤。
OpenTelemetry 链路追踪
设置 OTLP 端点即开启,未设置时完全关闭。网关只支持 OTLP/HTTP JSON,端口通常为 4318。
每个请求生成一个 SERVER span(名称如 POST /v1/chat/completions),带 HTTP 属性和 GenAI 语义约定属性(请求的模型、实际服务的模型、输入与输出 Token 等),以及与响应头 X-Request-Id 一致的 fabric.request_id。排队等待和每次上游调用各有子 span。span 中不写入 API Key、提示词、模型输出或上游错误内容。
网关接受客户端的 traceparent、tracestate 作为父 span,并向上游注入 traceparent,支持 OpenTelemetry 的推理服务可以接上同一条链路。启用追踪后,网关日志中与请求相关的行带 trace_id 和 span_id。
| 配置项 | 默认 | 说明 |
|---|---|---|
OTEL_EXPORTER_OTLP_ENDPOINT | 空(关闭) | Collector 的 OTLP/HTTP 基础地址,网关自动追加 /v1/traces,例如 http://otel-collector:4318 |
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT | 空 | 完整的 traces 地址,原样使用,优先于上一项 |
OTEL_EXPORTER_OTLP_HEADERS | 空 | 认证等请求头,k1=v1,k2=v2,值需 URL 编码,例如 Authorization=Bearer%20xxx |
OTEL_SERVICE_NAME | rst-ai-fabric | service.name |
OTEL_RESOURCE_ATTRIBUTES | 空 | 附加资源属性,例如 deployment.environment=prod |
OTEL_TRACES_SAMPLER | parentbased_always_on | always_on、always_off、traceidratio、parentbased_always_on、parentbased_always_off、parentbased_traceidratio |
OTEL_TRACES_SAMPLER_ARG | 1.0 | *traceidratio 的采样比例,0 至 1 |
在 .env 中设置后执行 docker compose up -d 生效。示例,生产环境抽样 10%:
OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
OTEL_TRACES_SAMPLER=parentbased_traceidratio
OTEL_TRACES_SAMPLER_ARG=0.1
OTEL_RESOURCE_ATTRIBUTES=deployment.environment=prod导出在后台批量进行,队列满时丢弃新 span 并计入 fabric_trace_spans_dropped_total,不会阻塞请求。