Enable Prometheus metrics client in a Python Flask app using prometheus_flask_exporter and prometheus_client

Prometheus is a robust monitoring and alerting system that can collect and analyze metrics from various systems and applications. One way to integrate Prometheus with a Python Flask application is by using the prometheus_flask_exporter and prometheus_client libraries.

First, you will need to install the libraries by running the following command:

pip install prometheus_client prometheus_flask_exporter


Once the libraries are installed, you can import them into your Flask application and create an instance of the Prometheus exporter. In the following example, we will create a Flask application with a single endpoint that returns the current time.


from flask import Flask
from prometheus_flask_exporter import PrometheusMetrics

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

@app.route("/time")
def time():
    return str(datetime.now())

if __name__ == "__main__":
    app.run()


In the code above, we first import the Flask and PrometheusMetrics classes from the prometheus_flask_exporter library. Then we create an instance of the PrometheusMetrics class and pass it to the Flask app instance. This will automatically add the /metrics endpoint to the application, which will return the metrics collected by the exporter.

The next step is to create custom metrics for your application. Prometheus_client provides a wide range of metric types that can be used to collect different types of data. In this example, we will create a Counter metric that will track the number of requests to the /time endpoint.

from prometheus_client import Counter

REQUESTS = Counter("requests_total", "Total number of requests made")

@app.route("/time")
def time():
    REQUESTS.inc()
    return str(datetime.now())


We first import the Counter class from the prometheus_client library in the code above. Then we create a new Counter metric called REQUESTS and give it a name and a description. In the /time endpoint, we increment the counter by one every time the endpoint is called.

Finally, you can configure the Prometheus server to scrape the metrics endpoint of your application and start collecting and analyzing the metrics. This can be done by adding the endpoint to the Prometheus configuration file and restarting the server.

In conclusion, Prometheus is a robust monitoring and alerting system that can be integrated with a Python Flask application using the prometheus_flask_exporter and prometheus_client libraries. By following the steps outlined in this article, you can easily add Prometheus metrics to your Flask application and start collecting and analyzing data to gain valuable insights into the performance and behavior of your application.

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

Leave a Reply