如何用 Grafana 搭建蜘蛛抓取监控面板?
介绍从零搭建 Grafana+Prometheus 爬虫监控面板的方法,先定义抓取量、成功率、耗时、队列指标并避免高基数标签,再用 Docker Compose 部署服务并在爬虫中埋点上报指标。
学完这篇,你能从零搭起一套 Grafana + Prometheus 面板,实时看到爬虫抓了多少条、成功率多少、慢在哪个站点。
整套链路是:爬虫埋点上报指标 → Prometheus 定时拉取 → Grafana 查询并画图。下面按顺序做一遍。
第一步:先定清楚要监控哪几个指标
这一步是把「监控什么」想明白,做完你就有一张指标清单,后面所有配置都围绕它。
新手必看的四个维度:
- 抓取量:
spider_items_total,按站点打标签,用 Counter 类型 - 请求成功率:
spider_requests_total{status="ok|error"},Counter - 请求耗时分布:
spider_request_duration_seconds,Histogram,用来算 P95 - 队列积压:
spider_queue_size,Gauge
注意:千万别把 URL、商品 ID 这类高基数值做成标签。标签组合数会指数膨胀,几千个 URL 就能把 Prometheus 内存吃满。标签只留站点名、任务名这种个位数的维度。
第二步:用 Docker Compose 起 Prometheus + Pushgateway + Grafana
这一步把三个服务跑起来,做完你能在本机 9090 / 9091 / 3000 三个端口看到界面。
新建 docker-compose.yml:
services:
prometheus:
image: prom/prometheus:v2.51.0
ports: ["9090:9090"]
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
pushgateway:
image: prom/pushgateway:v1.7.0
ports: ["9091:9091"]
grafana:
image: grafana/grafana:10.4.2
ports: ["3000:3000"]
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
执行 docker compose up -d。浏览器打开 http://localhost:9090 看到 Prometheus 界面就成功了。
注意:Docker Compose 从 v2 起命令是
docker compose(中间有空格),老版本才是docker-compose。
第三步:在爬虫里埋点
这一步让爬虫把指标吐出来。分两种情况选,做完指标就能被采集到。
常驻爬虫(一直挂着的),用 HTTP 暴露 /metrics:
from prometheus_client import Counter, Histogram, Gauge, start_http_server
ITEMS = Counter("spider_items_total", "抓取条目数", ["site"])
REQUESTS = Counter("spider_requests_total", "请求总数", ["site", "status"])
DURATION = Histogram("spider_request_duration_seconds", "请求耗时", ["site"],
buckets=(0.1, 0.3, 0.5, 1, 2, 5, 10))
QUEUE = Gauge("spider_queue_size", "待抓队列长度")
start_http_server(8000) # 启动后访问 :8000/metrics 就能看到指标
ITEMS.labels(site="news").inc()
REQUESTS.labels(site="news", status="ok").inc()
DURATION.labels(site="news").observe(0.42)
跑完就退出的定时任务,改用 Pushgateway 主动推:
from prometheus_client import CollectorRegistry, Counter, push_to_gateway
registry = CollectorRegistry()
c = Counter("spider_items_total", "抓取条目数", ["site"], registry=registry)
c.labels(site="news").inc(37)
push_to_gateway("localhost:9091", job="spider_batch", registry=registry)
注意:Pushgateway 的数据不会自动过期。任务停跑后,面板上那条线会一直保持最后一次的值,看着像还在跑。下线时要手动清掉:
curl -X DELETE http://localhost:9091/metrics/job/spider_batch。
第四步:配置 Prometheus 抓取
这一步告诉 Prometheus 去哪拉数据。同目录新建 prometheus.yml:
global:
scrape_interval: 15s
scrape_configs:
- job_name: spider
static_configs:
- targets: ["host.docker.internal:8000"] # 常驻爬虫
- job_name: pushgateway
honor_labels: true
static_configs:
- targets: ["pushgateway:9091"] # 短任务
改完执行 docker compose restart prometheus,然后去 http://localhost:9090/targets 确认两个 job 都是 UP。
注意:Linux 上
host.docker.internal默认不解析,需要在 prometheus 服务下加extra_hosts: ["host.docker.internal:host-gateway"],或者直接写宿主机内网 IP。
第五步:Grafana 接入数据源
这一步打通 Grafana 到 Prometheus。打开 http://localhost:3000,用户名 admin、密码 admin(Grafana 10 会强制你改一次密码)。
左侧菜单 Connections → Data sources → Add data source,选 Prometheus。在 Connection 区块的 Prometheus server URL 填:
http://prometheus:9090
点底部 Save & test,出现绿色 "Successfully queried the Prometheus API" 即成功。
注意:这里必须填容器服务名
prometheus,不是localhost:9090。Grafana 跑在容器里,localhost指向的是它自己。
第六步:把面板画出来
这一步出图。左侧 Dashboards → New → New dashboard → Add visualization,选刚建的 Prometheus 数据源,在 Query 输入框里写 PromQL,右上角给 Panel title 起名,点 Apply,最后 Ctrl+S 保存。
四个面板的 PromQL 直接抄:
每分钟抓取量(Time series)
sum by (site) (rate(spider_items_total[5m])) * 60
请求成功率(Stat,单位选 Percent 0-100)
sum(rate(spider_requests_total{status="ok"}[5m]))
/ sum(rate(spider_requests_total[5m])) * 100
P95 耗时(Time series,单位秒)
histogram_quantile(0.95,
sum by (le, site) (rate(spider_request_duration_seconds_bucket[5m])))
队列积压(Stat)
spider_queue_size
注意:
rate()的窗口至少要是scrape_interval的两倍。抓取间隔 15s 却写rate(x[10s]),会返回空值,面板一片 No data。
第七步:加个站点下拉筛选
这一步让你能按站点切换看板。点 Dashboard 右上角齿轮图标 Dashboard settings → Variables → New variable:
- Select variable type 选
Query - Name 填
site - Data source 选 Prometheus
- Query 填
label_values(spider_items_total, site)
点 Apply 保存。回到面板,在每个 PromQL 里把 spider_items_total 改成 spider_items_total{site="$site"},之后顶部就会出现站点下拉框。
第八步:配错误告警(可选)
左侧 Alerting → Alert rules → New alert rule,选 Prometheus 数据源,Query 填:
sum(rate(spider_requests_total{status="error"}[5m])) > 1
在 Set evaluation behavior 里设 Evaluate every 为 1m、Pending period 为 5m(持续 5 分钟才发),再到 Contact points 配一个邮件或钉钉 Webhook。这样错误率飙起来时你会先收到通知,而不是等看板。
原文链接:https://www.gj0.com/thread-1812.html
转载请注明出处并保留本声明;内容仅代表作者观点,与本站立场无关。若本文涉嫌侵权,请联系本站处理。