Skip to content

agfianf/fastapi-RED-metrics

Repository files navigation

FastAPI RED Metrics

Read more explanation here

This project demonstrates how to implement RED (Rate, Errors, Duration) metrics in a FastAPI application using Prometheus and Grafana.

red-sample red-sample-2

⚡ How to Run

  1. Make sure you have Docker and Docker Compose installed.
  2. Clone this repository.
  3. Navigate to the project directory.
  4. Run make up
  5. Access the FastAPI application at API Documentation http://localhost:18123/docs
  6. Access Prometheus at http://localhost:9090
  7. Access Grafana at http://localhost:3000
    • Login (default credentials: admin/admin)
    • Go to Dashboard , select FastAPP RED Metrics Dashboard

How Run the Simulation

This project uses Locust for load testing (simulating real usage). When you run make simulate, Locust will:

  • Start 10 users (virtual clients)
  • Spawn rate of 1 user per second
  • Target the API at http://localhost:18123
  • Run in headless mode (without Web UI)

How to Implement to Your Own FastAPI Application

  1. Install Dependencies: Add the required dependencies to your requirements.txt
  2. copy metrics.py to your FastAPI application
  3. in main.py, import and include the metrics middleware
...

from <your-path>.metrics import METRICS_COLLECTOR, expose_metrics_endpoint

...

# Add metrics endpoint
METRICS_COLLECTOR.init_app(app)
app.add_api_route(
    "/metrics",
    expose_metrics_endpoint,
    methods=["GET"],
    include_in_schema=False,
)

...

🤔 What are RED Metrics?

RED metrics are a set of metrics that provide a high-level overview of your service's performance:

  • Rate: The number of requests per second.
  • Errors: The number of failed requests per second.
  • Duration: The amount of time each request takes.

Benefits of RED Metrics

  1. Simplicity: RED metrics provide a clear and concise view of your service's health.
  2. Universal applicability: These metrics are relevant to almost any service or microservice.
  3. Quick problem detection: Anomalies in any of these metrics can quickly point to issues in your service.
  4. Performance insights: They help in understanding the service's performance characteristics over time.
  5. Capacity planning: Rate metrics can be used to plan for scaling and capacity needs.

🗂️ Project Structure

  • fastapi-app/: Contains the FastAPI application code.
  • monitoring/: Contains configuration files for Prometheus and Grafana.
  • docker-compose.yml: Defines and runs the multi-container Docker application.

📈 Monitoring

flowchart LR
    A["**FastAPI /metrics Endpoint**<br>_Exposes RED metrics data_"]
    B["**Prometheus Scraper**<br>_Scrapes metrics at configured interval_"]
    C["**Time Series DB**<br>_Stores & compresses metric data_"]
    D["**PromQL Engine**<br>_Processes metric queries_"]
    E["**Grafana Datasource**<br>_Prometheus connection config_"]
    F["**Query Panel**<br>_Sends queries & manages refresh_"]
    G["**Dashboard View**<br>_Displays metrics visualization_"]
  
    subgraph Application
        direction TB
        A
    end
  
    subgraph Prometheus
        direction TB
        B --> C
        C --> D
    end
  
    subgraph Grafana
        direction TB
        E --> F
        F --> G
    end

    Application --> Prometheus
    Prometheus --> Grafana
Loading
  • Use Prometheus to scrape and store the metrics.
  • Use Grafana to visualize the metrics with the provided dashboard template.

Enjoy monitoring your FastAPI application with RED metrics!

References