question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prometheus Flask exporter with __main__

See original GitHub issue

I want to use Prometheus Flask exporter with __main__.

This works fine by running env FLASK_APP=app.py flask run --port=80 --host=‘0.0.0.0’:

from flask import Flask
from prometheus_flask_exporter import PrometheusMetrics

app = Flask(__name__)
metrics = PrometheusMetrics(app)

app.debug = True

@app.route("/", methods=['GET'])
def index():
    return "hello world"

But I want to use my app in __main__, running python app.py.

from flask import Flask
from prometheus_flask_exporter import PrometheusMetrics

app = Flask(__name__)
metrics = PrometheusMetrics(app=None, path='/metrics')

app.debug = True

@app.route("/", methods=['GET'])
def index():
    return "hello world"

if __name__ == '__main__':
    metrics.init_app(app)
    app.run(host='0.0.0.0', port=80)

Here I get 400 on /metrics.

I got no clue how to init metrics correctly.

thx for helping klml

PS I asked this already stackoverflow.com, but got no answer.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
ampedandwiredcommented, Nov 4, 2019

Can confirm that the debug flag seems to break things. If I put the following in app.py and run with python app.py then I see the metrics page successfully at http://127.0.0.1:5000/metrics

from flask import Flask
from prometheus_flask_exporter import PrometheusMetrics
app = Flask(__name__)
metrics = PrometheusMetrics(app=app)
app.run(debug=False)

But change debug=False to debug=True and I get a 404. This is with prometheus-flask-exporter==0.11.0.

4reactions
klmlcommented, Nov 4, 2019

@rycus86 thx for your lightspeed responses;)

I tried DEBUG_METRICS and it suddenly worked. It even does not depend if DEBUG_METRICS is true or false. It only must be set. Vice versa, with no DEBUG_METRICS at all, I get “my” 404.

First I tried without DEBUG_METRICS and got a 404, but I got 200 be only setting DEBUG_METRICS.

root@ocpdevc001 [0] :/home/klml/python-ocp-demo
# echo $DEBUG_METRICS

root@ocpdevc001 [0] :/home/klml/python-ocp-demo
# python3 app.py 
 * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 780-306-174
10.12.69.235 - - [04/Nov/2019 10:13:23] "GET /metrics HTTP/1.1" 404 -


root@ocpdevc001 [0] :/home/klml/python-ocp-demo
# export DEBUG_METRICS=false
root@ocpdevc001 [0] :/home/klml/python-ocp-demo
# python3 app.py 
 * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 780-306-174
10.12.69.235 - - [04/Nov/2019 10:13:37] "GET /metrics HTTP/1.1" 200 -

So I got a workaround;)

Thx klml

Read more comments on GitHub >

github_iconTop Results From Across the Web

prometheus-flask-exporter - PyPI
This library provides HTTP request metrics to export into Prometheus. It can also track method invocations using convenient functions. Installing. Install using ...
Read more >
Monitoring Python Flask microservices with Prometheus
Learn about how easy it is to monitor your Python Flask applications with Prometheus using an exporter library!
Read more >
Implement Prometheus Metrics in a Flask Application
Learn to add Prometheus Metrics to an existing Python Application, step by step. Tagged with flask, python, prometheus, tutorial.
Read more >
Prometheus Flask exporter with __main__ - Stack Overflow
I want to use Prometheus Flask exporter with __main__ . But I want to use my app in __main__ , running python app.py...
Read more >
Prometheus Exporter for Flask Applications - Morioh
Prometheus Flask Exporter : Prometheus Exporter for Flask Applications. This library provides HTTP request metrics to export into Prometheus.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found