-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
17
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
17 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 a21087e
Merge branch 'main' into sentry-user
egelhaus d8e6759
Initial plan
Copilot a66a053
Improve Sentry user context implementation with error handling and do…
Copilot b5bbf87
Use null instead of empty strings to clear Sentry tags
Copilot af0542b
Merge pull request #1076 from gitroomhq/copilot/sub-pr-910
egelhaus 01d1c2e
Comment out Sentry DSN in .env.example
egelhaus bbdaf7d
Merge branch 'main' into sentry-user
egelhaus 4bec77b
Merge branch 'main' into sentry-user
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
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
63 changes: 63 additions & 0 deletions
63
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,63 @@ | ||
| 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) { | ||
egelhaus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| try { | ||
| 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); | ||
| } | ||
egelhaus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch { | ||
| // Silently fail if Sentry throws an error - we don't want to break the app | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * 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; | ||
| } | ||
|
|
||
| try { | ||
| Sentry.setUser(null); | ||
| Sentry.setTag('user.activated', null); | ||
| Sentry.setTag('user.provider', null); | ||
| Sentry.setTag('user.super_admin', null); | ||
egelhaus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch { | ||
| // Silently fail if Sentry throws an error - we don't want to break the app | ||
| } | ||
| }; | ||
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
| 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. | ||
| * | ||
| * Usage Options: | ||
| * | ||
| * 1. Global interceptor (recommended for APIs with consistent auth): | ||
| * In your app.module.ts: | ||
| * ```typescript | ||
| * import { APP_INTERCEPTOR } from '@nestjs/core'; | ||
| * import { SentryUserInterceptor } from '@gitroom/nestjs-libraries/sentry/sentry.user.interceptor'; | ||
| * | ||
| * @Module({ | ||
| * providers: [ | ||
| * { provide: APP_INTERCEPTOR, useClass: SentryUserInterceptor }, | ||
| * ], | ||
| * }) | ||
| * export class AppModule {} | ||
| * ``` | ||
| * | ||
| * 2. Controller-level (for specific controllers): | ||
| * ```typescript | ||
| * @UseInterceptors(SentryUserInterceptor) | ||
| * @Controller('users') | ||
| * export class UsersController {} | ||
| * ``` | ||
| * | ||
| * 3. Method-level (for specific routes): | ||
| * ```typescript | ||
| * @UseInterceptors(SentryUserInterceptor) | ||
| * @Get('profile') | ||
| * getProfile() {} | ||
| * ``` | ||
| */ | ||
| @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(); | ||
| } | ||
| } | ||
75 changes: 75 additions & 0 deletions
75
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,75 @@ | ||
| '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; | ||
| } | ||
|
|
||
| try { | ||
| 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); | ||
| } | ||
| } catch { | ||
| // Silently fail if Sentry throws an error - we don't want to break the app | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * 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; | ||
| } | ||
|
|
||
| try { | ||
| Sentry.setUser(null); | ||
| Sentry.setTag('user.org_id', null); | ||
| Sentry.setTag('user.role', null); | ||
| Sentry.setTag('user.tier', null); | ||
egelhaus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch { | ||
| // Silently fail if Sentry throws an error - we don't want to break the app | ||
| } | ||
| }; | ||
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.