【问题标题】:Collecting prometheus metrics from a separate port using flask and gunicorn with multiple workers使用烧瓶和 gunicorn 从一个单独的端口收集普罗米修斯指标和多个工人
【发布时间】:2018-09-19 02:41:53
【问题描述】:

我在 kubernetes 上运行一个带有 gunicorn 和多个工作进程的小型烧瓶应用程序。我想用普罗米修斯从这个应用程序收集指标,但指标应该只能在一个单独的端口上的集群内部访问(正如我们当前设置所要求的那样)。

对于一个 gunicorn 工作进程,我可以使用 python 客户端库中的 start_http_server 函数在与烧瓶应用程序不同的端口上公开指标。

一个最小的例子可能如下所示:

from flask import Flask
from prometheus_client import start_http_server, Counter

NUM_REQUESTS = Counter("num_requests", "Example counter")

app = Flask(__name__)

@app.route('/')
def hello_world():
    NUM_REQUESTS.inc()
    return 'Hello, World!'

start_http_server(9001)

要启动应用程序,请执行以下操作:

gunicorn --bind 127.0.0.1:8082 -w 1 app:app

但这仅适用于一个工作进程。

在客户端库的文档中还有一个section,关于如何通过将工作进程的共享目录指定为写入指标的环境变量来将 prometheus 和 gunicorn 与多个工作进程一起使用 (prometheus_multiproc_dir) .

因此,按照文档,上述多个工人的示例将是:

一个 gunicorn 配置文件:

from prometheus_client import multiprocess

def worker_exit(server, worker):    
    multiprocess.mark_process_dead(worker.pid)

申请文件:

import os
from flask import Flask
from prometheus_client import Counter

NUM_REQUESTS = Counter("num_requests", "Example counter")

app = Flask(__name__)

@app.route('/')
def hello_world():
    NUM_REQUESTS.inc()
    return "[PID {}]: Hello World".format(os.getpid())

要启动应用程序:

rm -rf flask-metrics/
mkdir flask-metrics
export prometheus_multiproc_dir=flask-metrics
gunicorn --bind 127.0.0.1:8082 -c gunicorn_conf.py -w 3 app:app

但是,在此设置中,我真的不知道如何访问存储在单独端口上的 flask-metrics 中的指标。有没有办法做到这一点?

我对这些事情有点陌生,所以如果我以错误的方式解决问题,我也很高兴得到建议,什么是解决我的案件的最佳方式。

【问题讨论】:

  • 嘿,你能解释一下你是如何解决这个问题的吗?

标签: python flask kubernetes gunicorn prometheus


【解决方案1】:

您在这里要做的是启动一个单独的进程来提供指标。将app 函数放在自己的应用程序中的https://github.com/prometheus/client_python#multiprocess-mode-gunicorn 中,并确保prometheus_multiproc_dir 对于它和主应用程序都是相同的。

【讨论】:

    【解决方案2】:

    我使用 Prometheus_flask_exporter 来做到这一点。

    我的 gunicorn 配置文件是这样的-

    from prometheus_flask_exporter.multiprocess import GunicornPrometheusMetrics
    
    hostname = "0.0.0.0"
    portname = 8080
    
    def when_ready(server):
        GunicornPrometheusMetrics.start_http_server_when_ready(8000)
    
    def child_exit(server, worker):
        GunicornPrometheusMetrics.mark_process_dead_on_child_exit(worker.pid)
    

    包含的wsgi文件-

    from prometheus_flask_exporter import PrometheusMetrics
    
    # an extension targeted at Gunicorn deployments for prometheus scraping in flask applications
    from prometheus_flask_exporter.multiprocess import GunicornPrometheusMetrics
    
    application = Flask(__name__, static_url_path='')
    CORS(application)
    
    health = HealthCheck(application, "/healthcheck")
    metrics = PrometheusMetrics(application)
    metrics = GunicornPrometheusMetrics(application)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      • 2020-11-12
      • 2019-04-17
      • 2020-07-21
      • 1970-01-01
      • 2022-12-22
      • 1970-01-01
      相关资源
      最近更新 更多