Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gunicorn metrics don't work #165

Open
SyperAlexKomp opened this issue Dec 9, 2023 · 6 comments
Open

Gunicorn metrics don't work #165

SyperAlexKomp opened this issue Dec 9, 2023 · 6 comments

Comments

@SyperAlexKomp
Copy link

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
Copy link
Owner

rycus86 commented Dec 9, 2023

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
Copy link

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
Copy link
Owner

rycus86 commented Jul 2, 2024

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

@armingerten
Copy link

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

@rycus86
Copy link
Owner

rycus86 commented Jul 2, 2024

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
Copy link

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

Thanks for the quick help! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants