3

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

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

提前致谢。

4

1 回答 1

3

考虑这样做:

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())

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

于 2018-12-21T14:54:14.407 回答