stephenhillier / starlette_exporter

Prometheus exporter for Starlette and FastAPI

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PrometheusMiddleware raises wrong exception

MrBTTF opened this issue · comments

Hi
If a route handler fails, PrometheusMiddleware raises this kind of exception

...
  File "/app/common/server.py", line 55, in middleware
    response = await call_next(request)
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 45, in call_next
    task.result()
  File "/usr/local/lib/python3.9/site-packages/starlette/middleware/base.py", line 38, in coro
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.9/site-packages/starlette_exporter/middleware.py", line 105, in __call__
    self.request_time.labels(*labels).observe(end - begin)
TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'

It is the result of end being not initialized on this line
https://github.com/stephenhillier/starlette_exporter/blob/master/starlette_exporter/middleware.py#L110
if wrapped_send function is not called due to exception on this line
https://github.com/stephenhillier/starlette_exporter/blob/master/starlette_exporter/middleware.py#L86
Apparently wrapper_send is not called because startlette raises exception from a route handler function before calling sender
https://github.com/encode/starlette/blob/master/starlette/exceptions.py#L82

Hi @MrBTTF, f81ee3c was supposed to fix this. Did you get a chance to test v0.8.1 or is the problem still happening?
ps, thanks for the all the details. If you're still having an issue that'll help narrow it down.

Now the proper exception is raised. Thanks!