跳到主要内容
版本:3.10.x

API7 网关 AI Agent Skill:prometheus 插件

概览

prometheus 插件以 Prometheus 文本格式暴露 API7 企业版指标,包括 HTTP 状态码、请求延迟、带宽、上游健康状态和系统状态。API7 企业版提供内置指标端点,可由 Prometheus 抓取,并使用 Grafana 进行可视化。

适用场景

  • 监控每个路由、服务或消费者的请求速率、错误率和延迟。
  • 跟踪上游健康检查状态。
  • 观察系统整体性能和资源使用情况。
  • 使用 Prometheus + Grafana 构建仪表板和告警。

插件配置参考(路由/服务/全局规则)

字段类型是否必填默认值说明
prefer_namebooleanfalse在指标标签中使用路由/服务名称而不是 ID

该插件的每路由配置很少。大多数配置通过 API7 企业版网关组设置管理。

导出的指标

核心指标

指标类型说明
apisix_http_statuscounter每个路由/服务/消费者的 HTTP 状态码
apisix_http_latencyhistogram请求延迟,单位为毫秒(类型:请求、上游、apisix)
apisix_bandwidthcounter带宽,单位为字节(类型:入口、出口)
apisix_http_requests_totalgauge收到的 HTTP 请求总数
apisix_nginx_http_current_connectionsgauge按状态统计的当前连接数
apisix_upstream_statusgauge上游健康状态(1=健康,0=不健康)
apisix_node_infogaugeAPI7 企业版节点主机名和版本
apisix_shared_dict_capacity_bytesgauge共享内存容量
apisix_shared_dict_free_space_bytesgauge共享内存可用空间

API7 企业版内置指标端点

API7 企业版提供内置指标端点。默认情况下,该端点位于配置的 Prometheus 端口(通常为 9091,也可以通过数据面端口 9080 暴露)的 /apisix/prometheus/metrics 路径。

分步指南:启用 Prometheus 指标

1. 在路由上启用

default 网关组启用指标:

a7 route create --gateway-group default -f - <<'EOF'
{
"id": "my-api",
"uri": "/api/*",
"plugins": {
"prometheus": {
"prefer_name": true
}
},
"upstream": {
"type": "roundrobin",
"nodes": [{"host": "backend", "port": 8080, "weight": 1}]
}
}
EOF

2. 全局启用(网关组中的所有路由)

使用全局规则为 prod 网关组中的所有路由启用指标:

a7 global_rule create --gateway-group prod -f - <<'EOF'
{
"id": "prometheus-global",
"plugins": {
"prometheus": {}
}
}
EOF

3. 访问指标

默认端点:http://<gateway-host>:9091/apisix/prometheus/metrics

4. 配置 Prometheus 抓取

# prometheus.yml
scrape_configs:
- job_name: api7-ee
scrape_interval: 15s
static_configs:
- targets: ['gateway-host:9091']

常见模式

自定义指标前缀和导出端口

这些配置通常在 API7 企业版控制台或网关组配置中设置。

为指标添加额外标签

可以添加额外标签,以捕获上游地址或特定请求头值等上下文。

自定义直方图桶

延迟使用的默认桶为:1、2、5、7、10、15、20、25、30、40、50、60、 70、80、90、100、200、300、400、500、1000、2000、5000、10000、30000、60000 毫秒。

故障排查

现象原因解决方法
端点没有指标插件未启用prometheus: {} 添加到路由或 global_rules
指标端口无法访问未启用导出服务器检查 API7 企业版网关组设置
缺少路由标签prefer_name: false 且路由没有名称设置 prefer_name: true 并为路由命名
基数过高额外标签过多减少 extra_labels,避免指标数量爆炸
配置未生效指定了错误的网关组确保 --gateway-group 与目标集群一致

配置同步示例

version: "1"
gateway_group: default
global_rules:
- id: prometheus-global
plugins:
prometheus:
prefer_name: true
routes:
- id: my-api
uri: /api/*
plugins:
prometheus: {}
upstream:
type: roundrobin
nodes:
- host: backend
port: 8080
weight: 1

本页面由 api7/a7 仓库中的 a7-plugin-prometheus/SKILL.md 生成。你可以在 AI Agent Skills 页面查看所有技能。