FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Capturing user in Flask · Issue #769 · getsentry/sentry-python · GitHub

Repository navigation

Capturing user in Flask #769

Closed
getsentry/sentry-docs
#1937

Description

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).

Activity

  1. untitaker commented on Jul 28, 2020

    Member

    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.

    @app.before_request
    def _before(*a, **kw):
        with sentry_sdk.configure_scope() as scope:
             scope.user = {"id": request.environ['????']}

    Hope that helps

  2. kam193 commented on Jul 28, 2020

    Author

    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

  3. untitaker commented on Jul 28, 2020

    Member

    Does this work? It's shared boilerplate text nested under the Behavior section getsentry/sentry-docs#1937

  4. kam193 commented on Jul 28, 2020

    Author

    Yeah, this is what I meant :)

  5. added a commit that references this issue on Jul 28, 2020
    e986bce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL