Writing metrics exporter for Prometheus using Python

In the previous articles, we have talked about monitoring using Prometheus and other ways. In this article, we are going to talk about how you can write your own exporter using Python.

To write your own exporter you need to use prometheus_client library. To install it you can type the below command.

pip install prometheus_client

Now let’s look at the code where we will export the metrics and then our Prometheus can scrape those metrics.

from prometheus_client import start_http_server, Summary
import random
import time

REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')

@REQUEST_TIME.time()
def process_request(t):
    time.sleep(t)

if __name__ == '__main__':
    start_http_server(9000)
    while True:
        process_request(random.random())

In the above code what we have done is imported prometheus_client and started the http_server which will serve the metrics endpoint. We have created a Request_Time function with help of summary, this function is acting as a decorator and then can be added to any function to check how much time it took to execute that part of code.

Then in our process_request function, we added a random sleep to simulate the time taken to execute the function. In main, we just started the server and then in a loop called the process_request function with random values. This will trigger the Request_Time function and the metrics get recorded. If you open localhost:9000/metrics you will see something like below.

Now you can add this endpoint in Prometheus to start scraping.

  - job_name: python
    static_configs:
    - targets: ['localhost:9000']

Now you Prometheus will start scrapping the metrics. You can now use Grafana to plot the metrics.

This was how you can write a very basic Prometheus exporter and then you to plot on Grafana.

If you like the article please share and subscribe.


Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Are you preparing for DevOps and SRE Interview?

Here is a book for you.


Interview preparation and interview questions for DevOps and SRE






A good reads for last-minute preparations for handling DevOps and SRE interviews at companies like LinkedIn, Visa, Atlassian, etc.