Skip to content

Instantly share code, notes, and snippets.

@rchakode

rchakode/prometheus_exporter_flask.py Secret

Last active Sep 21, 2019
Embed
What would you like to do?
Example Prometheus exporter that exports metrics of a Flask application
import prometheus_client
import werkzeug.wsgi
import flask
import random
import time
import threading
SERVICE_UPTIME = prometheus_client.Gauge('service_uptime',
'Hold the time elasted since service startup')
RESPONSE_TIME = prometheus_client.Gauge('response_time_last',
'Hold the last request response time')
# Create Flask app
app = flask.Flask(__name__)
@app.route('/')
@RESPONSE_TIME.time()
def hello():
time.sleep(random.random())
return "Hello World!"
# Prometheus wsgi middleware to route /metrics requests
app_dispatch = werkzeug.wsgi.DispatcherMiddleware(app, {
'/metrics': prometheus_client.make_wsgi_app()
})
def update_uptime():
while True:
SERVICE_UPTIME.inc(1)
time.sleep(1)
SERVICE_UPTIME.set(0)
uptime_updater = threading.Thread(target=update_uptime)
uptime_updater.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.