-
Notifications
You must be signed in to change notification settings - Fork 234
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
Changes from 2 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,19 @@ 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 +114,39 @@ export const enrichLogger = ( | |
| }, | ||
| }); | ||
|
|
||
| if (SENTRY_ENABLED && SENTRY_DSN) { | ||
|
Contributor
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. What's the reason why we initialize Sentry within
Contributor
Author
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. There're couple of reasons why it was chosen:
From a design/concerns perspective, conceptually, Sentry context would be better handled by a Sentry-specific helper, not a logging helper. Moving Sentry into Authentication or a Fastify hook in
|
||
| try { | ||
| if (authContext.userId) { | ||
| Sentry.setUser({ | ||
|
Contributor
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. Isn't sentry a noop if disabled?
Contributor
Author
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. Not sure, but would be better not to run on this code path at all if Sentry is not enabled. |
||
| 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'); | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| ctx.values.set<FastifyBaseLogger>({ id: fastifyLoggerId, defaultValue: newLogger }, newLogger); | ||
|
|
||
| return newLogger; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.