将普罗米修斯与Connexion一起使用-ValueError:收集器注册表中的时间序列重复



当使用python3.6/3.7将prometheus与connexion一起使用时,我收到以下错误消息:

ValueError:收集器注册表中重复的时间序列:{'app_request_processing_secconds_sum'、'app_rerequest_processing_seconds_count'、'app/request_proccessing_secconds-created'、'app.request_processing_secconds'}

#!/usr/bin/env python3
from gevent import monkey  # noqa
# monkey.patch_all()  # noqa
import json
import os
import connexion
import datetime
import logging
from connexion import NoContent
from prometheus_client import Summary, Counter
logger = logging.getLogger(__name__)
REQUEST_TIME = Summary('app_request_processing_seconds', 'time spent processing')
REQUEST_COUNTER = Counter('app_request_count', 'number of requests')
@REQUEST_TIME.time()
def get_health():
try:
'Hello'
except Exception:
return connexion.problem(503, "Service Unavailable", "Unhealthy")
else:
return "Healthy"

logging.basicConfig(level=logging.INFO)
app = connexion.App(__name__)
app.add_api("swagger.yaml")
if __name__ == "__main__":
# run our standalone gevent server
app.run(port=8080, server="gevent")

有一个swagger.yaml与以下内容相同:https://github.com/hjacobs/connexion-example-redis-kubernetes/blob/master/swagger.yaml

任何帮助都将是伟大的

作为猜测,您已将文件命名为app.py。如果装载型锻时,处理被指定为app.get_health:,会发生什么情况

paths:
/health:
get:
operationId: app.get_health

并且它(第二次)加载app.py来导入get_health()函数。

原因是主文件被加载为__main__模块,从而被第二次加载;有关更多信息,请参阅其他问题。因此,您最终需要定义两次普罗米修斯度量,这对收集器来说并不合适。

最简单的解决方案是重命名文件,并在另一个名为app.py的文件中实现应用程序。

最新更新