使用 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 提供强大的可视化功能,本地搭建监控环境可以帮我们本地压测提供数据支撑。

相关推荐
杰瑞学AI15 小时前
我的全栈学习之旅:FastAPI (持续更新!!!)
后端·python·websocket·学习·http·restful·fastapi
Python私教4 天前
浏览器不再拦请求:FastAPI 跨域(CORS)配置全解析
fastapi
是垚不是土4 天前
Prometheus接入“飞书“实现自动化告警
运维·安全·自动化·github·飞书·prometheus
朱包林6 天前
Prometheus监控K8S集群-ExternalName-endpoints-ElasticStack采集K8S集群日志实战
运维·云原生·容器·kubernetes·prometheus
带娃的IT创业者6 天前
第2集:技术选型的智慧:Flask vs FastAPI,GLM-4 vs GPT
python·gpt·flask·fastapi·glm·技术选型
3分云计算6 天前
Prometheus 05-01: 告警规则与Alertmanager配置
prometheus
散一世繁华,颠半世琉璃9 天前
Prometheus+Grafana轻松实现SpringBoot应用监控
spring boot·grafana·prometheus
万粉变现经纪人9 天前
如何解决 pip install 安装报错 ImportError: cannot import name ‘xxx’ from ‘yyy’ 问题
python·selenium·测试工具·flask·scikit-learn·fastapi·pip
Achou.Wang10 天前
kube-prometheus监控服务发现
服务发现·prometheus