ValueError: Duplicated timeseries in CollectorRegistry  

means that at leat two metrics were added with same metric name.
You should add the metric to the registry only one time.

In my case, I declared the metric in the util directory. (like util/prometheus/metrics.py)
And then import it ( from util.prometheus import metrics) and set the label and values in every api.

check the below code.

# src/prometheus/metrics.py
from prometheus_client import  Gauge 


metric_name = "metrics_info"
metric_description = "metric test"
metric_labels = ["status_code","os","handler"]
metric_gauge = Gauge(
    name=metric_name,
    documentation = metric_description,
    labelnames = metric_labels
) 
# src/api/test.py
from ..prometheus import metrics

@router.get("/metrictest")
def test():
... do sth 
metrics.metric_gauge.labels(status_code="400",os="someos",handler="/api/test/metrictest").set(1)        
... do sth else

Answer from JAESANGPARK on Stack Overflow
🌐
Medium
medium.com › @hitorunajp › prometheus-on-a-fastapi-application-aa25e5223a9e
Prometheus on a FastAPI application | by Hitoruna | Medium
September 1, 2025 - Here MetricsMiddleware is a custom middleware class that intercepts every HTTP request to our app, records Prometheus metrics, then passes the request on. Its core functionality is in the dispatch function. For every HTTP request, it forwards the request so that FastAPI can process it normally, then increments the corresponding counter.
🌐
Readthedocs
aioprometheus.readthedocs.io › en › latest › user › index.html
User Guide — aioprometheus Documentation
In this example a counter metric is instantiated and gets updated whenever the "/" route is accessed. A '/metrics' route is implemented using the render function and added to the application using the standard web framework method. The metrics route renders Prometheus metrics from the default ...
Discussions

Prometheus Middleware is out!
This is great work! Thanks for sharing Will try it out soon More on reddit.com
🌐 r/FastAPI
10
14
October 12, 2020
FastAPI and Prometheus endpoint
You won't be able to start 2 servers/processes listening on the same port. First one to start, wins. The other will fail to bind as the port is already in use. What you could do though is start both on different ports, say 8080 and 8081 or something, then put nginx in front of it all and proxy through to which ever backend based on the requested path. As a bonus you can terminate TLS at the nginx level using a single cert etc etc too. More on reddit.com
🌐 r/FastAPI
5
1
December 16, 2022
ASGI mounted to FastAPI mounts metrics to `/metrics/` instead of `/metrics`
The documentation gives an example application which mounts the ASGI app to /metrics in a FastAPI app · from fastapi import FastAPI from prometheus_client import make_asgi_app # Create app app = FastAPI(debug=False) # Add prometheus asgi middleware to route /metrics requests metrics_app = ... More on github.com
🌐 github.com
8
March 15, 2024
How do you monitor your FastAPI apps?
I usually use Sentry for error tracking More on reddit.com
🌐 r/FastAPI
18
27
February 20, 2024
Top answer
1 of 2
1
ValueError: Duplicated timeseries in CollectorRegistry  

means that at leat two metrics were added with same metric name.
You should add the metric to the registry only one time.

In my case, I declared the metric in the util directory. (like util/prometheus/metrics.py)
And then import it ( from util.prometheus import metrics) and set the label and values in every api.

check the below code.

# src/prometheus/metrics.py
from prometheus_client import  Gauge 


metric_name = "metrics_info"
metric_description = "metric test"
metric_labels = ["status_code","os","handler"]
metric_gauge = Gauge(
    name=metric_name,
    documentation = metric_description,
    labelnames = metric_labels
) 
# src/api/test.py
from ..prometheus import metrics

@router.get("/metrictest")
def test():
... do sth 
metrics.metric_gauge.labels(status_code="400",os="someos",handler="/api/test/metrictest").set(1)        
... do sth else

2 of 2
-2

Instead of setting up your own monitoring stack with Prometheus, which can be a bit fiddly, you could use a tool like Apitally to track API metrics, such as number of requests, error rates, response times etc.

Apitally comes with a middleware for FastAPI, which captures request and response metadata, and provides a simple dashboard with insights for the whole API and individual endpoints/routes.

There is a specific setup guide for FastAPI that you can follow. The basic steps are:

  1. Create an app in the Apitally dashboard to get a client ID.
  2. Install the client library as a dependency in your project:
pip install "apitally[fastapi]"
  1. Add the middleware to your FastAPI app:
from fastapi import FastAPI
from apitally.fastapi import ApitallyMiddleware

app = FastAPI()
app.add_middleware(
    ApitallyMiddleware,
    client_id="your-client-id",
    env="dev",  # or "prod" etc.
)

Disclaimer: I'm the author of Apitally.

🌐
PyPI
pypi.org › project › prometheus-fastapi-instrumentator
prometheus-fastapi-instrumentator · PyPI
It also features a modular approach to metrics that should instrument all FastAPI endpoints. You can either choose from a set of already existing metrics or create your own. And every metric function by itself can be configured as well. This chapter contains an example on the advanced usage of the Prometheus FastAPI Instrumentator to showcase most of it's features.
🌐
client_python
prometheus.github.io › client_python › exporting › http › fastapi-gunicorn
FastAPI + Gunicorn | client_python
April 15, 2024 - To use Prometheus with FastAPI and Gunicorn we need to serve metrics through a Prometheus ASGI application. Save the snippet below in a myapp.py file from fastapi import FastAPI from prometheus_client import make_asgi_app # Create app app = FastAPI(debug=False) # Add prometheus asgi middleware to route /metrics requests metrics_app = make_asgi_app() app.mount("/metrics", metrics_app) For Multiprocessing support, use this modified code snippet.
🌐
Reddit
reddit.com › r/fastapi › prometheus middleware is out!
r/FastAPI on Reddit: Prometheus Middleware is out!
October 12, 2020 -

Today is pretty unusual day - my first Middleware for #FastApi (and, obviously, #starlette) is out. It deals with integration and customization metrics for #prometheus with, I hopefully, simple and intuitive way.

Working with #FastApi is delight and I hope this middleware will make life of couple of folks even easier :)

Will be happy with criticism and suggestions :)

https://github.com/kozhushman/prometheusrock

🌐
Hashnode
carlosmv.hashnode.dev › adding-prometheus-to-a-fastapi-app-python
Adding Prometheus to a FastAPI app | Python
April 15, 2025 - In this file, we import make_asgi_app from prometheus_client to create a Prometheus metrics app. We pass that registry to make_asgi_app() to create the metrics app. We mount that metrics app at the /metrics route using app.mount("/metrics", ...
Find elsewhere
🌐
GitHub
github.com › stephenhillier › starlette_exporter
GitHub - stephenhillier/starlette_exporter: Prometheus exporter for Starlette and FastAPI · GitHub
Example: from fastapi import FastAPI from starlette_exporter import PrometheusMiddleware, handle_metrics from starlette_exporter.optional_metrics import response_body_size, request_body_size app = FastAPI() app.add_middleware(PrometheusMiddleware, optional_metrics=[response_body_size, request_body_size]) starlette_exporter will export all the prometheus metrics from the process, so custom metrics can be created by using the prometheus_client API.
Author: stephenhillier
🌐
Reddit
reddit.com › r/fastapi › fastapi and prometheus endpoint
r/FastAPI on Reddit: FastAPI and Prometheus endpoint
December 16, 2022 -

Hi all,

I have a fastapi app which generates some custom prometheus metrics with the prometheus client library.

I can start a separate server with start_http_server method from the prometheus client, but i would like to have the /metrics endpoint be served on the same port as my fastapi app.

I cant seem to find an easy way to do this, i see a prometheus offers integration with ASGI but i cant figure out how to piece everything together. AAnyone here done this before?

🌐
Medium
dimasyotama.medium.com › building-a-powerful-observability-stack-for-fastapi-with-prometheus-grafana-loki-426822422fd6
Building a Powerful Observability Stack for FastAPI with Prometheus, Grafana & Loki | by Dimas Yoga Pratama | Medium
August 21, 2025 - Note the FastAPI service is named the-app and runs on port 5060. version: "3.8" services: prometheus-app: image: prom/prometheus:latest restart: unless-stopped container_name: prometheus-observer ports: - 9090:9090 volumes: - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml networks: - monitoring grafana: image: grafana/grafana container_name: grafana-observer restart: unless-stopped depends_on: - prometheus-app - loki-app ports: - 3000:3000 volumes: - ./grafana/provisioning:/etc/grafana/provisioning environment: - GF_SECURITY_ADMIN_USER=admin - GF_SECURITY_ADMIN_PASSWORD=admin netwo
🌐
GitHub
github.com › trallnag › prometheus-fastapi-instrumentator
GitHub - trallnag/prometheus-fastapi-instrumentator: Instrument your FastAPI with Prometheus metrics. · GitHub
It also features a modular approach to metrics that should instrument all FastAPI endpoints. You can either choose from a set of already existing metrics or create your own. And every metric function by itself can be configured as well. This chapter contains an example on the advanced usage of the Prometheus FastAPI Instrumentator to showcase most of it's features.
Author: trallnag
🌐
PyPI
pypi.org › project › fastapi-prometheus-exporter
fastapi-prometheus-exporter · PyPI
December 3, 2023 - from fastapi import FastAPI from fastapi_prometheus_exporter import PrometheusExporterMiddleware app = FastAPI() PrometheusExporterMiddleware.setup( app=app, metrics_path="/metrics", # ignore_paths=["/healthz", "/metrics"], )
🌐
GitHub
github.com › kozhushman › prometheusrock
GitHub - kozhushman/prometheusrock: Prometheus middleware for Starlette and FastAPI
Set for path /metrics handler metrics_route and your metrics will be exposed on that url for Prometheus further use. If you don't want nothing extra, this is for you. Grab the code and run to paste it! For starlette and FastAPI init part pretty similar.
Author: kozhushman
🌐
DEV Community
dev.to › ken_mwaura1 › getting-started-monitoring-a-fastapi-app-with-grafana-and-prometheus-a-step-by-step-guide-3fbn
Getting Started: Monitoring a FastAPI App with Grafana and Prometheus - A Step-by-Step Guide - DEV Community
January 30, 2025 - Prometheus is a tool for collecting metrics from your application. It can be used to collect metrics such as CPU usage, memory usage, and network traffic. ... Inorder to keep we'll use an existing FastAPI app for this guide. You can clone the repo here. However, if you want to create/use your own FastAPI app, feel free to do so. git clone https://github.com/KenMwaura1/Fast-Api-example.git
🌐
Medium
medium.com › @natalia.oypmd.mnk › integrating-fastapi-with-prometheus-ea68aa8e5089
Integrating FastAPI with Prometheus | by Lidia Manik | Medium
November 17, 2025 - # Build the Docker Image docker build -t fastapi-prometheus . # Run the Container docker run -d -p 8000:8000 fastapi-prometheus · Access it at http://localhost:8000. ... Instrumentator() creates a Prometheus collector. .instrument(app) attaches metrics tracking middleware to FastAPI.
🌐
DEV Community
dev.to › carlosm27 › adding-prometheus-to-a-fastapi-app-python-c62
Adding Prometheus to a FastAPI app | Python - DEV Community
August 25, 2023 - In this file, we import make_asgi_app from prometheus_client to create a Prometheus metrics app. We pass that registry to make_asgi_app() to create the metrics app. We mount that metrics app at the /metrics route using app.mount("/metrics", ...
🌐
GitHub
github.com › trallnag › prometheus-fastapi-instrumentator › releases
Releases · trallnag/prometheus-fastapi-instrumentator
Added new optional parameter should_include_root_path to the Instrumentator constructor. When set to True, the default exported Prometheus metrics will include the FastAPI app's effective root_path in the handler label. Defaults to False to maintain backwards compatibility.
Author: trallnag
🌐
Medium
medium.com › @carlosmarcano2704 › adding-prometheus-to-a-fastapi-app-python-e038bccdd502
Adding Prometheus to a FastAPI app | Python | by Carlos Armando Marcano Vargas | Python in Plain English
August 23, 2023 - Now, we can create a middleware, ... server. from fastapi import FastAPI, Request from prometheus_client import make_asgi_app, Counter app = FastAPI() all_requests = Counter('all_requests', 'A counter of the all requests made') ...
🌐
GitHub
github.com › prometheus › client_python › issues › 1016
ASGI mounted to FastAPI mounts metrics to `/metrics/` instead of `/metrics` · Issue #1016 · prometheus/client_python
March 15, 2024 - The documentation gives an example application which mounts the ASGI app to /metrics in a FastAPI app · from fastapi import FastAPI from prometheus_client import make_asgi_app # Create app app = FastAPI(debug=False) # Add prometheus asgi middleware to route /metrics requests metrics_app = make_asgi_app() app.mount("/metrics", metrics_app)
Author: prometheus