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

相关推荐
hwj运维之路1 小时前
k8s监控方案实践(三):部署与配置Grafana可视化平台
云原生·kubernetes·grafana
Anesthesia丶2 天前
Vue3 + naive-ui + fastapi使用心得
vue.js·ui·fastapi
weixin_307779132 天前
使用FastAPI微服务在AWS EKS中构建上下文增强型AI问答系统
人工智能·python·云计算·fastapi·aws
掘金-我是哪吒2 天前
分布式微服务系统架构第130集:Python工程化FastAPI,运维Nginx-keepalived+Nginx实现高可用集群
运维·分布式·微服务·系统架构·fastapi
LingRannn2 天前
JWT的介绍与在Fastapi框架中的应用
fastapi
Python私教3 天前
使用FastAPI和React以及MongoDB构建全栈Web应用05 FastAPI快速入门
前端·react.js·fastapi
Python私教3 天前
全栈开发实战:FastAPI + React + MongoDB 构建现代Web应用
前端·react.js·fastapi
weixin_307779133 天前
使用FastAPI和Apache Flink构建跨环境数据管道
redis·python·云计算·fastapi·aws
一个向上的运维者4 天前
Prometheus生产实战全流程详解(存储/负载/调度篇)
云原生·prometheus
hwj运维之路4 天前
k8s监控方案实践(一):部署Prometheus与Node Exporter
容器·kubernetes·prometheus