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
🌐
PyPI
pypi.org › project › prometheus-fastapi-instrumentator
prometheus-fastapi-instrumentator · PyPI
To expose an endpoint for the metrics either follow Prometheus Python Client and add the endpoint manually to the FastAPI or serve it on a separate server. You can also use the included expose method. It will add an endpoint to the given FastAPI. With should_gzip you can instruct the endpoint to compress the data as long as the client accepts gzip encoding.
🌐
Medium
medium.com › @hitorunajp › prometheus-on-a-fastapi-application-aa25e5223a9e
Prometheus on a FastAPI application | by Hitoruna | Medium
September 1, 2025 - (If you are curious there is one more metric added by the Prometheus client library that tells the timestamp when the counter was created in Unix time) We will complicate things a bit more now. We are going to modify the FastAPI by adding a middleware.py file as in this tag.
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
python - How to add middleware to the Fast API to create metrics to track time spent and requests made? - Stack Overflow
I am adding Middleware to my Fast API app to create Prometheus metrics to get the Processing Time and Number of requests per route. Can someone tell me what I am missing? ... This is my middleware. import time from fastapi import Request from prometheus_client import Summary, make_asgi_app, ... More on stackoverflow.com
🌐 stackoverflow.com
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 = Fast... More on github.com
🌐 github.com
8
March 15, 2024
🌐
GitHub
github.com › trallnag › prometheus-fastapi-instrumentator
GitHub - trallnag/prometheus-fastapi-instrumentator: Instrument your FastAPI with Prometheus metrics. · GitHub
To expose an endpoint for the metrics either follow Prometheus Python Client and add the endpoint manually to the FastAPI or serve it on a separate server. You can also use the included expose method. It will add an endpoint to the given FastAPI. With should_gzip you can instruct the endpoint to compress the data as long as the client accepts gzip encoding.
Author: trallnag
🌐
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

🌐
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 ...
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.

🌐
Hashnode
carlosmv.hashnode.dev › adding-prometheus-to-a-fastapi-app-python
Adding Prometheus to a FastAPI app | Python
April 15, 2025 - 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') ...
Find elsewhere
🌐
Readthedocs
aioprometheus.readthedocs.io › en › latest › user › index.html
User Guide — aioprometheus Documentation
The metrics route renders Prometheus metrics from the default collector registry into the appropriate format. Run: (venv) $ pip install fastapi uvicorn (venv) $ python fastapi-example.py """ from typing import List from fastapi import FastAPI, Header, Request, Response from aioprometheus import REGISTRY, Counter, render app = FastAPI() app.state.events_counter = Counter("events", "Number of events.") @app.get("/") async def hello(request: Request): request.app.state.events_counter.inc({"path": "/"}) return "FastAPI Hello" @app.get("/metrics") async def handle_metrics( request: Request, # pylint: disable=unused-argument accept: List[str] = Header(None), ) -> Response: content, http_headers = render(REGISTRY, accept) return Response(content=content, media_type=http_headers["Content-Type"]) if __name__ == "__main__": import uvicorn uvicorn.run(app)
🌐
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"], )
🌐
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 - FastAPI Application (the-app): The Python application we want to monitor. We'll use the prometheus-fastapi-instrumentator library to automatically expose Prometheus-compatible metrics.
🌐
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 - 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 › stephenhillier › starlette_exporter
GitHub - stephenhillier/starlette_exporter: Prometheus exporter for Starlette and FastAPI · GitHub
from starlette.applications import Starlette from starlette_exporter import PrometheusMiddleware, handle_metrics app = Starlette() app.add_middleware(PrometheusMiddleware) app.add_route("/metrics", handle_metrics) ... from fastapi import FastAPI from starlette_exporter import PrometheusMiddleware, handle_metrics app = FastAPI() app.add_middleware(PrometheusMiddleware) app.add_route("/metrics", handle_metrics) ...
Author: stephenhillier
🌐
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 › trallnag › prometheus-fastapi-instrumentator › releases
Releases · trallnag/prometheus-fastapi-instrumentator
Major release with a single breaking change: Python 3.7 is not supported anymore. Beyond that, three improvements based on various pull requests. Instrumentator now works without FastAPI. This is possible because every FastAPI app is also a Starlette app (but not the other way around). Or to be more specific: FastAPI uses Starlette for things like routing and middleware this package relies on.
Author: trallnag
🌐
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 - 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) and says that you should be able to see the metrics at http://localhost:8000/metrics, however if you try to access this endpoint ·
Author: prometheus
🌐
PyPI
pypi.org › project › starlette-exporter
starlette-exporter · PyPI
from starlette.applications import Starlette from starlette_exporter import PrometheusMiddleware, handle_metrics app = Starlette() app.add_middleware(PrometheusMiddleware) app.add_route("/metrics", handle_metrics) ... from fastapi import FastAPI from starlette_exporter import PrometheusMiddleware, handle_metrics app = FastAPI() app.add_middleware(PrometheusMiddleware) app.add_route("/metrics", handle_metrics) ...
      » pip install starlette-exporter
    
Published: Jun 26, 2024
Version: 0.23.0
🌐
GitHub
github.com › kozhushman › prometheusrock
GitHub - kozhushman/prometheusrock: Prometheus middleware for Starlette and FastAPI
from prometheusrock import PrometheusMiddleware, metrics_route app = # Starlette() or FastAPI() app.add_middleware(PrometheusMiddleware) app.add_route("/metrics", metrics_route) ...
Author: kozhushman
🌐
Medium
medium.com › @bhagyarana80 › monitoring-fastapi-with-prometheus-and-grafana-2a1df999966f
Monitoring FastAPI with Prometheus and Grafana | by Bhagya Rana | Medium
August 16, 2025 - Monitor FastAPI apps using Prometheus and Grafana. Learn setup, metrics, dashboards, and performance insights for production-ready observability. FastAPI is one of the fastest-growing frameworks in the Python ecosystem.