sanic-prometheus icon indicating copy to clipboard operation
sanic-prometheus copied to clipboard

404 page causes KeyError: '__START_TIME__'

Open optroodt opened this issue 4 years ago • 1 comments

When you reach a 404, it looks like the before_request_handler is not called and thus triggers the following error in the after_request_handler:

[2020-06-10 15:13:42 +0200] [17655] [ERROR] Exception occurred in one of response middleware handlers
Traceback (most recent call last):
  File "/Users/optroodt/venvs/<snip>/lib/python3.7/site-packages/sanic/app.py", line 1017, in handle_request
    request, response, request_name=name
  File "/Users/optroodt/venvs/<snip>/lib/python3.7/site-packages/sanic/app.py", line 1315, in _run_response_middleware
    _response = await _response
  File "/Users/optroodt/venvs/<snip>/lib/python3.7/site-packages/sanic_prometheus/__init__.py", line 125, in before_response
    metrics.after_request_handler(request, response, get_endpoint)
  File "/Users/optroodt/venvs/<snip>/lib/python3.7/site-packages/sanic_prometheus/metrics.py", line 56, in after_request_handler
    lat = time.time() - request['__START_TIME__']
  File "/Users/optroodt/venvs/<snip>/lib/python3.7/site-packages/sanic/request.py", line 143, in __getitem__
    return self.ctx.__dict__[key]
KeyError: '__START_TIME__'

Running:

  • MacOS 10.14.6 (Yes, I should upgrade :D)
  • Python 3.7.7,
  • sanic 19.12.2
  • sanic-prometheus 0.2.0

optroodt avatar Jun 10 '20 13:06 optroodt

https://github.com/dkruchinin/sanic-prometheus/pull/33

Arvind2222 avatar Jul 08 '20 23:07 Arvind2222