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 OverflowToday 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
python - How to add middleware to the Fast API to create metrics to track time spent and requests made? - Stack Overflow
FastAPI and Prometheus endpoint
FastAPI Middleware: Performance issues when adding prometheus instrumentation package - increases latency of REST api - Stack Overflow
FastAPI Middleware: Why does adding prometheus instrumentation package increase latency of REST API?
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
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:
- Create an app in the Apitally dashboard to get a client ID.
- Install the client library as a dependency in your project:
pip install "apitally[fastapi]"
- 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.
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?