prometheus + Grafana 监控 fastapi程序

 

prometheus + Grafana监控见:https://chuna2.787528.xyz/zhongyehai/p/18544418  此次重点是监控fastapi程序,这里不重复讲了

 

安装监控: pip install prometheus_client

 

编写监控代码

from prometheus_client import Counter, Histogram

# 定义监控指标
REQUEST_COUNT = Counter('http_requests_total', 'Total HTTP requests', ['method', 'endpoint', 'status'])
REQUEST_DURATION = Histogram('http_request_duration_seconds', 'HTTP request duration', ['method', 'endpoint'])

image

 

在请求拦截处记录指标

start_time = time.time()
response: Response = await call_next(request)
duration = time.time() - start_time

# 记录监控指标
REQUEST_COUNT.labels(method=request.method, endpoint=request.url.path, status=response.status_code).inc()
REQUEST_DURATION.labels(method=request.method, endpoint=request.url.path).observe(duration)

image

 

注册一个接口,用于返回监控数据

from fastapi import Request
from fastapi.responses import Response
from prometheus_client import generate_latest


async def metrics(request: Request):
  return Response(generate_latest(), media_type="text/plain")

image

 

配置 prometheus ,监听这个接口的数据,编辑 prometheus.yml,添加以下配置,验证配置没问题后重启prometheus 

image

- job_name: "fastapi_monitor"
  metrics_path: /api/system/monitor/metrics
  static_configs:
  - targets:
    - "192.168.x.xx:8023"

 

配置 好Grafana数据源后,新建仪表盘

image

添加可视化

image

选择数据源

image

添加几个查询

QPS图表:sum(rate(http_requests_total{job="fastapi_monitor"}[5m])) by (endpoint, method)

image

接口响应耗时 (P50, P95, P99 )
P50: histogram_quantile(0.50, sum(rate(http_request_duration_seconds_bucket{job="fastapi_monitor"}[5m])) by (le, endpoint))
P95: histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket{job="fastapi_monitor"}[5m])) by (le, endpoint))
P99: histogram_quantile(0.99, sum(rate(http_request_duration_seconds_bucket{job="fastapi_monitor"}[5m])) by (le, endpoint))

image

 

最后的效果

image

 

其他的监控内容可以问大模型,但数据都是基于代码里面的监控指标来的

image

 

posted @ 2026-03-10 17:17  向前走。  阅读(8)  评论(0)    收藏  举报