-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat/sentry user implementation #910
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
Open
egelhaus
wants to merge
9
commits into
main
Choose a base branch
from
sentry-user
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cf03b9d
feat/sentry user implementation
egelhaus a997bec
Update libraries/react-shared-libraries/src/sentry/sentry.user.contex…
egelhaus 3dd49e2
Update libraries/nestjs-libraries/src/sentry/sentry.user.context.ts
egelhaus 9364012
Make "admin" user contexr optional
egelhaus 8932ed7
Update libraries/react-shared-libraries/src/sentry/sentry.user.contex…
egelhaus 47e1795
Merge branch 'main' into sentry-user
egelhaus 894dfdb
Update user.context.tsx
egelhaus d8f790b
Update user.context.tsx
egelhaus 4a21eba
Update sentry.user.context.ts
egelhaus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
libraries/nestjs-libraries/src/sentry/sentry.user.context.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,55 @@ | ||||||||||||||
import * as Sentry from '@sentry/nestjs'; | ||||||||||||||
import { User } from '@prisma/client'; | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Sets user context for Sentry for the current request. | ||||||||||||||
* This will include user information in all error reports and events. | ||||||||||||||
* Only executes if Sentry DSN is configured. | ||||||||||||||
* | ||||||||||||||
* @param user - The user object from the database | ||||||||||||||
*/ | ||||||||||||||
export const setSentryUserContext = (user: User | null) => { | ||||||||||||||
// Only set context if Sentry is configured | ||||||||||||||
if (!process.env.NEXT_PUBLIC_SENTRY_DSN) { | ||||||||||||||
return; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
if (!user) { | ||||||||||||||
// Clear user context when no user is present | ||||||||||||||
Sentry.setUser(null); | ||||||||||||||
return; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
Sentry.setUser({ | ||||||||||||||
id: user.id, | ||||||||||||||
email: user.email, | ||||||||||||||
username: user.email, // Use email as username since that's the primary identifier | ||||||||||||||
// Add additional useful context | ||||||||||||||
ip_address: undefined, // Let Sentry auto-detect IP | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
// Also set additional tags for better filtering in Sentry | ||||||||||||||
Sentry.setTag('user.activated', user.activated); | ||||||||||||||
Sentry.setTag('user.provider', user.providerName || 'local'); | ||||||||||||||
|
||||||||||||||
if (user.isSuperAdmin) { | ||||||||||||||
Sentry.setTag('user.super_admin', true); | ||||||||||||||
} | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Clears the Sentry user context. | ||||||||||||||
* Useful when logging out or switching users. | ||||||||||||||
* Only executes if Sentry DSN is configured. | ||||||||||||||
*/ | ||||||||||||||
export const clearSentryUserContext = () => { | ||||||||||||||
// Only clear context if Sentry is configured | ||||||||||||||
if (!process.env.NEXT_PUBLIC_SENTRY_DSN) { | ||||||||||||||
egelhaus marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
return; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
Sentry.setUser(null); | ||||||||||||||
Sentry.setTag('user.activated', null); | ||||||||||||||
Sentry.setTag('user.provider', null); | ||||||||||||||
Sentry.setTag('user.super_admin', null); | ||||||||||||||
Comment on lines
+52
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Setting tags to null may not properly clear them in Sentry. Consider using empty strings or calling Sentry.removeTag() if available, to ensure tags are properly cleared.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback
egelhaus marked this conversation as resolved.
Show resolved
Hide resolved
egelhaus marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
}; |
24 changes: 24 additions & 0 deletions
24
libraries/nestjs-libraries/src/sentry/sentry.user.interceptor.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { Request } from 'express'; | ||
import { User } from '@prisma/client'; | ||
import { setSentryUserContext } from './sentry.user.context'; | ||
|
||
/** | ||
* Interceptor that automatically sets Sentry user context for all requests. | ||
* This interceptor runs after authentication middleware has set req.user. | ||
*/ | ||
@Injectable() | ||
export class SentryUserInterceptor implements NestInterceptor { | ||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | ||
const request = context.switchToHttp().getRequest<Request>(); | ||
|
||
// Get user from request (set by auth middleware) | ||
const user = (request as any).user as User | undefined; | ||
egelhaus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Set Sentry user context for this request | ||
setSentryUserContext(user || null); | ||
|
||
return next.handle(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
libraries/react-shared-libraries/src/sentry/sentry.user.context.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use client'; | ||
|
||
import * as Sentry from '@sentry/nextjs'; | ||
|
||
interface UserInfo { | ||
id: string; | ||
email: string; | ||
orgId?: string; | ||
role?: string; | ||
tier?: string; | ||
} | ||
|
||
/** | ||
* Sets user context for Sentry in the frontend. | ||
* This will include user information in all error reports and events. | ||
* Only executes if Sentry DSN is configured. | ||
* | ||
* @param user - The user object from the API | ||
*/ | ||
export const setSentryUserContext = (user: UserInfo | null) => { | ||
// Only set context if Sentry is configured | ||
if (!process.env.NEXT_PUBLIC_SENTRY_DSN) { | ||
return; | ||
} | ||
|
||
if (!user) { | ||
// Clear user context when no user is present | ||
Sentry.setUser(null); | ||
return; | ||
} | ||
|
||
Sentry.setUser({ | ||
id: user.id, | ||
email: user.email, | ||
username: user.email, // Use email as username since that's the primary identifier | ||
}); | ||
|
||
// Also set additional tags for better filtering in Sentry | ||
if (user.orgId) { | ||
Sentry.setTag('user.org_id', user.orgId); | ||
} | ||
|
||
if (user.role) { | ||
Sentry.setTag('user.role', user.role); | ||
} | ||
|
||
if (user.tier) { | ||
Sentry.setTag('user.tier', user.tier); | ||
} | ||
}; | ||
|
||
/** | ||
* Clears the Sentry user context. | ||
* Useful when logging out or switching users. | ||
* Only executes if Sentry DSN is configured. | ||
*/ | ||
export const clearSentryUserContext = () => { | ||
// Only clear context if Sentry is configured | ||
if (!process.env.NEXT_PUBLIC_SENTRY_DSN) { | ||
return; | ||
} | ||
|
||
Sentry.setUser(null); | ||
Sentry.setTag('user.org_id', ''); | ||
Sentry.setTag('user.role', ''); | ||
Sentry.setTag('user.tier', ''); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.