Oct 8, 2024
Prometheus Docker Compose Setup with Grafana
Prometheus collects valuable metrics from applications and stores them in a time-series database. Grafana can query the time-series data and create powerful visualizations.
This article will guide you through setting up Prometheus and Grafana using Docker Compose. We'll also configure Prometheus to collect metrics from a Python API and create a Grafana dashboard to visualize the data.
Table of Contents
Setting Up the Monitoring Stack
Exploring the FastAPI 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 FastAPI Application Metrics
Before we dive into Prometheus, let's take a look at the metrics our FastAPI 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 FastAPI 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.
To learn more about how FastAPI can be instrumented to expose Prometheus metrics, refer to this article: https:/kubernetestraining.io/blog/fastapi-prometheus-monitoring. It's worth noting that this approach is not unique to FastAPI. 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)
2. 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:
http_request_total
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.
3. 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.
Conclusion
In this guide, we've set up a monitoring stack using Docker Compose, consisting of:
A FastAPI application exposing metrics
Prometheus for collecting and storing these metrics
Grafana for visualizing the collected data
Subscribe to our mailing list for more Prometheus and DevOps tutorials.