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 23, 2024
1 parent f26c0b6 commit d866cbc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions rero_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3455,6 +3455,11 @@ def _(x):
#: Sentry: use celery or not
LOGGING_SENTRY_CELERY = True

LOGGING_SENTRY_INIT_KWARGS = {
# Instruct Sentry to send user data attached to the event
"send_default_pii": True,
}

ROLLOVER_LOGGING_CONFIG = {
"version": 1,
"disable_existing_loggers": False,
Expand Down
10 changes: 10 additions & 0 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_user
from urllib3.util import Retry
from werkzeug.local import LocalProxy

Expand Down Expand Up @@ -1091,6 +1092,10 @@ def set_user_name(sender, user):

user_name = None
remove_user_name(sender, user)
set_user_data = {
"id": user.id,
"email": user.email
}

if current_librarian:
user_name = current_librarian.formatted_name
Expand All @@ -1101,12 +1106,17 @@ def set_user_name(sender, user):
user_name = current_user.email
if user_name:
session["user_name"] = user_name
set_user_data["username"] = user_name
# Set the sentry user data values for logged in user
set_user(set_user_data)


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 user data values
set_user(None)


def sorted_pids(query):
Expand Down

0 comments on commit d866cbc

Please sign in to comment.