Skip to content

Commit

Permalink
🔧(api) set request user in Sentry's context
Browse files Browse the repository at this point in the history
We need to track how users are using the API and who's raising observed
errors. Adding only the username to the context will help a lot.
  • Loading branch information
jmaupetit committed Jan 6, 2025
1 parent 329c631 commit 04f9e66
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to

- Activate and configure Sentry profiling by setting the
`SENTRY_PROFILES_SAMPLE_RATE` configuration
- Set request's user (`username`) in Sentry's context

### Changed

Expand Down
4 changes: 4 additions & 0 deletions src/api/qualicharge/auth/oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
InvalidTokenError,
)
from pydantic import AnyHttpUrl
from sentry_sdk import set_user
from sqlalchemy.orm import joinedload
from sqlmodel import Session as SMSession
from sqlmodel import select
Expand Down Expand Up @@ -200,6 +201,9 @@ def get_user(
logger.error(f"User {token.email} tried to login but is not active")
raise AuthenticationError("User is not active")

# Add username to sentry's context
set_user({"username": user.username})

# We do not check scopes for admin users
if user.is_superuser:
return user
Expand Down

0 comments on commit 04f9e66

Please sign in to comment.