Skip to content

Commit

Permalink
logging: add user information to sentry
Browse files Browse the repository at this point in the history
* Closes: #2734.

Co-Authored-by: Peter Weber <[email protected]>
  • Loading branch information
rerowep committed Oct 28, 2024
1 parent 462e814 commit 5fd16de
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'
# needs_sphinx = '1.0'

# Do not warn on external images.
suppress_warnings = ['image.nonlocal_uri']
Expand Down
25 changes: 23 additions & 2 deletions rero_ils/modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from lxml import etree
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
from requests.adapters import HTTPAdapter
from sentry_sdk import set_context
from urllib3.util import Retry
from werkzeug.local import LocalProxy

Expand Down Expand Up @@ -1092,21 +1093,41 @@ def set_user_name(sender, user):
user_name = None
remove_user_name(sender, user)

profile = user.user_profile
name_parts = [
profile.get("last_name", "").strip(),
profile.get("first_name", "").strip(),
]

user_name = ", ".join(filter(None, name_parts))
patron = None
if current_librarian:
user_name = current_librarian.formatted_name
patron = current_librarian
elif current_patrons:
user_name = current_patrons[0].formatted_name
patron = current_patrons[0]
else:
with contextlib.suppress(AttributeError):
user_name = current_user.email
if user_name:
session["user_name"] = user_name
if patron:
# Set the sentry context data for logged in user
set_context(
"patron",
{
"pid": patron.pid,
"organisation_pid": patron.organisation_pid,
"barcodes": patron.get("barcode"),
},
)


def remove_user_name(sender, user):
"""Remove the username in the current flask session."""
if session.get("user_name"):
del session["user_name"]
# Remove the sentry context data
set_context("patron", {})


def sorted_pids(query):
Expand Down

0 comments on commit 5fd16de

Please sign in to comment.