Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat/provide sentry userID and orgID #908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
egelhaus wants to merge 2 commits into main from sentry-userid
Closed

feat/provide sentry userID and orgID #908

egelhaus wants to merge 2 commits into main from sentry-userid

Conversation

@egelhaus
Copy link
Collaborator

@egelhaus egelhaus commented Jul 30, 2025
edited
Loading

What kind of change does this PR introduce?

eg: Bug fix, feature, docs update, ...

Why was this change needed?

Please link to related issues when possible, and explain WHY you changed things, not WHAT you changed.

Other information:

eg: Did you discuss this change with anybody before working on it (not required, but can be a good idea for bigger changes). Any plans for the future, etc?

Checklist:

Put a "X" in the boxes below to indicate you have followed the checklist;

  • I have read the CONTRIBUTING guide.
  • I checked that there were not similar issues or PRs already open for this.
  • This PR fixes just ONE issue (do not include multiple issues or types of change in the same PR) For example, don't try and fix a UI issue and include new dependencies in the same PR.

Copy link

vercel bot commented Jul 30, 2025
edited
Loading

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
postiz ⬜️ Ignored (Inspect) Jul 30, 2025 6:01pm

@egelhaus egelhaus requested a review from Copilot July 30, 2025 18:06
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds Sentry user context tracking by implementing utility functions to associate user and organization IDs with Sentry error reports. This will help with debugging by providing user context when errors occur.

Key changes:

  • Created shared utility functions for setting Sentry user context in both React and NestJS libraries
  • Integrated Sentry context setting into authentication middleware and user context components
  • Added isolation scopes to prevent context leaking between requests

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
libraries/react-shared-libraries/src/sentry/sentry.user.context.ts New utility function for setting Sentry user context in React apps
libraries/nestjs-libraries/src/sentry/sentry.user.context.ts New utility function for setting Sentry user context in NestJS apps
libraries/nestjs-libraries/src/sentry/sentry.background.context.ts New wrapper function for background jobs with Sentry isolation
apps/frontend/src/components/layout/user.context.tsx Integration of Sentry user context in React user provider
apps/backend/src/services/auth/auth.middleware.ts Integration of Sentry user context in authentication middleware
apps/backend/src/app.module.ts Minor formatting fix (trailing comma)

Comment on lines +13 to +23
// Set user context if available and Sentry is configured
if (!userId || !process.env.NEXT_PUBLIC_SENTRY_DSN) {
return await callback();
}

Sentry.getCurrentScope().setTag('user.id', userId);
if (!orgId) {
return await callback();
}
Sentry.getCurrentScope().setTag('org.id', orgId);

Copy link

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function continues execution and calls callback() even when userId is missing, but then sets user context tags later. This creates inconsistent behavior. Consider restructuring to set all context at once or return early only when Sentry is not configured.

Suggested change
// Set user context if available and Sentry is configured
if (!userId || !process.env.NEXT_PUBLIC_SENTRY_DSN) {
return await callback();
}
Sentry.getCurrentScope().setTag('user.id',userId);
if(!orgId){
returnawaitcallback();
}
Sentry.getCurrentScope().setTag('org.id', orgId);
// Return early if userId is missing or Sentry is not configured
if (!userId || !process.env.NEXT_PUBLIC_SENTRY_DSN) {
return await callback();
}
// Set all Sentry context tags before invoking the callback
constscope=Sentry.getCurrentScope();
scope.setTag('user.id',userId);
if(orgId){
scope.setTag('org.id', orgId);
}

Copilot uses AI. Check for mistakes.
Comment on lines +14 to +23
if (!userId || !process.env.NEXT_PUBLIC_SENTRY_DSN) {
return await callback();
}

Sentry.getCurrentScope().setTag('user.id', userId);
if (!orgId) {
return await callback();
}
Sentry.getCurrentScope().setTag('org.id', orgId);

Copy link

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple early returns with callback() execution creates code duplication. Consider setting both user.id and org.id tags first, then executing callback() once at the end.

Suggested change
if (!userId || !process.env.NEXT_PUBLIC_SENTRY_DSN) {
return await callback();
}
Sentry.getCurrentScope().setTag('user.id', userId);
if (!orgId) {
return await callback();
}
Sentry.getCurrentScope().setTag('org.id', orgId);
if (userId && process.env.NEXT_PUBLIC_SENTRY_DSN) {
Sentry.getCurrentScope().setTag('user.id', userId);
if (orgId) {
Sentry.getCurrentScope().setTag('org.id', orgId);
}
}

Copilot uses AI. Check for mistakes.

if (!user) {
// Wrap the entire request handling in an isolation scope to prevent context leaking
await Sentry.withIsolationScope(async () => {
Copy link

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isolation scope wrapping doesn't properly handle the async middleware pattern. The next() call should be awaited and the function should return the result of withIsolationScope, otherwise the middleware chain may not wait for completion.

Suggested change
await Sentry.withIsolationScope(async () => {
returnawait Sentry.withIsolationScope(async () => {

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

@nevo-david Do not merge this please.
There's a different, better, way to do this.

Copy link
Collaborator Author

Redoing this

@egelhaus egelhaus deleted the sentry-userid branch July 31, 2025 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot Copilot left review comments

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

AltStyle によって変換されたページ (->オリジナル) /