-
Notifications
You must be signed in to change notification settings - Fork 192
feat: add user/app context in controlplane sentry #2335
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
base: main
Are you sure you want to change the base?
Changes from 10 commits
30c821a
2bc1941
bd1e37f
036ee65
18ce188
47c630a
db7c87c
e6d811f
5287c98
1382a46
70e97ba
ec97d98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import { randomFill } from 'node:crypto'; | ||
| import * as Sentry from '@sentry/node'; | ||
| import { S3ClientConfig } from '@aws-sdk/client-s3'; | ||
| import { HandlerContext } from '@connectrpc/connect'; | ||
| import { | ||
|
|
@@ -34,13 +35,15 @@ import { isAuthenticationError, isAuthorizationError, isPublicError } from './er | |
| import { GraphKeyAuthContext } from './services/GraphApiTokenAuthenticator.js'; | ||
| import { composeFederatedContract, composeFederatedGraphWithPotentialContracts } from './composition/composition.js'; | ||
| import { SubgraphsToCompose } from './repositories/FeatureFlagRepository.js'; | ||
| import { envVariables } from './env.schema.js'; | ||
|
|
||
| const labelRegex = /^[\dA-Za-z](?:[\w.-]{0,61}[\dA-Za-z])?$/; | ||
| const organizationSlugRegex = /^[\da-z]+(?:-[\da-z]+)*$/; | ||
| const namespaceRegex = /^[\da-z]+(?:[_-][\da-z]+)*$/; | ||
| const schemaTagRegex = /^(?![/-])[\d/A-Za-z-]+(?<![/-])$/; | ||
| const graphNameRegex = /^[\dA-Za-z]+(?:[./@_-][\dA-Za-z]+)*$/; | ||
| const pluginVersionRegex = /^v\d+$/; | ||
| const { SENTRY_ENABLED, SENTRY_DSN } = envVariables.parse(process.env); | ||
|
||
|
|
||
| /** | ||
| * Wraps a function with a try/catch block and logs any errors that occur. | ||
|
|
@@ -107,6 +110,40 @@ export const enrichLogger = ( | |
| }, | ||
| }); | ||
|
|
||
| if (SENTRY_ENABLED && SENTRY_DSN) { | ||
|
||
| try { | ||
| // NB: Fastify integration automatically creates request-specific scopes | ||
| if (authContext.userId) { | ||
| Sentry.setUser({ | ||
|
||
| id: authContext.userId, | ||
| username: authContext.userDisplayName, | ||
| }); | ||
| } | ||
|
|
||
| if (authContext.organizationId) { | ||
| Sentry.setTag('org.id', authContext.organizationId); | ||
| } | ||
|
|
||
| if ('organizationSlug' in authContext && authContext.organizationSlug) { | ||
| Sentry.setTag('org.slug', authContext.organizationSlug); | ||
| } | ||
|
|
||
| if ('apiKeyName' in authContext && authContext.apiKeyName) { | ||
| Sentry.setTag('api.key', authContext.apiKeyName); | ||
| } | ||
|
|
||
| if ('federatedGraphId' in authContext && authContext.federatedGraphId) { | ||
| Sentry.setTag('graph.id', authContext.federatedGraphId); | ||
| } | ||
|
|
||
| if ('auth' in authContext && authContext.auth) { | ||
| Sentry.setTag('auth.kind', authContext.auth); | ||
| } | ||
| } catch (error) { | ||
| newLogger.debug({ err: error }, 'Failed to enrich Sentry context'); | ||
| } | ||
| } | ||
|
|
||
| ctx.values.set<FastifyBaseLogger>({ id: fastifyLoggerId, defaultValue: newLogger }, newLogger); | ||
|
|
||
| return newLogger; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it necessary? To me it looks like it would be better to set necessary env in the test setup file.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was required for Vitest once I added this, it was requiring all envvars not just the imported/parsed two:
Do you have any suggestion instead?