使用 Prometheus 监控 APISIX 指标
Prometheus 是一种常用的系统监控和告警工具包。它收集并存储多维时间序列数据,例如带有键值对标签的指标。
APISIX 能够以低延迟向 Prometheus 公开大量指标,从而支持持续监控和诊断。
本指南介绍如何启用 prometheus 插件,与 Prometheus 和 Grafana 服务集成,以收集并可视化 APISIX HTTP 指标。

前置条件
- 安装 Docker。
- 安装 cURL 以向服务发送验证请求。
- 按照快速入门教程在 Docker 中启动新的 APISIX 实例。
- 如果使用 Admin API 示例且启用了身份验证,请将管理员角色的密钥导出为
ADMIN_API_KEY。请在deployment.admin.admin_key下配置该密钥。Docker 快速入门为了便于本地评估而禁用了身份验证;生产环境中请保持启用。
启用 Prometheus 插件
全局启用 prometheus 插件。你也可以仅在某个路由上启用该插件。
- Admin API
- ADC
curl "http://127.0.0.1:9180/apisix/admin/global_rules/prometheus" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"plugins": {
"prometheus": {}
}
}'
APISIX 收集内部运行时指标,并默认通过端口 9091 和路径 /apisix/prometheus/metrics 公开这些指标。可以在配置文件中自定义端口和路径。
ADC 会将全局规则导出为一个以插件名为键的 map。导出内容不会保留原始规则 ID,也不会保留哪些插件曾归属于同一条规则。如果多条规则配置了同一个插件,导出结果只会保留一份配置。需要保留原始规则 ID 或分组时,请使用本节的 Admin API 标签页。
导出当前全局规则:
adc dump -o adc-global-rules.yaml \
--include-resource-type global_rule
ADC dump 可能包含 Admin API 返回的已解密敏感字段。请限制对导出文件的访问,并在完成变更后将其删除。
编辑导出的文件,在保留其他所有插件条目的同时添加 Prometheus 插件:
global_rules:
# Keep every other global plugin entry from the exported file.
prometheus: {}
预览全局规则,并确认差异中没有无关的插件变更或删除:
adc diff -f adc-global-rules.yaml \
--include-resource-type global_rule
同步已审查的全局规则:
adc sync -f adc-global-rules.yaml \
--include-resource-type global_rule
APISIX 收集内部运行时指标,并默认通过端口 9091 和路径 /apisix/prometheus/metrics 公开这些指标。可以在配置文件中自定义端口和路径。
向 /apisix/prometheus/metrics 路径发送请求,从 APISIX 获取指标:
curl "http://127.0.0.1:9091/apisix/prometheus/metrics"
你应该看到类似以下的指标列表:
# HELP apisix_etcd_modify_indexes Etcd modify index for APISIX keys
# TYPE apisix_etcd_modify_indexes gauge
apisix_etcd_modify_indexes{key="consumers"} 0
apisix_etcd_modify_indexes{key="global_rules"} 0
apisix_etcd_modify_indexes{key="max_modify_index"} 16
apisix_etcd_modify_indexes{key="prev_index"} 15
apisix_etcd_modify_indexes{key="protos"} 0
apisix_etcd_modify_indexes{key="routes"} 16
apisix_etcd_modify_indexes{key="services"} 0
apisix_etcd_modify_indexes{key="ssls"} 0
apisix_etcd_modify_indexes{key="stream_routes"} 0
apisix_etcd_modify_indexes{key="upstreams"} 0
apisix_etcd_modify_indexes{key="x_etcd_index"} 16
# HELP apisix_etcd_reachable Config server etcd reachable from APISIX, 0 is unreachable
# TYPE apisix_etcd_reachable gauge
apisix_etcd_reachable 1
...
# HELP apisix_http_status HTTP status codes per service in APISIX
# TYPE apisix_http_status counter
...
配置 Prometheus
在 Prometheus 中,目标是 Prometheus 抓取指标的端点。可以将 APISIX 指标端点配置为 Prometheus 目标,以便从中收集指标。
创建配置文件 prometheus.yml:
cat > prometheus.yml <<'EOF'
global:
scrape_interval: 15s
scrape_configs:
- job_name: apisix
metrics_path: /apisix/prometheus/metrics
static_configs:
- targets:
- apisix-quickstart:9091
EOF
创建数据卷并启动 Prometheus。主机上的端口 9092 映射到容器内端口 9090 上的 Prometheus Web 界面:
docker volume create apisix-quickstart-prometheus-data
docker run -d --name apisix-quickstart-prometheus \
--network apisix-quickstart-net \
-p 9092:9090 \
-v "$(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml:ro" \
-v apisix-quickstart-prometheus-data:/prometheus \
prom/prometheus:v3.7.3
打开 Prometheus 目标页面。apisix 目标的状态应为 UP。
