Instrumentation with Prometheus in Practice
To illustrate how Prometheus can be used for instrumentation, we will consider a simple Python application written in Flask (a lightweight Python micro-framework) that creates different counters to track the number of requests made to different endpoints.
The choice of Python here is for illustrative purposes. Python has been chosen because of its code readability and simplicity. Even if you are not familiar with Python, you should be able to read and understand the code. The same principles can be applied to other programming languages using their respective client libraries.
The application will be deployed to server1 (not on the monitoring server) and will expose two endpoints: / for the home page and /about for the about page.
Start by installing a development environment for our sample Python application on server1:
apt update
apt install python3-pip -y
pip3 install virtualenv --break-system-packages
Create a new Python virtual environment and activate it:
mkdir -p /prometheus-python-example
cd /prometheus-python-example
virtualenv venv
source venv/bin/activate
Install Flask, the Prometheus client library for Python, and other dependencies that we will use in our application:
pip install \
prometheus_client==0.20.0 \
flask==3.0.3 \
requests==2.31.0
Our code will expose the Prometheus metrics endpoint at /metrics on port 5000. Therefore, we can start by configuring the Prometheus server to scrape the metrics from there.
Go to the Prometheus server (monitoring) and start by exporting the IP address of server1 as an environment variable:
Observability with Prometheus and Grafana
A Complete Hands-On Guide to Operational Clarity in Cloud-Native SystemsEnroll now to unlock all content and receive all future updates for free.
