Oct 8, 2024
Prometheus & Grafana: Docker Compose Monitoring Tutorial
This tutorial will guide you through running Prometheus and Grafana in a Docker Compose environment to monitor application metrics. You'll learn how to set up a monitoring stack where:
An application exposes metrics in a Prometheus-compatible format
Prometheus scrapes these metrics at regular intervals
Grafana creates visualizations by querying the Prometheus data
Types of Metrics
Counter Metrics: Values that only increase (e.g., total HTTP requests)
Gauge Metrics: Values that can go up or down (e.g., CPU usage, memory usage)
Histogram Metrics: Measurements grouped into buckets (e.g., most requests take 0-5ms, some take 5-10ms, and very few take longer)
How It Works
Applications are instrumented using libraries specific to their framework (Spring Boot, Flask, FastAPI, Node.js) to expose metrics at a designated endpoint (commonly /metrics
)
Prometheus is configured to discover and scrape these metrics endpoints at regular intervals (typically every 15 seconds)
The metric data is stored in Prometheus's time-series database
Grafana queries this data using PromQL (Prometheus Query Language) to create real-time visualizations and dashboards
With this foundation, let's proceed with setting up our monitoring stack…
Table of Contents
Setting Up the Monitoring Stack
Exploring the Application Metrics
Exploring Prometheus
Setting Up Grafana
Conclusion
1. Setting Up the Monitoring Stack
Start by cloning the repository
Run the Docker Compose command to start the stack:
This command will download the necessary Docker images and start the services defined in the docker-compose.yaml
file. Let's break down what's happening:
a) FastAPI Application:
This service starts a FastAPI application that's been instrumented with Prometheus metrics. The application is accessible at http://localhost:8000
. It exposes a /metrics
endpoint that Prometheus will scrape to collect performance data.
b) Prometheus:
Prometheus is started and configured to scrape metrics from our FastAPI application. The prometheus.yml
file in the repository is mounted into the container, providing the scraping configuration:
c) Grafana:
Grafana is started and will be available at http://localhost:3000
. You can log in with the username "admin" and the password "admin". Grafana is used to create dashboards and visualizations based on the metrics collected by Prometheus.
All these services are connected through a Docker network named monitoring
, allowing them to communicate with each other using their service names as hostnames.
Putting it all together:
After running docker-compose up
, you'll have a fully functional monitoring stack:
The FastAPI application running and exposing metrics.
Prometheus collecting these metrics at regular intervals.
Grafana ready to be configured to visualize the collected metrics.
Exploring the Application Metrics
Before we dive into Prometheus, let's take a look at the metrics our application is exposing. These are the raw metrics that Prometheus will scrape and store.
Open your web browser and navigate to
http://localhost:8000/metrics
You should see a page with text content that looks something like this:
This output displays various metrics being exposed by the containerized application. Prometheus will scrape these exposed metrics every 15 seconds, as configured in our prometheus.yml
, storing them in its time-series database for subsequent querying and analysis.
Side Note: Prior to being containerized, this Python application was instrumented to expose Prometheus metrics. All major web frameworks have Prometheus clients available to expose metrics in a format that Prometheus can understand and scrape. This includes:
Spring Boot (Java)
NestJS (Typescript)
Express (Node.js)
Flask (Python)
To learn more about how an application can be instrumented to expose Prometheus metrics, you may feel free to refer to the articles linked above.
3. Exploring Prometheus
Once the stack is running, you can access the Prometheus UI at http://localhost:9090
.
First, check if Prometheus is successfully scraping your FastAPI application:
Go to Status > Targets in the Prometheus UI. You should see fastapi-app
listed with the state "UP".
Now, let's try to query for a metric that's exposed by our FastAPI application:
You should see a result showing the total number of HTTP requests, broken down by method, path, and status code. If you see this data, it confirms that Prometheus is successfully scraping metrics from our FastAPI application and storing them in its database.
With this confirmation, we're ready to set up Grafana to query this data and create visualizations.
4. Setting Up Grafana
Access Grafana at http://localhost:3000
. The default login is admin/admin.
Setting up the Data Source
In order for Grafana to query the Prometheus data, we need to set up Prometheus as a data source:
Click on Settings (Gear Icon)
Go to Configuration > Data Sources.
Click "Add data source" and select Prometheus.
Set the URL to
http://prometheus:9090
.We use
prometheus:9090
instead oflocalhost:9090
because Grafana and Prometheus are on the same Docker network, andprometheus
resolves to the Prometheus container's IP.
Click "Save & Test" to ensure the connection is working.
Import a Dashboard
Go to Dashboards > Import and paste the JSON from grafana-dashboard.json
:
Each panel in the dashboard uses a PromQL query to visualize metrics from your FastAPI application.
Keep Going!
You've set up Prometheus and Grafana using Docker Compose. Ready to scale it up?
Next Lesson: Deploy the Prometheus Monitoring Stack in Kubernetes.
Kubernetes Training
If you found these guides helpful, check out The Complete Kubernetes Training course