使用 Prometheus 和 Grafana在FastAPI 应用程序中进行监控和日志记录
监控和日志记录为何重要
- 性能洞察:识别瓶颈并优化性能。
- 错误检测:快速检测和诊断问题。
- 用户体验:确保应用程序为最终用户顺利运行。
- 维护:促进更轻松的调试和维护。
先决条件
- FastAPI 的基本知识。
- 您的机器上安装了 Docker。
- 要监控的 FastAPI 应用程序。
使用 Docker 设置 Prometheus 和 Grafana
-
- 创建 Docker Compose 文件创建 docker-compose.yml 文件来设置 Prometheus 和 Grafana。
version: '3.7'services:prometheus:image: prom/prometheusvolumes:./prometheus.yml:/etc/prometheus/prometheus.ymlports:"9090:9090"grafana:image: grafana/grafanaports:"3000:3000"
- 配置 Prometheus
创建 prometheus.yml 文件来配置 Prometheus。
global:scrape_interval: 15sscrape_configs:job_name: 'fastapi'scrape_interval: 5sstatic_configs:targets: ['host.docker.internal:8000']
将 Prometheus 与 FastAPI 集成
-
- 安装依赖项
pip install prometheus_client
-
- 修改您的 FastAPI 应用程序
from fastapi import FastAPIfrom prometheus_client import start_http_server, Summaryimport timeapp = FastAPI()# Create a metric to track time spent and requests made.REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')def process_request():time.sleep(2)def read_root():process_request()return {"Hello": "World"}if __name__ == "__main__":start_http_server(8000)import uvicornuvicorn.run(app, host="0.0.0.0", port=8000)
-
- 运行 Docker Compose
docker-compose up
设置 Grafana
- 访问 Grafana
- 打开浏览器并导航至 http://localhost:3000。默认登录名为 admin/admin。
- 将 Prometheus 添加为数据源
- 导航至配置 > 数据源。
- 添加 Prometheus 并将 URL 设置为 http://prometheus:9090。
- 创建仪表板
- 导航到创建 >仪表板。
- 添加新面板并选择您的 Prometheus 数据源。
- 使用 Prometheus 查询(如 request_processing_seconds_count)来可视化指标。
将日志记录与 FastAPI 集成
-
- 安装依赖项
pip install loguru
-
- 修改您的 FastAPI 应用程序
from fastapi import FastAPIfrom loguru import loggerapp = FastAPI()def read_root():logger.info("Root endpoint was called")return {"Hello": "World"}if __name__ == "__main__":import uvicornuvicorn.run(app, host="0.0.0.0", port=8000)
-
- 配置 LoguruLoguru 具有高度可配置性。您可以自定义日志格式、记录到文件等。
from loguru import loggerlogger.add("file_{time}.log", rotation="1 day")
以下是一些其他演示,深入介绍如何使用 Prometheus 和 Grafana 监控 FastAPI 应用程序。
演示 1:监控 FastAPI 中的自定义指标
步骤 1:定义自定义指标
定义自定义指标以跟踪 FastAPI 应用程序中的特定事件或数据点。
from fastapi import FastAPIfrom prometheus_client import Counter, Histogram, start_http_serverimport randomapp = FastAPI()# Create custom metricsREQUEST_COUNT = Counter('request_count', 'Total number of requests')REQUEST_LATENCY = Histogram('request_latency_seconds', 'Request latency in seconds')def random_number():REQUEST_COUNT.inc() # Increment the request countlatency = random.random()REQUEST_LATENCY.observe(latency) # Observe latencyreturn {"number": random.randint(1, 100)}if __name__ == "__main__":start_http_server(8000)import uvicornuvicorn.run(app, host="0.0.0.0", port=8000)
第 2 步:更新Prometheus 配置
确保 Prometheus 抓取 FastAPI 指标端点。
scrape_configs:job_name: 'fastapi'scrape_interval: 5sstatic_configs:targets: ['host.docker.internal:8000']
步骤 3:在 Grafana 中可视化自定义指标
- 通过 http://localhost:3000 访问 Grafana。
- 如果尚未完成,请添加 Prometheus 作为数据源。
- 创建新的仪表板和面板。
- 使用 Prometheus 查询 request_count 和 request_latency_seconds 可视化自定义指标。
演示 2:使用 Prometheus 和 Grafana 发出警报
根据 Prometheus 指标在 Grafana 中设置警报,以便在满足某些条件时通知您。
步骤 1:在 Prometheus 中定义警报规则
在名为 alert_rules.yml 的文件中创建警报规则。
groups:name: example_alertrules:alert: HighRequestLatencyexpr: request_latency_seconds_bucket{le="1"} > 0.5for: 1mlabels:severity: warningannotations:summary: "High request latency"description: "Request latency is greater than 1 second for more than 1 minute."
更新 prometheus.yml 以包含警报规则。
rule_files:- "alert_rules.yml"
步骤 2:配置 Alertmanager
设置 Alertmanager 来处理警报。创建 alertmanager.yml 配置文件。
global:resolve_timeout: 5mroute:receiver: 'email'receivers:name: 'email'email_configs:to: 'your-email@example.com'from: 'alertmanager@example.com'smarthost: 'smtp.example.com:587'auth_username: 'user'auth_password: 'password'
更新 docker-compose.yml 以包含 Alertmanager。
version: '3.7'services:prometheus:image: prom/prometheusvolumes:./prometheus.yml:/etc/prometheus/prometheus.yml./alert_rules.yml:/etc/prometheus/alert_rules.ymlports:"9090:9090"grafana:image: grafana/grafanaports:"3000:3000"alertmanager:image: prom/alertmanagervolumes:./alertmanager.yml:/etc/alertmanager/alertmanager.ymlports:"9093:9093"
步骤 3:在 Grafana 中创建警报
- 通过 http://localhost:3000 访问 Grafana。
- 导航到警报部分。
- 根据您的 Prometheus 指标创建新警报。
演示 3:使用 Grafana 仪表板监控 FastAPI 应用程序性能
步骤 1:在 Grafana 中创建仪表板
- 通过 http://localhost:3000 访问 Grafana。
- 如果尚未完成,请添加 Prometheus 作为数据源。
- 创建新仪表板。
- 添加各种指标的面板,例如请求数、延迟和错误率。
步骤 2:示例面板配置
请求数面板
- 查询:rate(request_count[1m])
- 可视化:图表
- 根据需要配置时间范围和刷新间隔。
请求延迟面板
- 查询:histogram_quantile(0.95, sum(rate(request_latency_seconds_bucket[1m])) by (le))
- 可视化:图表
- 根据需要配置时间范围和刷新间隔。
错误率面板
-
- 在 FastAPI 中定义错误计数器。
ERROR_COUNT = Counter('error_count', 'Total number of errors')def error_endpoint():ERROR_COUNT.inc() # Increment the error countraise ValueError("This is an error")
- 查询:rate(error_count[1m])
- 可视化:图表
- 根据需要配置时间范围和刷新间隔。
演示 4:使用 Grafana 注释进行事件跟踪
Grafana 中的注释可以帮助将指标与特定事件或部署关联起来。
步骤 1:在 Grafana 中创建注释
- 访问您的 Grafana 仪表板。
- 单击“添加注释”按钮(顶部栏上的铅笔图标)。
- 添加有关事件的详细信息,例如部署、错误修复或事件。
步骤 2:使用 API 自动注释
使用 Grafana 的 API 自动注释。例如,在部署后添加注释。
curl -X POST \-H "Content-Type: application/json" \-H "Authorization: Bearer YOUR_GRAFANA_API_KEY" \-d '{"dashboardId": 1,"time": 1609459200000,"tags": ["deployment"],"text": "Deployed version 2.0"}' \http://localhost:3000/api/annotations
将 YOUR_GRAFANA_API_KEY 替换为您的 Grafana API 密钥,并根据需要调整时间和文本。
这些演示提供了有关使用 Prometheus 和 Grafana 监控 FastAPI 应用程序的更多见解。通过定义自定义指标、设置警报、创建全面的仪表板和利用注释,您可以维护强大的监控和日志记录系统,以确保应用程序的健康和性能。
通过将 Prometheus 和 Grafana 与您的 FastAPI 应用程序集成,您可以监控其性能并可视化重要指标。此外,使用像 Loguru 这样的日志库有助于跟踪应用程序行为和错误,从而更轻松地维护和调试应用程序。使用这些工具,您将拥有一个强大的设置,以确保您的 FastAPI 应用程序顺利高效地运行。




