To monitor a FastAPI application with Prometheus and Grafana, you typically follow a three-part architecture: the FastAPI App (generates metrics), Prometheus (scrapes and stores metrics), and Grafana (visualizes metrics).
The most efficient way to do this is using the prometheus-fastapi-instrumentator library, which automates the collection of standard metrics like request count, latency, and error rates.
Step 1: Instrument your FastAPI App
First, install the necessary library:
pip install prometheus-fastapi-instrumentator
Then, add these four lines to your main.py to expose a /metrics endpoint:
from fastapi import FastAPI
from prometheus_fastapi_instrumentator import Instrumentator
app = FastAPI()
# This exposes /metrics and starts tracking request data
Instrumentator().instrument(app).expose(app)
@app.get("/")
async def root():
return {"message": "Monitoring is active!"}
Step 2: Configure Prometheus
Prometheus needs to know where to look for your app's data. Create a prometheus.yml file:
scrape_configs:
- job_name: 'fastapi-app'
scrape_interval: 5s
static_configs:
- targets: ['host.docker.internal:8000'] # Point this to your FastAPI server
Step 3: Run the Stack with Docker Compose
The easiest way to run everything together is using a docker-compose.yml file:
services:
app:
build: .
ports:
- "8000:8000"
prometheus:
image: prom/prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
grafana:
image: grafana/grafana
ports:
- [span_5](start_span)"3000:3000"[span_5](end_span)
Step 4: Visualize in Grafana
* Open Grafana at http://localhost:3000 (Default login: admin / admin).
* Go to Connections > Data Sources and add Prometheus. Use http://prometheus:9090 as the URL.
* Go to Dashboards > New > Import.
* Enter the ID 16110 (a popular community dashboard for FastAPI).
This dashboard will automatically populate with graphs showing your API's requests per second, p99 latency, and success rates.
Summary of Monitoring Components
| Component | Responsibility | Port |
|---|---|---|
| FastAPI | Generates raw metric data via /metrics. | 8000 |
| Prometheus | Pulls (scrapes) and stores time-series data. | 9090 |
| Grafana | Queries Prometheus to create visual dashboards. | 3000 |
Would you like me to show you how to create custom metrics (like tracking how many times a specific button is clicked) instead of just default request data?
Prometheus Metrics for your Python FastAPI App
This video provides a step-by-step walkthrough of setting up Prometheus metrics specifically for FastAPI using the instrumentator library.
YouTube video views will be stored in your YouTube History, and your data will be stored and used by YouTube according to its Terms of Service
No comments:
Post a Comment