question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AttributeError: 'Request' object has no attribute 'prom_start_time'

See original GitHub issue

before_request from export_defaults() is not guaranteed to run if some other before_request hook raises exception, for example for authentication.

Therefore, when after_request is triggered request.prom_start_time might not be set.

Suggested fix would be to skip the histogram section in after_request if request has no prom_start_time attribute.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nonnibcommented, Nov 13, 2018

That’s an amazing turn-around time, thank you!

1reaction
nonnibcommented, Nov 13, 2018

Yes, I’m sorry, I should have elaborated. an unhandled exception will of course just crash in the before_request hook, but if the exception is handled by the app (or flask) before_request handlers which are unlucky enough to be processed after that one won’t get called, but after_request handlers will be called as usual.

Minimal example here:

from flask import Flask
from werkzeug.serving import run_simple
from prometheus_flask_exporter import PrometheusMetrics
from werkzeug.exceptions import HTTPException
app = Flask(__name__)

@app.before_request
def before_request():
    raise HTTPException(404) # or abort(404)

# if we initialize the metrics middleware _after_ our own before_request handler
# we ensure (as much as dict ordering can be ensured) that its `before_request` handlers
# are called after the one above.
metrics = PrometheusMetrics(app)

if __name__ == '__main__':
    run_simple('localhost', 8080, app, use_reloader=False)

navigate to http://localhost:8080 and you should get this error:

Traceback (most recent call last):
...
  File "/venv/lib/python3.6/site-packages/prometheus_flask_exporter/__init__.py", line 239, in after_request
    total_time = max(default_timer() - request.prom_start_time, 0)
  File "/venv/lib/python3.6/site-packages/werkzeug/local.py", line 347, in __getattr__
    return getattr(self._get_current_object(), name)
AttributeError: 'Request' object has no attribute 'prom_start_time'
Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found