如果从 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
提前致谢。