rycus86 / prometheus_flask_exporter

Prometheus exporter for Flask applications
https://pypi.python.org/pypi/prometheus-flask-exporter
MIT License
633 stars 162 forks source link

Gunicorn metrics don't work #165

Open SyperAlexKomp opened 9 months ago

SyperAlexKomp commented 9 months ago

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?

rycus86 commented 9 months ago

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

armingerten commented 2 months ago

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.

rycus86 commented 2 months ago

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

armingerten commented 2 months ago

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:

rycus86 commented 2 months ago

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!

armingerten commented 2 months ago

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

Thanks for the quick help! 👍