1

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

ValueError:CollectorRegistry 中的重复时间序列:{'app_request_processing_seconds_sum'、'app_request_processing_seconds_count'、'app_request_processing_seconds_created'、'app_request_processing_seconds'}

#!/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

任何帮助都会很棒

4

1 回答 1

1

作为猜测,您已将文件命名为app.py. 加载 swagger 时会发生什么情况,处理被指定为app.get_health

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

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

主文件作为__main__模块加载并因此第二次加载的原因;有关更多信息,请参阅其他问题。因此,您最终会两次定义 Prometheus 指标,这与收集器不相符。

最简单的解决方案是重命名您的文件并在另一个名为app.py.

于 2019-09-18T07:01:58.703 回答