prometheus-fastapi-metrics

Add Prometheus custom metrics to FastAPI applications with a /metrics endpoint.

Updated Apr 4, 2026
One-click install
npx skills add https://github.com/saintgo7/claude-skills --skill prometheus-fastapi-metrics
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: prometheus-fastapi-metrics
Source: https://github.com/saintgo7/claude-skills/tree/main/prometheus-fastapi-metrics
Command: npx skills add https://github.com/saintgo7/claude-skills --skill prometheus-fastapi-metrics

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes templates (resource) components.

What problem does it solve?

This Skill prevents blind monitoring and guesswork by showing how to add Prometheus custom metrics to a FastAPI service in a way that stays production-safe, especially around label cardinality and observability readiness.

Core Features & Use Cases

  • Production-ready /metrics endpoint: Exposes Prometheus scrape data via prometheus_client.generate_latest() for FastAPI.
  • 3-location metric recording: Implements a clear pattern for placing metrics in middleware (request lifecycle), routes (domain/model/token metrics), and services (quota/rejection outcomes).
  • Cardinality safety rules: Provides concrete guardrails to avoid Prometheus time-series explosions (path whitelisting, known enums for model/reason, grouping status codes, and banning high-cardinality identifiers).
  • Histogram bucket guidance: Recommends latency bucket selection aligned with expected SLO ranges (e.g., LLM gateway latencies in ms).
  • Grafana dashboard starter: Supplies a template dashboard driven by the metric schema for fast visualization and alert panel setup.

Quick Start

Use the skill to instrument your FastAPI app by following the provided 3-location pattern and applying the cardinality rules so you can immediately start scraping and building Grafana panels from the exported metrics.

Frequently Asked Questions about prometheus-fastapi-metrics

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add Prometheus metrics to a FastAPI application?

Add Prometheus metrics to FastAPI by defining Counters, Gauges, and Histograms then exposing a /metrics endpoint via prometheus_client.generate_latest(). Instrument middleware for request lifecycle, routes for domain metrics, and services for quota outcomes.

Why does Prometheus time-series cardinality explode when monitoring FastAPI?

Prometheus cardinality explodes when using high-cardinality labels like user_id, ip, or request_id. Prevent this by applying path whitelists, grouping status codes, and restricting model and reason labels to known enums.

How do I configure Histogram buckets for API latency monitoring in Grafana?

Configure Histogram buckets for API latency monitoring by selecting boundaries aligned with expected SLO ranges, such as millisecond latencies for LLM gateways, to ensure reliable percentile visualization in Grafana dashboards.

What is the best way to track LLM token throughput and quota rejections in FastAPI?

Track LLM token throughput and quota rejections in FastAPI by recording custom metrics within routes and service layers using Counters and Gauges, exposing the data for scraping to monitor failures and token usage.

Can I use a Grafana dashboard template with FastAPI Prometheus metrics?

Yes, you can use a Grafana dashboard template with FastAPI Prometheus metrics. A starter dashboard driven by the metric schema is provided for fast visualization and alert panel setup.

What are the limitations of using custom Prometheus metrics for FastAPI observability?

Limitations of custom Prometheus metrics for FastAPI observability include the risk of time-series explosions from unbounded labels and inaccurate percentile calculations if Histogram buckets are misaligned with actual latency distributions.