如何使用 Prometheus 监控 Celery 任务完成情况
How to monitor Celery task completion with Prometheus
我正在尝试使用 Prometheus 来监控 Celery 任务,我对此比较陌生,但我在增加计数器时遇到了问题。如果我试图在 Celery.task
内进行,它不会递增
例如
from celery import Celery
from prometheus_client import Counter
app = Celery('tasks', broker='redis://localhost')
TASKS = Counter('tasks', 'Count of tasks')
@app.task
def add(x, y):
TASKS.inc(1)
return x + y
当我访问端点以查看公开了哪些指标时,我可以看到 tasks_total
,但无论执行了多少 add
任务,它的值都不会改变。
但是,当我尝试从常规函数递增相同的计数器时,它起作用了。
例如
def dummy_add(x, y):
TASKS.inc()
return x + y
你能解释一下我做错了什么吗?
默认情况下,Celery 使用 process pool for workers. This doesn't go well with Prometheus Python client and you have to use its multiprocessing mode。
您可能更愿意使用采用不同方法的现有 Celery 导出器之一(例如 this one)。他们只是启动自己的进程并监听来自工作人员的事件,从而克服了上述缺点。
我尝试了现有的 Celery 导出器 (OvalMoney/celery-exporter),发现一些指标缺失或无效。
我在这里写了一个用于监控任务状态的导出器 - https://github.com/danihodovic/celery-exporter。
我正在尝试使用 Prometheus 来监控 Celery 任务,我对此比较陌生,但我在增加计数器时遇到了问题。如果我试图在 Celery.task
例如
from celery import Celery
from prometheus_client import Counter
app = Celery('tasks', broker='redis://localhost')
TASKS = Counter('tasks', 'Count of tasks')
@app.task
def add(x, y):
TASKS.inc(1)
return x + y
当我访问端点以查看公开了哪些指标时,我可以看到 tasks_total
,但无论执行了多少 add
任务,它的值都不会改变。
但是,当我尝试从常规函数递增相同的计数器时,它起作用了。
例如
def dummy_add(x, y):
TASKS.inc()
return x + y
你能解释一下我做错了什么吗?
默认情况下,Celery 使用 process pool for workers. This doesn't go well with Prometheus Python client and you have to use its multiprocessing mode。
您可能更愿意使用采用不同方法的现有 Celery 导出器之一(例如 this one)。他们只是启动自己的进程并监听来自工作人员的事件,从而克服了上述缺点。
我尝试了现有的 Celery 导出器 (OvalMoney/celery-exporter),发现一些指标缺失或无效。
我在这里写了一个用于监控任务状态的导出器 - https://github.com/danihodovic/celery-exporter。