Skip to content

Commit

Permalink
fix(sentry): fix set_sentry_context before_request function
Browse files Browse the repository at this point in the history
Set user ; ID, username and email ; from `flask_login.current_user`, if `current_user.is_authenticated`, rather than from `g.current_user` that may not exist because of uncontrolled order for before_request functions calls.
  • Loading branch information
VincentCauchois authored and jacquesfize committed Apr 15, 2024
1 parent 75f0845 commit be8624b
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions backend/geonature/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,23 @@ def load_current_user():
g._permissions_by_user = {}
g._permissions = {}

# FIXME : order of @app.before_request is random thus g.current_user can be empty...
# if config.get("SENTRY_DSN"):
# from sentry_sdk import set_tag, set_user

# @app.before_request
# def set_sentry_context():
# if "FLASK_REQUEST_ID" in request.environ:
# set_tag("request.id", request.environ["FLASK_REQUEST_ID"])
# if g.current_user:
# set_user(
# {
# "id": g.current_user.id_role,
# "username": g.current_user.identifiant,
# "email": g.current_user.email,
# }
# )
if config.get("SENTRY_DSN"):
from sentry_sdk import set_tag, set_user

@app.before_request
def set_sentry_context():
from flask_login import current_user

if "FLASK_REQUEST_ID" in request.environ:
set_tag("request.id", request.environ["FLASK_REQUEST_ID"])
if current_user.is_authenticated:
set_user(
{
"id": current_user.id_role,
"username": current_user.identifiant,
"email": current_user.email,
}
)

admin.init_app(app)

Expand Down

0 comments on commit be8624b

Please sign in to comment.