Create graphs in Grafana
Collect and graph metrics with Python
PROMETHEUS
We delve into exposing Prometheus metrics with some capable Python 3 scripts and the help of Mihalis Tsoukalos.
The code
Get it from linuxform at. com/archives and on the DVD
OUR EXPERT
Mihalis Tsoukalos
is a systems engineer and technical writer. He’s also the author of Go Systems Programming and Mastering Go. Contact him on @mactsouk.
QUICK TIP
Prometheus pulls data from its data sources, which means it needs to know where to look for this data.
Additionally, each data source (including all presented Python applications) should wait for Prometheus to read its data using an HTTP connection.
W e collect metrics because we want to explore and understand a situation. This is usually because there’s a need to improve the performance of a system and eliminate bottlenecks.
Therefore, when collecting metrics, always bear in mind the problem that needs solving or the system that we want to understand better, else the amount of metrics you could collect would become overwhelming! It’s a good idea to have a scenario in mind and then try to collect the appropriate metrics.
This tutorial will explain how to use Python 3 to collect metrics and enable Prometheus to pull and store them, using practical scenarios that include Linux System metrics, and metrics from the timing of database servers.
Prometheus has basic visualisation capabilities that are only useful for debugging purposes. As a result, there’s no point in keeping metrics in Prometheus or any other data storage if you’re not going to visualise them in another piece of software. In this tutorial we’ll visualise the metrics in Grafana, but feel free to use any visualisation software you’re comfortable with.
Docker-compose
For the purposes of this tutorial, we are going to use a docker-compose.yml file to run and use Prometheus and Grafana. Later on, we’re going to add more Docker images, which are going to run Python scripts and Database servers. The screenshot (above) shows the initial version docker-compose.yml – this version is for running Prometheus and Grafana only because these are the essential parts of the process. The prometheus directory is where the Prometheus configuration file (prometheus.yml) is stored.
Based on this docker-compose.yml, all data is kept locally and isn’t going to be lost after you stop the Docker containers. The prometheus_data and grafana_ data directories are used for storing the data of Prometheus and Grafana. Initially, they’ll be empty but will be populated automatically once the dockercompose.yml file is executed.
Run docker-compose up from the directory where docker-compose.yml is located in order to use it. You should use docker-compose down for stopping all Docker containers included in docker-compose.yml.