使用 Prometheus 和 Grafana 监控 FastAPI 服务

在现代应用开发中,监控和可视化服务的运行状态和性能指标对于保证系统稳定性至关重要。本文将介绍如何使用 Prometheus 和 Grafana 对 FastAPI 服务进行监控和可视化,并展示如何通过 prometheus_fastapi_instrumentator 将 FastAPI 应用与 Prometheus 集成。

1. 环境准备

首先,你需要准备以下环境:

  • Docker: 用于快速部署 Prometheus 和 Grafana。
  • python: 本地安装好python环境

2. 安装 Prometheus

创建 Prometheus 配置文件

在服务器上创建一个 prometheus.yml 文件,定义需要监控的目标:

yaml 复制代码
global:
  scrape_interval: 15s
scrape_configs:
  - job_name: 'fastapi_service'
    static_configs:
      - targets: ['localhost:8000']  # 替换为你的 FastAPI 服务地址和端口
使用 Docker 运行 Prometheus

在服务器上执行以下命令启动 Prometheus:

bash 复制代码
docker run -d \
  --name prometheus \
  -p 9090:9090 \
  -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
  prom/prometheus
  • -v /your_path/prometheus.yml:/etc/prometheus/prometheus.yml:将本地配置文件映射到容器内。
  • -p 9090:9090:将 Prometheus 的 9090 端口暴露出来。
访问 Prometheus

在浏览器中访问 http://<your-server-ip>:9090,确保 Prometheus 正常运行。

3. 安装 Grafana

使用 Docker 运行 Grafana

在服务器上执行以下命令启动 Grafana:

bash 复制代码
docker run -d \
  --name=grafana \
  -p 3000:3000 \
  grafana/grafana
  • -p 3000:3000:将 Grafana 的 3000 端口暴露出来。
访问 Grafana

在浏览器中访问 http://<your-server-ip>:3000,默认用户名为 admin,密码也是 admin。首次登录后,系统会提示你修改密码。

4. 配置 Prometheus 数据源

  1. 登录 Grafana 后,点击左侧菜单栏的 "Configuration" -> "Data Sources"。
  2. 点击 "Add data source"。
  3. 选择 "Prometheus"。
  4. 在 "URL" 栏中填入 http://<your-server-ip>:9090
  5. 点击 "Save & Test" 确保配置成功。

5. 集成 Prometheus 到 FastAPI

要将 Prometheus 与 FastAPI 集成,你可以使用 prometheus_fastapi_instrumentator 库。

安装 prometheus_fastapi_instrumentator

执行以下命令安装:

bash 复制代码
pip install prometheus-fastapi-instrumentator
修改 FastAPI 应用

在你的 FastAPI 应用中,导入并使用 PrometheusInstrumentator

python 复制代码
from fastapi import FastAPI
from prometheus_fastapi_instrumentator import Instrumentator
app = FastAPI()
# 添加 Prometheus 监控
instrumentator = Instrumentator()
instrumentator.instrument(app).expose(app)
验证指标端点

启动 FastAPI 应用,并访问 http://localhost:8000/metrics,确保 Prometheus 格式的指标数据正常展示。

6. 创建 Grafana 仪表盘

  1. 添加面板
    • 点击左侧菜单栏的 "+" -> "Dashboard"。
    • 点击 "Add new panel" 创建新的面板。
    • 在 "Query" 部分选择 Prometheus 数据源,填写 PromQL 查询语句,例如 rate(http_requests_total[1m]) 用于监控 HTTP 请求速率。
    • 配置完毕后,点击 "Apply" 保存面板,一个监控请求数的图表就做好了。

7. 设置告警

  • 在 Prometheus 中定义告警规则并配置告警管理器(Alertmanager)来处理告警。
  • 在 Grafana 中基于 Prometheus 的数据源设置告警阈值,并配置通知渠道(如邮件、Slack)。

总结

通过上述步骤,你可以成功实现 Prometheus 和 Grafana 的集成,对 FastAPI 服务进行全面的监控和可视化。Prometheus 负责采集和存储指标数据,Grafana 提供强大的可视化功能,本地搭建监控环境可以帮我们本地压测提供数据支撑。

相关推荐
DLYSB_29 分钟前
Kafka 消费倾斜死锁与 Partition 掉队:我用 Rust 写了个“数据管道物理哨兵”,比 Grafana 报警快了 18 秒
rust·kafka·grafana·报警灯
云烟成雨TD42 分钟前
Micrometer 系列【63】统一观测:基于 Spring Boot 的生产级演示案例 | 基于 OTLP 集成 Prometheus + Jaeger
spring boot·云原生·prometheus
Python私教13 小时前
API 输出模型怎么设计:从 model_dump() 到显式展示层
python·fastapi
Python私教14 小时前
本地 AI 工具服务该绑定 127.0.0.1 还是 0.0.0.0?
python·fastapi
weixin_4713830319 小时前
08 Chroma 持久化 + FastAPI 封装
fastapi·chroma
京东云开发者1 天前
【全栈实践】第一个 AI Agent 项目:从零搭建 AI 音频创作助手(进阶篇)
typescript·fastapi·vuex
云和数据.ChenGuang2 天前
fastapi项目拆分实战数据模型
java·服务器·数据库·人工智能·深度学习·fastapi·强化学习
燐妤2 天前
中老年智能健康管理项目
python·ai·pycharm·vue3·fastapi·项目开发·健康
circuitsosk2 天前
构建高可用AI后端服务:REST API设计、数据库交互及异步任务编排经验总结
数据库·人工智能·python·交互·fastapi·数据库连接池·rest api
java1234_小锋3 天前
【免费】基于Python的微信小程序网约车(打车,在线叫车,移动出行)系统(FastAPI+Vue3) 锋哥原创出品,必属精品
微信小程序·小程序·fastapi·网约车系统·在线叫车系统