prometheus_flask_exporter
prometheus_flask_exporter copied to clipboard
Receiving 404 when trying to curl /metrics
I have a flask app running and I am attempting to utilize this exporter. Below is part of the code with addition of the exporter.
I am able to curl x.x.x.x:5004, but when i try x.x.x.x:5004/metrics, I am getting back a 404 and can't figure out why. Any help would be greatly appreciated.
from flask import Flask, request
from prometheus_flask_exporter import PrometheusMetrics
from celery import Celery
from celery_batches import Batches
from pulsar import Client, AuthenticationTLS
import signal, time, json, datetime, sys
## add pause to fix issue on reboots
time.sleep(30)
## Set time
now = datetime.datetime.now()
## Define flask app and enable prom metrics
app = Flask(__name__)
metrics = PrometheusMetrics(app)
## Set route to receive alerts and send to redis queue
@app.route('/', methods=['POST','GET'])
@metrics.counter('alert_counts', 'Number of alerts received by sevone')
def get_alert():
if request.method == 'GET':
return '<h1>Hello from Dev Webhook Listener!</h1>'
if request.method == 'POST':
alert = request.get_json()
alert_parse.delay(alert)
return "Sent alert to redis queue to be processed"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5004, threaded=True, debug=True)
signal.signal(signal.SIGINT, handler)
Did you try a minimal example without Flask.threaded param and without any non-related imports (celery, pulsar, other sys libs..)? Don't know about your infrastructure but better check if your routes / network / if applicable work as intended, since you seem to be using containers.
You can also use a static value for testing, to remove any possible issue with the / endpoint. Should be triggering right after app start, so no need to artifically curl endpoints: metrics.info('app_info', 'Application info', version='0.0.1')
Thank you for your response.
I tried all your suggestions, but I did not recognize any change. I did the minimal example by removing all the unrelated imports and set threading to false, but still received a 404. I also set the static value, but did not notice anything when restarting the app.
I don't have anything running as a container in this instance. I see the curl attempt hit the app as the 404 is in the flask app logs, it just doesn't recognize /metrics.
tried your code without the signal and alert_parse lines (see below) on mac with python 3.11.4, Flask 2.2.3, prometheus-flask-exporter 0.22.4:
➜ ~ curl 127.0.0.1:5004/
<h1>Hello from Dev Webhook Listener!</h1>%
➜ ~ curl 127.0.0.1:5004/metrics
# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 483.0
...
from flask import Flask, request
from prometheus_flask_exporter import PrometheusMetrics
import signal, time, json, datetime, sys
## add pause to fix issue on reboots
time.sleep(30)
## Set time
now = datetime.datetime.now()
## Define flask app and enable prom metrics
app = Flask(__name__)
metrics = PrometheusMetrics(app)
## Set route to receive alerts and send to redis queue
@app.route('/', methods=['POST','GET'])
@metrics.counter('alert_counts', 'Number of alerts received by sevone')
def get_alert():
if request.method == 'GET':
return '<h1>Hello from Dev Webhook Listener!</h1>'
if request.method == 'POST':
alert = request.get_json()
return "Sent alert to redis queue to be processed"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5004)
Okay, let me go ahead and upgrade python and flask and give it a shot again. Thank you!
So, in the end it looks like it doesn't like the debug flag below. I removed that and I am able to hit the /metrics endpoint now.
app.run(host='0.0.0.0', port=5004, threaded=True, debug=True)
Thanks for helping out here, everyone! ❤️ The debug mode has some known quirks, maybe there's more, see https://github.com/rycus86/prometheus_flask_exporter#debug-mode