如果从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
谢谢。
答案 0 :(得分:2)
考虑这样做:
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())
一旦我遇到了这样的问题,它就对我有用