最完整RQ任务监控告警指南:3步实现Alertmanager告警路由配置
【免费下载链接】rq Simple job queues for Python 项目地址: https://gitcode.com/gh_mirrors/rq1/rq
你还在手动检查RQ任务队列状态?服务器过载时收不到通知?本文将带你通过Prometheus+Alertmanager构建企业级RQ监控告警系统,实时追踪任务失败、队列积压和Worker异常,让运维效率提升10倍!
读完本文你将掌握:
- RQ指标采集的3种实现方式
- 15个关键监控指标定义与告警阈值
- Alertmanager多渠道告警路由配置
- 完整监控架构的Docker化部署方案
RQ监控体系架构
RQ作为轻量级Python任务队列,本身提供基础监控能力,但缺乏完善的告警机制。通过整合Prometheus和Alertmanager,我们可以构建完整的监控闭环:
核心组件说明
- 指标采集层:通过rq-exporter或自定义脚本提取RQ关键指标
- 存储分析层:Prometheus存储时序数据并执行告警规则
- 告警分发层:Alertmanager处理告警路由、抑制和静默
RQ指标采集实现
方案1:使用官方rq-dashboard
RQ官方提供的RQ dashboard是最简单的监控方式,支持Web界面查看队列状态:
安装命令:
$ pip install rq-dashboard
$ rq-dashboard --port 9181
通过访问http://localhost:9181可查看实时队列状态,但需注意该工具不直接支持Prometheus指标导出。
方案2:自定义Prometheus Exporter
创建rq_exporter.py实现指标暴露:
from prometheus_client import start_http_server, Gauge
from rq import Queue
from rq.connections import get_connection
# 定义指标
RQ_QUEUE_JOBS = Gauge('rq_queue_jobs_total', 'Total jobs in RQ queue', ['queue'])
RQ_WORKERS = Gauge('rq_workers_total', 'Total active RQ workers')
def collect_metrics():
conn = get_connection()
# 收集队列指标
for queue in ['high', 'default', 'low']:
q = Queue(queue, connection=conn)
RQ_QUEUE_JOBS.labels(queue=queue).set(q.count)
# 收集Worker指标
workers = Worker.all(connection=conn)
RQ_WORKERS.set(len(workers))
if __name__ == '__main__':
start_http_server(9237)
while True:
collect_metrics()
time.sleep(10)
关键指标定义参考rq/queue.py和rq/worker.py中的核心方法。
方案3:使用rq-prometheus库
社区维护的rq-prometheus库提供开箱即用的指标导出:
$ pip install rq-prometheus
$ rq-prometheus --redis-url redis://localhost:6379/0 --port 9237
该方案自动暴露以下指标类型:
rq_jobs_total:按状态(pending/started/completed/failed)统计的任务数rq_workers_total:活跃Worker数量rq_queue_latency_seconds:队列平均延迟时间
关键监控指标与告警阈值
必须监控的15个RQ指标
| 指标名称 | 类型 | 告警阈值 | 说明 |
|---|---|---|---|
| rq_jobs_failed_total | Counter | 5分钟内>10 | 失败任务数,参考rq/registry.py |
| rq_queue_pending_jobs | Gauge | >1000 | 队列积压任务,对应Queue.count方法 |
| rq_worker_idle_seconds | Gauge | >300 | Worker空闲时间,需结合业务调整 |
| rq_job_execution_time_seconds | Histogram | P95>10 | 任务执行耗时,参考rq/job.py |
| redis_memory_used_bytes | Gauge | >80%内存 | Redis内存使用率,影响队列稳定性 |
Prometheus告警规则配置
创建rq_alerts.yml:
groups:
- name: rq_alerts
rules:
- alert: RQJobFailureRate
expr: increase(rq_jobs_failed_total[5m]) > 10
for: 2m
labels:
severity: critical
annotations:
summary: "RQ任务失败率过高"
description: "5分钟内失败任务数{{ $value }},超过阈值10"
- alert: RQQueueBacklog
expr: rq_queue_pending_jobs > 1000
for: 5m
labels:
severity: warning
annotations:
summary: "RQ队列积压"
description: "{{ $labels.queue }}队列积压任务{{ $value }}个"
- alert: RQWorkerDown
expr: absent(rq_workers_total) or rq_workers_total < 1
for: 1m
labels:
severity: critical
annotations:
summary: "RQ Worker全部离线"
description: "当前活跃Worker数量为{{ $value }}"
Alertmanager告警路由配置
基础配置文件(alertmanager.yml)
global:
resolve_timeout: 5m
smtp_smarthost: 'smtp.example.com:587'
smtp_from: 'alerts@example.com'
smtp_auth_username: 'alerts@example.com'
smtp_auth_password: 'your-password'
smtp_require_tls: true
route:
group_by: ['alertname', 'queue']
group_wait: 10s
group_interval: 1m
repeat_interval: 4h
# 优先路由严重告警
routes:
- match:
severity: critical
receiver: 'pagerduty'
continue: true
- match_re:
severity: warning|info
receiver: 'email'
receivers:
- name: 'email'
email_configs:
- to: 'dev-team@example.com'
send_resolved: true
- name: 'pagerduty'
pagerduty_configs:
- service_key: 'your-pagerduty-service-key'
send_resolved: true
告警抑制规则
避免告警风暴的关键配置:
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['queue', 'env']
当某个队列触发严重告警时,抑制该队列的警告级别告警。
完整部署方案
Docker Compose配置
创建docker-compose.yml整合所有组件:
version: '3'
services:
redis:
image: redis:6-alpine
ports:
- "6379:6379"
rq-worker:
build: .
command: rq worker high default low --with-scheduler
depends_on:
- redis
environment:
- REDIS_URL=redis://redis:6379/0
rq-exporter:
build: .
command: python rq_exporter.py
ports:
- "9237:9237"
depends_on:
- redis
prometheus:
image: prom/prometheus:v2.30.3
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
- ./rq_alerts.yml:/etc/prometheus/rules/rq_alerts.yml
ports:
- "9090:9090"
alertmanager:
image: prom/alertmanager:v0.23.0
volumes:
- ./alertmanager.yml:/etc/alertmanager/alertmanager.yml
ports:
- "9093:9093"
部署验证
- 启动所有服务:
$ docker-compose up -d
-
检查Prometheus targets: 访问
http://localhost:9090/targets确认rq-exporter状态正常 -
测试告警触发: 手动创建大量失败任务,验证Alertmanager是否按预期发送通知
高级监控技巧
与Sentry集成处理任务异常
虽然本文重点在Prometheus告警,但RQ也支持与Sentry集成捕获任务异常:
import sentry_sdk
from sentry_sdk.integrations.rq import RqIntegration
sentry_sdk.init(
dsn="your-sentry-dsn",
integrations=[RqIntegration()],
)
使用rq info命令行工具
RQ提供的rq info命令可快速查看队列状态:
$ rq info --interval 5
high |██████████████████████████ 20
low |██████████████ 12
default |█████████ 8
3 queues, 45 jobs total
Bricktop.19233 idle: low
Bricktop.19232 idle: high, default, low
Bricktop.18349 idle: default
3 workers, 3 queues
添加--by-queue参数按队列分组显示Worker:
$ rq info -R
high: Bricktop.25458 (busy), Mickey.26421 (idle)
low: Bricktop.25458 (busy)
default: Bricktop.25458 (busy), Mickey.26421 (idle)
总结与最佳实践
- 指标采集:生产环境推荐使用rq-prometheus+Prometheus组合
- 告警分级:按业务重要性设置不同告警级别和通知渠道
- 阈值调优:根据任务特性调整告警阈值,避免告警疲劳
- 监控覆盖:除队列指标外,需同时监控Redis性能和Worker资源使用
- 定期演练:每月进行告警触发测试,确保通知渠道畅通
通过本文方案,你已拥有企业级RQ监控告警系统。完整配置文件可参考项目examples/目录下的监控示例。
【免费下载链接】rq Simple job queues for Python 项目地址: https://gitcode.com/gh_mirrors/rq1/rq
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考




