How can I send additional user context to Sentry to identify the user? #9060
Unanswered
niazangels
asked this question in
Questions
Replies: 3 comments 1 reply
-
|
@niazangels what you are saying is correct. something like this |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
I have a def get_current_user(db: Session = Depends(get_db), token: str = Security(auth)):
try:
payload = jwt.decode(token, config("SECRET_KEY"), algorithms=[ALGORITHM])
except PyJWTError:
raise HTTPException(
status_code=HTTP_403_FORBIDDEN, detail="Could not validate credentials"
)
user = crud.user.get(db, id=payload["user_id"])
if not user:
raise HTTPException(status_code=404, detail="User not found")
with configure_scope() as scope:
scope.user = {"id": user.id, "email": user.email}
try:
yield user
except Exception as e:
capture_exception(e)
raiseused in routes like this @router.get("/me/_error", response_model=User)
def get_user_error(
db: Session = Depends(get_db),
current_user: DBUser = Depends(get_current_active_user),
):
1 / 0 |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
With But with from sentry_sdk import set_user
async def set_sentry_user(
request: Request, credentials: Optional[HTTPAuthorizationCredentials] = security
) -> None:
username = // get username from credentials somehow
client = request.client
set_user({"username": username, "ip_address": client.host if client else None}) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
First check
Description
How can I send additional context to Sentry to identify the user who faced the error?
Additional context
I already use JWT to identify the user, plus I have the lovely dependencies from FastAPI for pulling in additional information from the database such as the user name. I don't know where to leverage this though.
So far, I have
which does a fine job of capturing a lot of stuff automatically, but I want to add the user information in the payload. Sentry's website tells me the following is the way to go about:
But I have no idea where to put this- exceptions could happen from any endpoint from any routers. Some of these require the users to be logged in, while some don't. Adding a scope to every router seems like a wrong way to approach this problem.
What is a good way to go about this? Any suggestions would be much appreciated.
Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions