Apache APISIX监控告警:Prometheus+Grafana监控体系
2026-02-04 04:52:51作者:鲍丁臣Ursa
概述
在现代微服务架构中,API网关作为流量入口,其稳定性和性能至关重要。Apache APISIX作为云原生API网关,提供了强大的监控能力。本文将深入探讨如何构建基于Prometheus和Grafana的完整监控告警体系,确保APISIX网关的可靠运行。
监控架构设计
graph TB
A[APISIX网关节点] --> B[Prometheus插件]
B --> C[指标暴露端点]
C --> D[Prometheus Server]
D --> E[Grafana Dashboard]
D --> F[Alertmanager]
F --> G[告警通知]
subgraph "数据采集层"
A
B
C
end
subgraph "存储计算层"
D
end
subgraph "可视化告警层"
E
F
G
end
Prometheus插件配置详解
基础配置启用
在APISIX中启用Prometheus监控非常简单,只需在路由配置中添加空配置即可:
curl http://127.0.0.1:9180/apisix/admin/routes/1 \
-H "X-API-KEY: $admin_key" -X PUT -d '
{
"uri": "/api/*",
"plugins": {
"prometheus":{}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"backend-service:8080": 1
}
}
}'
高级配置选项
在conf/config.yaml中进行深度配置:
plugin_attr:
prometheus:
export_uri: /apisix/metrics
export_addr:
ip: 0.0.0.0
port: 9091
default_buckets:
- 10
- 50
- 100
- 500
- 1000
- 5000
metric_prefix: "apisix_"
expire: 60
核心监控指标体系
HTTP流量指标
| 指标名称 | 类型 | 描述 | 关键标签 |
|---|---|---|---|
apisix_http_status |
Counter | HTTP状态码统计 | code, route, service, consumer |
apisix_bandwidth |
Counter | 带宽消耗统计 | type(ingress/egress), route |
apisix_http_latency |
Histogram | 请求延迟分布 | type(request/upstream/apisix) |
apisix_nginx_http_current_connections |
Gauge | Nginx连接状态 | state(active/reading/writing) |
系统健康指标
| 指标名称 | 类型 | 描述 | 告警阈值 |
|---|---|---|---|
apisix_etcd_reachable |
Gauge | etcd连接状态 | <1 (持续30s) |
apisix_upstream_status |
Gauge | 上游节点健康状态 | =0 (持续60s) |
apisix_shared_dict_free_space_bytes |
Gauge | 共享内存使用率 | <10%容量 |
业务维度指标
pie title APISIX监控指标分类
"HTTP指标" : 45
"系统指标" : 25
"网络指标" : 20
"业务指标" : 10
Prometheus采集配置
基础采集配置
# prometheus.yml
scrape_configs:
- job_name: 'apisix'
scrape_interval: 15s
metrics_path: '/apisix/metrics'
static_configs:
- targets: ['apisix-node1:9091', 'apisix-node2:9091']
relabel_configs:
- source_labels: [__address__]
target_label: instance
regex: '(.*):\d+'
replacement: '${1}'
多实例集群配置
- job_name: 'apisix-cluster'
scrape_interval: 15s
metrics_path: '/apisix/metrics'
kubernetes_sd_configs:
- role: pod
namespaces:
names: ['apisix']
relabel_configs:
- source_labels: [__meta_kubernetes_pod_label_app]
action: keep
regex: apisix
- source_labels: [__meta_kubernetes_pod_ip]
target_label: __address__
regex: '(.*)'
replacement: '${1}:9091'
Grafana监控大屏配置
核心监控面板
1. 流量概览面板
-- 总请求量趋势
sum(rate(apisix_http_status[1m])) by (instance)
-- 错误率计算
sum(rate(apisix_http_status{code=~"5.."}[1m])) / sum(rate(apisix_http_status[1m]))
-- 带宽使用情况
sum(rate(apisix_bandwidth[1m])) by (type)
2. 延迟分析面板
-- P99延迟
histogram_quantile(0.99, sum(rate(apisix_http_latency_bucket[1m])) by (le, route))
-- 平均延迟
sum(rate(apisix_http_latency_sum[1m])) / sum(rate(apisix_http_latency_count[1m]))
-- 延迟分布
histogram_quantile(0.5, rate(apisix_http_latency_bucket[1m]))
histogram_quantile(0.9, rate(apisix_http_latency_bucket[1m]))
histogram_quantile(0.99, rate(apisix_http_latency_bucket[1m]))
3. 系统健康面板
-- etcd连接状态
apisix_etcd_reachable
-- 上游健康状态
apisix_upstream_status
-- 内存使用率
1 - (apisix_shared_dict_free_space_bytes / apisix_shared_dict_capacity_bytes)
告警规则配置
groups:
- name: apisix-alerts
rules:
- alert: APISIXHighErrorRate
expr: sum(rate(apisix_http_status{code=~"5.."}[5m])) / sum(rate(apisix_http_status[5m])) > 0.05
for: 5m
labels:
severity: critical
annotations:
summary: "APISIX高错误率告警"
description: "实例 {{ $labels.instance }} 5xx错误率超过5%,当前值: {{ $value }}"
- alert: APISIXHighLatency
expr: histogram_quantile(0.99, rate(apisix_http_latency_bucket[5m])) > 1000
for: 5m
labels:
severity: warning
annotations:
summary: "APISIX高延迟告警"
description: "实例 {{ $labels.instance }} P99延迟超过1000ms,当前值: {{ $value }}ms"
- alert: APISIXEtcdUnreachable
expr: apisix_etcd_reachable == 0
for: 1m
labels:
severity: critical
annotations:
summary: "APISIX etcd连接异常"
description: "实例 {{ $labels.instance }} 无法连接etcd配置中心"
- alert: APISIXUpstreamUnhealthy
expr: apisix_upstream_status == 0
for: 2m
labels:
severity: warning
annotations:
summary: "上游服务不可用"
description: "上游节点 {{ $labels.ip }}:{{ $labels.port }} 健康检查失败"
实战:端到端监控配置
步骤1:APISIX配置验证
# 验证Prometheus端点
curl http://localhost:9091/apisix/metrics
# 检查关键指标
curl -s http://localhost:9091/apisix/metrics | grep -E "(http_status|etcd_reachable|upstream_status)"
步骤2:Prometheus配置验证
# 检查target状态
curl http://localhost:9090/api/v1/targets | jq '.data.activeTargets[] | select(.labels.job == "apisix")'
# 验证指标采集
curl http://localhost:9090/api/v1/query?query=apisix_http_status_total
步骤3:Grafana数据源配置
{
"name": "APISIX-Prometheus",
"type": "prometheus",
"url": "http://prometheus:9090",
"access": "proxy",
"isDefault": true,
"jsonData": {
"timeInterval": "15s",
"httpMethod": "GET"
}
}
性能优化建议
监控数据采样优化
# 降低高频指标采集频率
scrape_interval: 30s
scrape_timeout: 25s
# 使用记录规则预计算
rule_files:
- /etc/prometheus/rules/apisix-recording.rules.yml
资源限制配置
# Prometheus资源限制
global:
scrape_interval: 15s
evaluation_interval: 15s
# 每实例指标限制
metric_relabel_configs:
- source_labels: [__name__]
regex: 'apisix_.*'
action: keep
故障排查指南
常见问题处理
| 问题现象 | 可能原因 | 解决方案 |
|---|---|---|
| 指标采集超时 | 网络问题或APISIX负载高 | 调整scrape_timeout,检查节点负载 |
| 指标数据缺失 | Prometheus配置错误 | 验证target状态和网络连通性 |
| 延迟指标异常 | 上游服务性能问题 | 分析upstream延迟分布,排查后端服务 |
诊断命令集
# 检查APISIX指标端点
curl -v http://apisix-node:9091/apisix/metrics
# 验证Prometheus target状态
curl http://localhost:9090/api/v1/targets | jq
# 查询特定指标
curl "http://localhost:9090/api/v1/query?query=apisix_etcd_reachable"
# 检查告警状态
curl http://localhost:9090/api/v1/alerts
总结
通过本文的完整指南,您已经掌握了Apache APISIX基于Prometheus和Grafana的监控告警体系构建。这套监控方案提供了:
- 全面的指标覆盖:从HTTP流量到系统健康的全方位监控
- 多维度分析:支持按路由、服务、消费者等多维度分析
- 智能告警:基于Prometheus Alertmanager的智能告警机制
- 可视化大屏:Grafana提供的专业监控可视化
实际部署时,建议根据业务规模调整采集频率和资源分配,确保监控系统本身不会成为性能瓶颈。定期审查告警规则和监控指标,保持监控体系的有效性和准确性。
登录后查看全文
热门项目推荐
相关项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust098- DDeepSeek-V4-ProDeepSeek-V4-Pro(总参数 1.6 万亿,激活 49B)面向复杂推理和高级编程任务,在代码竞赛、数学推理、Agent 工作流等场景表现优异,性能接近国际前沿闭源模型。Python00
MiMo-V2.5-ProMiMo-V2.5-Pro作为旗舰模型,擅⻓处理复杂Agent任务,单次任务可完成近千次⼯具调⽤与⼗余轮上 下⽂压缩。Python00
GLM-5.1GLM-5.1是智谱迄今最智能的旗舰模型,也是目前全球最强的开源模型。GLM-5.1大大提高了代码能力,在完成长程任务方面提升尤为显著。和此前分钟级交互的模型不同,它能够在一次任务中独立、持续工作超过8小时,期间自主规划、执行、自我进化,最终交付完整的工程级成果。Jinja00
Kimi-K2.6Kimi K2.6 是一款开源的原生多模态智能体模型,在长程编码、编码驱动设计、主动自主执行以及群体任务编排等实用能力方面实现了显著提升。Python00
MiniMax-M2.7MiniMax-M2.7 是我们首个深度参与自身进化过程的模型。M2.7 具备构建复杂智能体应用框架的能力,能够借助智能体团队、复杂技能以及动态工具搜索,完成高度精细的生产力任务。Python00
热门内容推荐
最新内容推荐
Notepad--极速优化指南:中文开发者的轻量编辑器解决方案Axure RP本地化配置指南:提升设计效率的中文界面切换方案3个技巧让你10分钟消化3小时视频,B站学习效率翻倍指南让虚拟角色开口说话:ComfyUI语音驱动动画全攻略7个效率倍增技巧:用开源工具实现系统优化与性能提升开源船舶设计新纪元:从技术原理到跨界创新的实践指南Zynq UltraScale+ RFSoC零基础入门:软件定义无线电Python开发实战指南VRCX虚拟社交管理系统:技术驱动的VRChat社交体验优化方案企业级Office插件开发:从概念验证到生产部署的完整实践指南语音转换与AI声音克隆:开源工具实现高质量声音复刻全指南
项目优选
收起
deepin linux kernel
C
28
16
Claude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed.
Get Started
Rust
568
98
暂无描述
Dockerfile
709
4.51 K
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
958
955
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.61 K
942
Ascend Extension for PyTorch
Python
572
694
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
413
339
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
1.42 K
116
暂无简介
Dart
951
235
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
12
2