Skip to content
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/controlplane-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ jobs:
- name: Setup Keycloak
run: nohup .github/scripts/setup-keycloak.sh &

- name: Copy dummy env file
Copy link
Contributor

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.

Copy link
Contributor Author

@miklosbarabas miklosbarabas Nov 16, 2025

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:

const { SENTRY_ENABLED, SENTRY_DSN } = envVariables.parse(process.env);

Do you have any suggestion instead?

run: cp ./controlplane/.env.example ./controlplane/.env

- name: Test
run: pnpm run --filter controlplane test
env:
Expand Down
2 changes: 2 additions & 0 deletions controlplane/src/core/sentry.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Sentry from '@sentry/node';
import { nodeProfilingIntegration } from '@sentry/profiling-node';
import { eventLoopBlockIntegration } from '@sentry/node-native';
import { fastifyIntegration, pinoIntegration } from '@sentry/node';
import { version } from '../../package.json';
import { envVariables } from './env.schema.js';

const {
Expand All @@ -18,6 +19,7 @@ const {
if (SENTRY_ENABLED && SENTRY_DSN) {
Sentry.init({
dsn: SENTRY_DSN,
release: version,
integrations: [
fastifyIntegration(),
eventLoopBlockIntegration({ threshold: SENTRY_EVENT_LOOP_BLOCK_THRESHOLD_MS }),
Expand Down
37 changes: 37 additions & 0 deletions controlplane/src/core/util.ts
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 {
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

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

There should be only the main entry file that interacts with globals directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so how would you use those envvars in here?


/**
* Wraps a function with a try/catch block and logs any errors that occur.
Expand Down Expand Up @@ -107,6 +110,40 @@ export const enrichLogger = (
},
});

if (SENTRY_ENABLED && SENTRY_DSN) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the reason why we initialize Sentry within enrichLogger? How are they related?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There're couple of reasons why it was chosen:

  • enrichLogger(ctx, logger, authContext) is the first point in each Connect handler where you have:
    • authContext (user/org/api-key/graph info), and
    • ctx (request-scoped context tied to the Fastify request via contextValues in src/core/build-server.ts:493).
  • That's also the only shared helper called from all the bufservice handlers immediately after opts.authenticator.authenticate(...).

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 build-server.ts sounds attractive, but:

  • Authentication doesn't have access to HandlerContext/Fastify logger.
  • The Fastify contextValues hook in build-server.ts doesn’t have authContext (auth hasn’t run yet).
  • To do that properly we'd need a custom Connect interceptor/middleware layer, which is a more invasive change.

try {
// NB: Fastify integration automatically creates request-specific scopes
if (authContext.userId) {
Sentry.setUser({
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't sentry a noop if disabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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');
}
}

ctx.values.set<FastifyBaseLogger>({ id: fastifyLoggerId, defaultValue: newLogger }, newLogger);

return newLogger;
Expand Down
3 changes: 2 additions & 1 deletion controlplane/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ export default defineConfig({
test: {
pool: 'forks',
// Increase the timeout for integration tests
testTimeout: 20_000,
testTimeout: 35_000,
teardownTimeout: 10_000,
// Ensure always the CJS version is used otherwise we might conflict with multiple versions of graphql
alias: [{ find: /^graphql$/, replacement: 'graphql/index.js' }],
setupFiles: ['dotenv/config'],
},
});
Loading