Building a Custom Prometheus Exporter in Python

Today I felt like learning something new, so let's get into building custom Prometheus exporters in python!

To preface a few terms.
Prometheus: Prometheus is a metrics collection and aggregation platform. It is really popular in a cloud-native environment.
Exporter: Exporters are software that collects non-Prometheus native metrics and exposes them as Prometheus scrapable config.
Gauge: Gauges in Prometheus are a kind of metric where it exposes the current statistic of an application.
Counter: Counters in Prometheus are a kind of metric that exposes the cumulative metric of an application. Things like the total amount of HTTP requests that happened on this application.

Now let's get into it!

First, what would it take to build a Prometheus exporter anyways? kindly, Prometheus has a python client library that allows you to simply create a class to scrape some metrics. It would look something like this:

This implements a metric collector that collects a random number as a gauge and has a summation of random numbers as a counter metric. you can also populate it with things like making an HTTP request and sending the results as the metric. Another thing to notice here is this collector actually implements a generator via yields. So you can’t simply return values. Or at least it won’t be exposed if you return them.

Now, let's start this server up! To do so, simply append this to the end of collector.py

This hardcodes some values like the listener port and scrape speed, but it should be able to provide a working Prometheus exporter. to start it, run python3 collector.py. Now go to http://localhost:9000, and see what happens!

output of prometheus

You should see something like this! A super cool thing here is it also exports default python metrics like garbage collector performance. That is always fun!

Now, in the name of cloud-native, let's make the exporter more modular. Switch your main function to this:

This reads the configuration from a config.yml file, so now we can configure the exporter with the config.yml file.

Now, to deploy this, let's make a Dockerfile to package it!

  • hint: Don’t forget to create requirements.txt for managing dependencies of this application, including prometheus_client package*

Now we can push this to production, be it Kubernetes or whatever other platform you like to run production! Thanks for reading the article and talk to you next time!

Resources: feel free to check out my GitHub repository of this from start to complete!

9xAWS Certified | Terraform Associate | Azure Certified | VMA | Cloud+