Sentry-python: Cannot push user scope using 0.13.2 and Flask

Created on 20 Nov 2019  ·  7Comments  ·  Source: getsentry/sentry-python

Here's my decorator that I use to set the scope when necessary

def sentry_configure_scope(func):
    """
    This decorator will set the user scope for sentry
    """

    @wraps(func)
    def decorated_function(*args, **kwargs):
        if session['token']:
            with configure_scope() as scope:
                token = session['token']
                claims = decode(token, verify=False)
                u = {
                    "id": claims['identity']['client_id'],
                    "username": "" + claims['identity']['first_name'] + " " + claims['identity']['last_name'],
                    "email": claims['identity']['email']
                }
                scope.set_user(u)
        return func(*args, **kwargs)

    return decorated_function

I've checked using the pycharm debugger and it seems that the scope._user is correctly set with the dict but once on sentry.io, the user scope is nowhere to be found...
Any guess ? (only tested on local, maybe that's it ?)

question

All 7 comments

There might be a user object set by the flask integration that overwrites
yours. We should fix that to update the user object instead.

Try setting the data in extra, or using before_send which runs slightly
later.

On Wed, Nov 20, 2019, 23:02 Jules Lasne (jlasne) notifications@github.com
wrote:

Here's my decorator that I use to set the scope when necessary

def sentry_configure_scope(func):
""" This decorator will set the user scope for sentry """

@wraps(func)
def decorated_function(*args, **kwargs):
    if session['token']:
        with configure_scope() as scope:
            token = session['token']
            claims = decode(token, verify=False)
            u = {
                "id": claims['identity']['client_id'],
                "username": "" + claims['identity']['first_name'] + " " + claims['identity']['last_name'],
                "email": claims['identity']['email']
            }
            scope.set_user(u)
    return func(*args, **kwargs)

return decorated_function

I've checked using the pycharm debugger and it seems that the scope._user
is correctly set with the dict but once on sentry.io, the user scope is
nowhere to be found...
Any guess ? (only tested on local, maybe that's it ?)


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/getsentry/sentry-python/issues/561?email_source=notifications&email_token=AAGMPRJ6X2WILJ5AKZVPVZLQUWXYFA5CNFSM4JPZ5KI2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4H25ZWEA,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAGMPRMSHRSUC4KVB26WP7DQUWXYFANCNFSM4JPZ5KIQ
.

I just tried before_event like so

def configure_user_scope(event, hint):
    if session["token"]:
        with sentry_sdk.configure_scope() as scope:
            token = session["token"]
            claims = decode(token, verify=False)
            u = {
                "id": claims["identity"]["client_id"],
                "username": ""
                + claims["identity"]["first_name"]
                + " "
                + claims["identity"]["last_name"],
                "email": claims["identity"]["email"],
            }
            scope.set_user(u)
    return event

which didn't work... I'll try the extras

@Seluj78 try

def configure_user_scope(event, hint):
    if session["token"]:
        with sentry_sdk.configure_scope() as scope:
            token = session["token"]
            claims = decode(token, verify=False)
            u = {
                "id": claims["identity"]["client_id"],
                "username": ""
                + claims["identity"]["first_name"]
                + " "
                + claims["identity"]["last_name"],
                "email": claims["identity"]["email"],
            }
            event.setdefault("user", {}).update(u)
    return event

scopes are not a thing inside before_send

I tried this (before your message about scopes)

def configure_user_scope(event, hint):
    if session["token"]:
        with sentry_sdk.configure_scope() as scope:
            token = session["token"]
            claims = decode(token, verify=False)
            scope.set_extra("id", claims["identity"]["client_id"])
            scope.set_extra("username", "" + claims["identity"]["first_name"] + " " + claims["identity"]["last_name"])
            scope.set_extra("email", claims["identity"]["email"])
    return event

which obviously didn't work

I'll try your solution now :D

Heeeeey your solution worked ! 🚀

Thanks a lot for the solution ! Have a wonderful day ! ❤️ 🤗 🚀 🎉

Glad to hear that!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

untitaker picture untitaker  ·  3Comments

miracle2k picture miracle2k  ·  6Comments

hyperknot picture hyperknot  ·  4Comments

xarg picture xarg  ·  6Comments

mlennon-lumere picture mlennon-lumere  ·  4Comments