prometheus_flask_exporter icon indicating copy to clipboard operation
prometheus_flask_exporter copied to clipboard

Gunicorn metrics don't work

Open SyperAI opened this issue 2 years ago • 6 comments

I've done all the same as in the README.MD, but when I use one of GunicornInternalPrometheusMetrics or GunicornPrometheusMetrics - /metrics request returns empty response. I've tried everything. I'm starting gunicorn by systemctl. Also when I'm using PrometheusMetrics all is working fine. Maybe it's because systemctl?

SyperAI avatar Dec 09 '23 16:12 SyperAI

If you have a repo to look at, or a (minimal?) example of the setup, we could try working out what's going wrong.

rycus86 avatar Dec 09 '23 20:12 rycus86

I am having the same issue. Here's a minimal example that I used to reproduce this:

import os
from flask import Flask
from gunicorn.app.base import BaseApplication

from prometheus_flask_exporter.multiprocess import GunicornInternalPrometheusMetrics


class StandaloneApplication(BaseApplication):

    def __init__(self):
        os.environ['PROMETHEUS_MULTIPROC_DIR'] = '/tmp'

        self.application = Flask('my_app')
        self.application.add_url_rule('/test', 'test', self.index)

        self.metrics = GunicornInternalPrometheusMetrics(self.application)

        super().__init__()

    def load_config(self) -> None:
        pass

    def load(self):
        return self.application

    def index(self):
        return 'Hello world'


StandaloneApplication().run()

Accessing the application via http://localhost:8000/test works fine, but http://localhost:8000/metrics yields an empty page.

armingerten avatar Jul 02 '24 09:07 armingerten

I'm curious, what is the status code on the /metrics request? Is it 404 or 200 or something else?

rycus86 avatar Jul 02 '24 09:07 rycus86

It's 200:

curl -v http://127.0.0.1:8000/metrics
*   Trying 127.0.0.1:8000...
* Connected to 127.0.0.1 (127.0.0.1) port 8000
> GET /metrics HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/8.6.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: gunicorn
< Date: Tue, 02 Jul 2024 10:42:57 GMT
< Connection: close
< Content-Type: text/plain; version=0.0.4; charset=utf-8
< Content-Length: 0
< 
* Closing connection

I am using:

  • gunicorn: 22.0.0
  • flask: 3.0.3
  • prometheus-flask-exporter: 0.23.0

armingerten avatar Jul 02 '24 10:07 armingerten

I had a look now, it looks like the problem is that the PROMETHEUS_MULTIPROC_DIR configuration happens too late. If you run your app with PROMETHEUS_MULTIPROC_DIR=/tmp python3 app.py then things work as expected. If you move the os.environ['PROMETHEUS_MULTIPROC_DIR'] = '/tmp' line right after the import os line to line 2, then it also works, but that's less recommended.

Hope this helps!

rycus86 avatar Jul 02 '24 23:07 rycus86

Indeed, when setting the environment variable before importing the prometheus_flask_exporter, it is working as expected.

Thanks for the quick help! 👍

armingerten avatar Jul 03 '24 10:07 armingerten