【问题标题】:Prometheus not capturing data for counterPrometheus 没有为计数器捕获数据
【发布时间】:2018-12-21 14:49:19
【问题描述】:

如果从 ipython 控制台或 celery 任务调用增量,Prometheus 无法为计数器捕获数据。计数器适用于 API 请求,但在 API 请求之外不起作用,就像我从 Ipython 控制台尝试它,从 celery 任务增加计数器一样。

Prometheus 也不会抛出任何异常。

# Using Sanic python framework (async) and python 3.6.
Here are the library Details:
sanic==0.7.0
ipython
prometheus-client==0.5.0
python 3.6.5

这是我在 Sanic Python 应用程序中用来配置 Prometheus 的配置:

class MetricsHandler(HTTPMethodView):
    """prometheus metrics exporter"""

    async def get(self, request, *args, **kwargs):
        registry = None
        if 'prometheus_multiproc_dir' in os.environ:
            registry = core.REGISTRY
        else:
            registry = CollectorRegistry()
            multiprocess.MultiProcessCollector(registry)
        data = generate_latest(registry)
        return raw(data, content_type=CONTENT_TYPE_LATEST)

# ipython console example:
In [1]: Prom_Count = Counter('Prom_Counter', 'prom counter', ['func', 'count'])
In [2]: Prom_Count.labels("test", "count").inc()

prometheus_multiproc_dir enviroment varibale is also set.

之前我们使用的是 sanic-prometheus 库:

prometheus-client==0.2.0
sanic-prometheus==0.1.4
wrapt==1.10.11

提前致谢。

【问题讨论】:

    标签: python-3.x monitoring prometheus sanic prometheus-node-exporter


    【解决方案1】:

    考虑这样做:

    def get_registry():
        if 'prometheus_multiproc_dir' in os.environ:
            registry = CollectorRegistry()
            multiprocess.MultiProcessCollector(registry)
        else:
            registry = REGISTRY
        return registry
    

    然后将注册表传递给Counter构造函数:

    Prom_Count = Counter('Prom_Counter', 'prom counter', ['func', 'count'], registry=get_registry())
    

    一旦我遇到了这样的问题并且它对我有用

    【讨论】:

    • 它在 ipython 控制台中有效,但在 celery 任务中无效。
    • 我在 celery_tasks 中遇到的问题是在 celery 实例中运行的服务器没有为 /metrics API 调用提供响应。如果您可以通过 collect() 调用查看计数,并且 /metrics API 调用中没有数据,则很可能是问题所在。
    • @Arjunsingh 你有没有想过这个问题?我可以获得与 Celery 一起提供的指标端点,但我的自定义计数器永远不会更新。
    • @dacox 我不记得确切的问题,但我记得它有一些注册或文件路径问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 2020-06-02
    • 2014-12-03
    • 1970-01-01
    相关资源
    最近更新 更多