tl;dr: What is the recommended way to capture user in Flask, in case when there is no flask-login?
First try is to configure scope, so maybe decorate view with push_scope? Nope - it looks nice, but it is not enough if you want to have user info during e.g. error handling, which could be outside view method.
So we have configure_scope, what tell us the Sentry doc? Great, but only before #758 - before this PR, FlaskIntegration cleared the scope on every app context change (so typically for every request). Now information set global can be shared during requests - not the best option for user info.
And the last try, we have before_send hook, what is currently probably the only way to capture user. Ok, but it's like totally different use as designed for it (stripping sensitive data), and this way is undocumented neither in general Sentry doc, nor in Flask-specific page (as an option for capturing user).
I think we should have a recommended way for capturing user in Flask: by scope (similar like other platforms), hook (which works now) or a special one (like new hook in FlaskIntegration instead of hard coded support for flask-login only).
You can use configure_scope() inside of @app.before_request, your view, or a wsgi middleware. If the scope is set during request handling, the data is not shared across requests (it also works similarly for e.g. Celery: inside of the task function the scope is isolated)
push_scope and with Hub(Hub.current) is used to "fork"/isolate the scope, which happens implicitly around your request handler.
If an exception bubbles through it, the scope is popped again, as you noticed. This is an unfortunate limitation of the implementation.
However, as said, you only need configure_scope(), as the Flask integration already pushed a scope.
Oh, ok, before_request isn't the solution for me, but it looks like I misunderstand the role of #758, the global scope is still limited to the current context by _request_started - I missed this.
Activity
untitaker commented on Jul 28, 2020
You can use configure_scope() inside of @app.before_request, your view, or a wsgi middleware. If the scope is set during request handling, the data is not shared across requests (it also works similarly for e.g. Celery: inside of the task function the scope is isolated)
push_scope and with Hub(Hub.current) is used to "fork"/isolate the scope, which happens implicitly around your request handler.
If an exception bubbles through it, the scope is popped again, as you noticed. This is an unfortunate limitation of the implementation.
However, as said, you only need configure_scope(), as the Flask integration already pushed a scope.
Hope that helps
kam193 commented on Jul 28, 2020
Oh, ok, before_request isn't the solution for me, but it looks like I misunderstand the role of #758, the global scope is still limited to the current context by _request_started - I missed this.
So, issue to close ;) But can I document this behavior in https://docs.sentry.io/platforms/python/flask/? I think it's important information
untitaker commented on Jul 28, 2020
Does this work? It's shared boilerplate text nested under the Behavior section getsentry/sentry-docs#1937
kam193 commented on Jul 28, 2020
Yeah, this is what I meant :)
fix: Clarify how scopes work in Flask/Django (#1937)