基于Prometheus的LLM指标可视化方案
最近在尝试将LLM服务接入Prometheus监控体系时,踩了不少坑。分享一下我的实践过程。
环境准备
首先需要部署Prometheus服务和Grafana面板。我使用Docker快速部署:
# 启动Prometheus
docker run -d --name prometheus \
-p 9090:9090 \
-v ./prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
# 启动Grafana
docker run -d --name grafana \
-p 3000:3000 \
grafana/grafana
LLM服务指标接入
在LLM服务中集成Prometheus客户端库:
from prometheus_client import start_http_server, Histogram, Counter
import time
# 创建指标
request_duration = Histogram('llm_request_duration_seconds', 'LLM请求耗时')
request_count = Counter('llm_requests_total', 'LLM总请求数')
# 启动监控端口
start_http_server(8000)
@app.route('/predict')
def predict():
with request_duration.time():
# 业务逻辑
result = llm_model.predict(data)
request_count.inc()
return result
配置Prometheus抓取
配置prometheus.yml文件:
scrape_configs:
- job_name: 'llm-service'
static_configs:
- targets: ['localhost:8000']
Grafana可视化
创建Dashboard时,建议关注以下关键指标:
- 请求耗时分布
- QPS变化趋势
- 错误率监控
注意:如果服务重启后指标数据丢失,需要配置持久化存储。
踩坑总结
- 保证指标命名规范统一
- 避免指标维度过多导致性能下降
- 定期清理无用指标
- 监控告警阈值设置要合理

讨论