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提供的专业监控可视化
实际部署时,建议根据业务规模调整采集频率和资源分配,确保监控系统本身不会成为性能瓶颈。定期审查告警规则和监控指标,保持监控体系的有效性和准确性。
登录后查看全文
热门项目推荐
相关项目推荐
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5-w4a8GLM-5-w4a8基于混合专家架构,专为复杂系统工程与长周期智能体任务设计。支持单/多节点部署,适配Atlas 800T A3,采用w4a8量化技术,结合vLLM推理优化,高效平衡性能与精度,助力智能应用开发Jinja00
请把这个活动推给顶尖程序员😎本次活动专为懂行的顶尖程序员量身打造,聚焦AtomGit首发开源模型的实际应用与深度测评,拒绝大众化浅层体验,邀请具备扎实技术功底、开源经验或模型测评能力的顶尖开发者,深度参与模型体验、性能测评,通过发布技术帖子、提交测评报告、上传实践项目成果等形式,挖掘模型核心价值,共建AtomGit开源模型生态,彰显顶尖程序员的技术洞察力与实践能力。00
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00
MiniMax-M2.5MiniMax-M2.5开源模型,经数十万复杂环境强化训练,在代码生成、工具调用、办公自动化等经济价值任务中表现卓越。SWE-Bench Verified得分80.2%,Multi-SWE-Bench达51.3%,BrowseComp获76.3%。推理速度比M2.1快37%,与Claude Opus 4.6相当,每小时仅需0.3-1美元,成本仅为同类模型1/10-1/20,为智能应用开发提供高效经济选择。【此简介由AI生成】Python00
Qwen3.5Qwen3.5 昇腾 vLLM 部署教程。Qwen3.5 是 Qwen 系列最新的旗舰多模态模型,采用 MoE(混合专家)架构,在保持强大模型能力的同时显著降低了推理成本。00- RRing-2.5-1TRing-2.5-1T:全球首个基于混合线性注意力架构的开源万亿参数思考模型。Python00
最新内容推荐
Degrees of Lewdity中文汉化终极指南:零基础玩家必看的完整教程Unity游戏翻译神器:XUnity Auto Translator 完整使用指南PythonWin7终极指南:在Windows 7上轻松安装Python 3.9+终极macOS键盘定制指南:用Karabiner-Elements提升10倍效率Pandas数据分析实战指南:从零基础到数据处理高手 Qwen3-235B-FP8震撼升级:256K上下文+22B激活参数7步搞定机械键盘PCB设计:从零开始打造你的专属键盘终极WeMod专业版解锁指南:3步免费获取完整高级功能DeepSeek-R1-Distill-Qwen-32B技术揭秘:小模型如何实现大模型性能突破音频修复终极指南:让每一段受损声音重获新生
项目优选
收起
deepin linux kernel
C
27
11
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
568
3.84 K
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
68
20
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
12
1
暂无简介
Dart
801
199
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.37 K
781
喝着茶写代码!最易用的自托管一站式代码托管平台,包含Git托管,代码审查,团队协作,软件包和CI/CD。
Go
24
0
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
349
202
Ascend Extension for PyTorch
Python
379
452
无需学习 Kubernetes 的容器平台,在 Kubernetes 上构建、部署、组装和管理应用,无需 K8s 专业知识,全流程图形化管理
Go
16
1