【问题标题】:How to monitor Celery task completion with Prometheus如何使用 Prometheus 监控 Celery 任务完成情况
【发布时间】:2020-01-27 12:52:36
【问题描述】:

我正在尝试使用 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

你能解释一下我做错了什么吗?

【问题讨论】:

    标签: python celery prometheus


    【解决方案1】:

    默认情况下,Celery 使用 process pool 表示工人。这不适用于 Prometheus Python 客户端,您必须使用它的multiprocessing mode

    您可能更愿意使用采用不同方法的现有 Celery 导出器之一(例如 this one)。他们只是启动自己的进程并侦听来自工作人员的事件,从而克服了上述缺点。

    【讨论】:

      【解决方案2】:

      我尝试了现有的 Celery 导出器 (OvalMoney/celery-exporter),发现某些指标缺失或无效。

      我在这里写了一个导出器用于监控任务状态 - https://github.com/danihodovic/celery-exporter

      【讨论】:

        猜你喜欢
        • 2020-08-06
        • 1970-01-01
        • 1970-01-01
        • 2012-12-26
        • 2020-07-19
        • 2017-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多