Qwen3-32B Web网关监控:Clawdbot集成Prometheus指标采集教程
1. 为什么需要监控Qwen3-32B的Web网关
你可能已经把Qwen3-32B跑起来了,Clawdbot也连上了,聊天界面能用,看起来一切正常。但等真正开始用——比如同时接入几十个用户、跑批量任务、或者做长时间对话测试时,问题就悄悄冒出来了:响应突然变慢、请求开始超时、偶尔返回空结果、甚至服务直接卡住。
这些都不是“能不能用”的问题,而是“稳不稳定”“撑不撑得住”的问题。
而这些问题,光靠肉眼观察页面、翻日志、手动curl接口是根本抓不住的。你需要一套能实时看到数字的系统:当前有多少并发请求?平均响应时间是多少毫秒?API错误率有没有突然跳到5%?模型推理队列积压了多少?内存用了多少?GPU显存是不是快爆了?
这就是Prometheus的作用——它不是锦上添花的工具,而是你把Qwen3-32B从“能跑”变成“敢用”“敢上线”的关键一环。
本教程不讲抽象概念,不堆术语,只带你一步步把Prometheus接进Clawdbot + Qwen3-32B的Web网关链路里,让所有核心指标自动上报、可视化、可告警。全程基于真实部署结构:Ollama提供模型API → 内部代理转发(8080→18789)→ Clawdbot调用 → 用户访问前端页面。
你不需要改模型代码,也不用动Ollama源码,只需要加一层轻量级指标暴露器,再配几个配置文件,就能拿到生产级可观测能力。
2. 整体架构与数据流向
2.1 当前系统是怎么连通的
先理清楚你已经在跑的这条链路,因为监控必须贴着实际流量走:
- Qwen3-32B模型由Ollama本地加载,监听在
http://localhost:11434/api/chat(默认Ollama API端点) - 一个内部反向代理(比如Nginx或自研Go代理)把外部请求从
http://your-server:8080/v1/chat/completions转发到Ollama,并额外做了端口映射:8080端口进,18789端口出(注意:18789是代理对外暴露的网关端口,不是Ollama原生端口) - Clawdbot作为客户端,直连这个
18789端口发起HTTP请求 - 最终用户通过Clawdbot提供的Web页面(就是你截图里的那个界面)和大模型对话
所以真正的请求路径是:
用户浏览器 → Clawdbot前端 → Clawdbot后端 → http://your-server:18789/v1/chat/completions → 反向代理 → http://localhost:11434/api/chat → Qwen3-32B
监控点不能只盯Ollama,也不能只看Clawdbot前端——最该被监控的是18789这个Web网关端口,因为它是整个链路的“咽喉”:所有流量必经此处,所有超时、错误、延迟都最先在这里体现。
2.2 Prometheus要监控什么
我们不追求大而全,只聚焦4类对运维和体验影响最大的指标:
| 指标类型 | 具体内容 | 为什么重要 |
|---|---|---|
| 请求维度 | 总请求数、成功率(HTTP 2xx/5xx占比)、平均响应时间(P90/P95) | 直接反映服务是否可用、用户是否卡顿 |
| 网关维度 | 当前活跃连接数、请求排队长度、转发耗时(代理层开销) | 判断是模型慢,还是代理本身成了瓶颈 |
| 资源维度 | 代理进程CPU使用率、内存占用、打开文件数 | 防止代理因资源耗尽导致请求丢弃 |
| 模型维度 | Ollama API调用延迟、模型加载状态、token生成速率(可选) | 确认Qwen3-32B是否健康,有没有OOM或卡死 |
注意:我们不监控Ollama原生指标(它本身不暴露Prometheus格式),而是通过代理层“旁路采集”——在请求经过18789端口时,自动打点、计时、统计,零侵入模型本身。
3. 部署指标暴露器:轻量级HTTP代理增强版
3.1 选择方案:用Prometheus Client for Go写一个增强代理
你不需要重写整个代理。我们采用“中间件式增强”思路:在现有反向代理(假设是用Go写的,这是Clawdbot生态常见做法)基础上,加几十行代码,让它同时具备两个能力:
- 正常转发HTTP请求(保持原有功能不变)
- 自动记录每条请求的路径、状态码、耗时、大小,并暴露
/metrics端点供Prometheus抓取
如果你用的是Nginx,也可以用Nginx VTS模块 + nginx-prometheus-exporter,但配置稍复杂。Go代理方案更透明、更可控,也更贴合Clawdbot技术栈。
下面是一个最小可行的增强代理核心逻辑(Go语言):
// main.go package main import ( "log" "net/http" "net/http/httputil" "net/url" "time" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) var ( // 定义4个核心指标 httpRequestsTotal = prometheus.NewCounterVec( prometheus.CounterOpts{ Name: "clawdbot_qwen3_gateway_requests_total", Help: "Total number of HTTP requests to Qwen3 gateway", }, []string{"code", "method", "path"}, ) httpRequestDuration = prometheus.NewHistogramVec( prometheus.HistogramOpts{ Name: "clawdbot_qwen3_gateway_request_duration_seconds", Help: "Latency of HTTP requests to Qwen3 gateway", Buckets: []float64{0.01, 0.05, 0.1, 0.25, 0.5, 1, 2, 5}, }, []string{"code", "method", "path"}, ) httpRequestSize = prometheus.NewSummaryVec( prometheus.SummaryOpts{ Name: "clawdbot_qwen3_gateway_request_size_bytes", Help: "Size of HTTP request body", }, []string{"method", "path"}, ) httpResponseSize = prometheus.NewSummaryVec( prometheus.SummaryOpts{ Name: "clawdbot_qwen3_gateway_response_size_bytes", Help: "Size of HTTP response body", }, []string{"code", "method", "path"}, ) ) func init() { prometheus.MustRegister(httpRequestsTotal, httpRequestDuration, httpRequestSize, httpResponseSize) } func main() { // 假设Ollama运行在 localhost:11434 backendURL, _ := url.Parse("http://localhost:11434") proxy := httputil.NewSingleHostReverseProxy(backendURL) // 自定义Director:把 /v1/chat/completions 映射到 Ollama 的 /api/chat originalDirector := proxy.Director proxy.Director = func(req *http.Request) { originalDirector(req) if req.URL.Path == "/v1/chat/completions" { req.URL.Path = "/api/chat" req.URL.RawQuery = "" } } // 中间件:记录指标 + 转发 http.Handle("/v1/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { start := time.Now() path := r.URL.Path method := r.Method // 记录请求大小(仅body,不含header) var reqBodySize float64 if r.Body != nil { reqBodySize = float64(r.ContentLength) } httpRequestSize.WithLabelValues(method, path).Observe(reqBodySize) // 执行转发 r.Header.Set("X-Forwarded-For", getClientIP(r)) proxy.ServeHTTP(w, r) // 记录响应指标 statusCode := http.StatusText(w.(interface{ Status() int }).Status()) duration := time.Since(start).Seconds() httpRequestsTotal.WithLabelValues(statusCode, method, path).Inc() httpRequestDuration.WithLabelValues(statusCode, method, path).Observe(duration) // 记录响应大小(估算,实际需包装responseWriter) respSize := float64(0) httpResponseSize.WithLabelValues(statusCode, method, path).Observe(respSize) })) // 暴露/metrics端点 http.Handle("/metrics", promhttp.Handler()) log.Println("Qwen3-32B Gateway Proxy started on :18789") log.Fatal(http.ListenAndServe(":18789", nil)) } func getClientIP(r *http.Request) string { for _, h := range []string{"X-Forwarded-For", "X-Real-IP"} { if ip := r.Header.Get(h); ip != "" { return ip } } return r.RemoteAddr }说明:这段代码做了三件事——
① 把/v1/chat/completions请求自动转成Ollama能识别的/api/chat;
② 在每次请求进出时,自动记录状态码、耗时、大小;
③ 开启:18789/metrics端点,Prometheus可以直接抓取。
编译后直接运行,它就替代了你原来的代理,功能完全一致,还多了一个监控入口。
3.2 编译与启动
确保你已安装Go(1.19+):
# 下载依赖 go mod init qwen3-gateway-metrics go get github.com/prometheus/client_golang/prometheus go get github.com/prometheus/client_golang/prometheus/promhttp go get net/http/httputil # 编译(生成单文件二进制) go build -o qwen3-proxy . # 后台运行(建议用systemd或supervisord管理) ./qwen3-proxy &验证是否生效:
curl http://localhost:18789/metrics | head -20你应该看到类似这样的输出:
# HELP clawdbot_qwen3_gateway_requests_total Total number of HTTP requests to Qwen3 gateway # TYPE clawdbot_qwen3_gateway_requests_total counter clawdbot_qwen3_gateway_requests_total{code="200",method="POST",path="/v1/chat/completions"} 127 clawdbot_qwen3_gateway_requests_total{code="400",method="POST",path="/v1/chat/completions"} 3 # HELP clawdbot_qwen3_gateway_request_duration_seconds Latency of HTTP requests to Qwen3 gateway # TYPE clawdbot_qwen3_gateway_request_duration_seconds histogram clawdbot_qwen3_gateway_request_duration_seconds_bucket{code="200",method="POST",path="/v1/chat/completions",le="0.1"} 89只要能看到这些指标,说明暴露器已就绪。
4. 配置Prometheus抓取目标
4.1 修改prometheus.yml
Prometheus默认配置文件通常在/etc/prometheus/prometheus.yml。添加一个新的job,专门抓取你的Qwen3网关:
# prometheus.yml global: scrape_interval: 15s evaluation_interval: 15s scrape_configs: # 原有job保持不变... - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] # 新增:Qwen3-32B网关监控 - job_name: 'qwen3-gateway' static_configs: - targets: ['your-server-ip:18789'] # 替换为你的服务器真实IP metrics_path: '/metrics' scheme: http # 可选:加点标签方便筛选 labels: instance: 'qwen3-32b-web-gateway' environment: 'production'4.2 重启Prometheus并确认目标在线
# 重载配置(无需重启) curl -X POST http://localhost:9090/-/reload # 或者重启服务 sudo systemctl restart prometheus打开Prometheus Web界面(http://your-server:9090),点击Status → Targets,找到qwen3-gateway这一项,状态应为UP,Last Scrape显示最近时间。
如果显示DOWN,请检查:
qwen3-proxy进程是否在运行(ps aux | grep qwen3-proxy)- 防火墙是否放行18789端口(
sudo ufw allow 18789) your-server-ip是否填错(本地测试可直接填localhost)
5. 在Grafana中构建实用监控看板
5.1 导入现成模板(推荐新手)
我们为你准备了一个专用于Qwen3-32B网关的Grafana看板JSON(含12个核心面板),涵盖:
- 实时请求速率与成功率热力图
- P95响应延迟趋势(按状态码分色)
- 活跃连接数与排队长度监控
- 请求大小分布(识别异常大请求)
- 错误明细下钻(快速定位4xx/5xx原因)
下载地址:qwen3-gateway-dashboard.json(注:此为示意链接,实际使用时请替换为真实托管地址)
导入步骤:
- 登录Grafana → Dashboards → Manage → Import
- 点击 “Upload JSON file”,选择下载的文件
- 选择已配置好的Prometheus数据源 → Import
5.2 关键面板解读与日常怎么看
即使不导入完整看板,你也应该掌握这几个核心查询,在Prometheus表达式浏览器里直接输入:
当前每秒请求数
rate(clawdbot_qwen3_gateway_requests_total[1m])成功率(过去5分钟)
100 * (1 - rate(clawdbot_qwen3_gateway_requests_total{code=~"5.."}[5m]) / rate(clawdbot_qwen3_gateway_requests_total[5m]))P95响应延迟(单位:秒)
histogram_quantile(0.95, rate(clawdbot_qwen3_gateway_request_duration_seconds_bucket[5m]))当前排队中的请求数(需代理支持排队计数,可在代码中扩展)
clawdbot_qwen3_gateway_queue_length(如需此指标,可在代理中增加goroutine计数器)
小技巧:把这三个查询保存为Grafana的“Quick Switch”快捷入口,早会同步状态时,3秒就能看清全局健康度。
6. 设置基础告警规则
光看图不够,得让系统主动提醒你。在Prometheus目录下新建alerts/qwen3-gateway.rules.yml:
groups: - name: qwen3-gateway-alerts rules: - alert: Qwen3GatewayHighErrorRate expr: 100 * (rate(clawdbot_qwen3_gateway_requests_total{code=~"5.."}[5m]) / rate(clawdbot_qwen3_gateway_requests_total[5m])) > 2 for: 3m labels: severity: warning annotations: summary: "Qwen3网关错误率过高 ({{ $value }}%)" description: "过去5分钟内5xx错误占比超过2%,请检查Ollama模型状态或代理负载" - alert: Qwen3GatewayHighLatency expr: histogram_quantile(0.95, rate(clawdbot_qwen3_gateway_request_duration_seconds_bucket[5m])) > 3 for: 2m labels: severity: critical annotations: summary: "Qwen3网关P95延迟超3秒" description: "用户将明显感知卡顿,可能由GPU显存不足或Ollama推理阻塞导致" - alert: Qwen3GatewayDown expr: probe_success{job="qwen3-gateway"} == 0 for: 1m labels: severity: critical annotations: summary: "Qwen3网关服务不可达" description: "无法访问:18789/metrics,代理进程可能已崩溃"然后在prometheus.yml中引用:
rule_files: - "alerts/*.rules.yml"重载Prometheus,告警就会生效。你可以用Alertmanager配置邮件、企业微信或钉钉通知,这里不再展开——重点是,这三条规则覆盖了90%的线上故障场景。
7. 总结:你现在已经拥有了什么
1. 一条可监控的Qwen3-32B服务链路
不再是黑盒调用,每个请求都有迹可循,每个错误都有上下文,每个延迟都有归因。
2. 一个开箱即用的指标暴露器
不用改Ollama,不用动Clawdbot,只替换代理二进制,就获得全链路观测能力。
3. 一套可落地的告警机制
当错误率突增、延迟飙升、服务宕机时,你比用户更早收到通知,而不是等投诉进来。
4. 一个持续优化的起点
有了数据,你就能回答真实问题:
- 是该升级GPU,还是优化提示词长度?
- 是该加缓存,还是该调小max_tokens?
- 是Clawdbot前端并发太高,还是Ollama本身扛不住?
监控不是终点,而是你把Qwen3-32B真正用深、用稳、用出价值的第一步。
下一步,你可以:
把指标接入企业微信,让值班同学手机收告警
在Clawdbot前端嵌入实时延迟小部件(告诉用户“当前响应约1.2秒”)
对比不同batch size下的P95延迟,找到最优推理参数
但所有这些,都建立在今天你亲手部署的这套监控之上。
获取更多AI镜像
想探索更多AI镜像和应用场景?访问 CSDN星图镜像广场,提供丰富的预置镜像,覆盖大模型推理、图像生成、视频生成、模型微调等多个领域,支持一键部署。