fastapi prometheus exception: ValueError: Duplicated timeseries in CollectorRegistry

234 views
Skip to first unread message

Aqa Mustafa Akhlaqi

unread,
Jul 14, 2022, 8:39:09 AM7/14/22
to Prometheus Users
I am new user of Prometheus and I instrumented my fastapi application with it. But I get an exception  ValueError: Duplicated timeseries in CollectorRegistry exception.
My code looks like following:

app = FastAPI(title="billing-service")
instrumentator = Instrumentator(
     excluded_handlers=[

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


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(
  request: Request,  exc: RequestValidationError
) -> JSONResponse:
# some code
  return await  request_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 these exception handlers from app then everything work fine but when I add these exception handlers, Prometheus raise 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'}


The line of code which causes the problem is exactly the line which first exception handler is added to fastapi app.

Reply all
Reply to author
Forward
0 new messages