FastAPI Prometheus exception ValueError: Duplicated timeseries in CollectorRegistry

70 views
Skip to first unread message

Aqa Mustafa Akhlaqi

unread,
Jul 14, 2022, 8:39:13 AM7/14/22
to Prometheus Users
I am new user of Prometheus.
I get  ValueError: Duplicated timeseries in CollectorRegistry and I tried different ways but could not figure out out is the problem and how to solve it.
My code looks like the following: 

instrumentator.instrument(app)
instrumentator.expose(app, include_in_schema=False)
app.include_router(xxx)
app.include_router(xxx)
app.include_router(xxx)

async def validation_exception_handler(
request: Request, exc: RequestValidationError
) -> JSONResponse:
# some code
return await input_validation_exception_handler(request, exc)

@app.exception_handler(HTTPException)
async def custom_http_exception_handler(
request: Request, exc: HTTPException
) -> JSONResponse:
# some code
return await http_exception_handler(request, exc)


When I remove this exception handler everything is fine but if I add this exception handler then it raises the following exception:

File "/app/app/main.py", line 113, in <module>

    async def custom_http_exception_handler(

  File "/usr/local/lib/python3.8/site-packages/starlette/applications.py", line 161, in decorator

    self.add_exception_handler(exc_class_or_status_code, func)

  File "/usr/local/lib/python3.8/site-packages/starlette/applications.py", line 135, in add_exception_handler

    self.middleware_stack = self.build_middleware_stack()

  File "/usr/local/lib/python3.8/site-packages/fastapi/applications.py", line 186, in build_middleware_stack

    app = cls(app=app, **options)

  File "/usr/local/lib/python3.8/site-packages/prometheus_fastapi_instrumentator/middleware.py", line 52, in __init__

    self.instrumentations = instrumentations or [metrics.default()]

  File "/usr/local/lib/python3.8/site-packages/prometheus_fastapi_instrumentator/metrics.py", line 544, in default

    TOTAL = Counter(

  File "/usr/local/lib/python3.8/site-packages/prometheus_client/metrics.py", line 143, in __init__

    registry.register(self)

  File "/usr/local/lib/python3.8/site-packages/prometheus_client/registry.py", line 43, in register

    raise ValueError(

ValueError: Duplicated timeseries in CollectorRegistry: {'http_requests_created', 'http_requests_total', 'http_requests'}

Reply all
Reply to author
Forward
0 new messages