Skip to content

Commit 5dffbc9

Browse files
committed
fix: sentry controlplane auto-instrumentation
1 parent e348472 commit 5dffbc9

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

controlplane/src/core/env.schema.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,30 @@ export const envVariables = z
186186
* Admission Webhook
187187
*/
188188
AUTH_ADMISSION_JWT_SECRET: z.string(),
189+
190+
/**
191+
* Sentry
192+
*/
193+
SENTRY_ENABLED: z
194+
.string()
195+
.optional()
196+
.transform((val) => val === 'true')
197+
.default('false'),
198+
SENTRY_DSN: z.string().optional(),
199+
SENTRY_SEND_DEFAULT_PII: z
200+
.string()
201+
.optional()
202+
.transform((val) => val === 'true')
203+
.default('false'),
204+
SENTRY_TRACES_SAMPLE_RATE: z.coerce.number().optional().default(1),
205+
SENTRY_PROFILE_SESSION_SAMPLE_RATE: z.coerce.number().optional().default(1),
206+
SENTRY_PROFILE_LIFECYCLE: z.enum(['manual', 'trace']).optional().default('manual'),
207+
SENTRY_EVENT_LOOP_BLOCK_THRESHOLD_MS: z.coerce.number().optional().default(100),
208+
SENTRY_ENABLE_LOGS: z
209+
.string()
210+
.optional()
211+
.transform((val) => val === 'true')
212+
.default('false'),
189213
})
190214
.refine((input) => {
191215
if (input.STRIPE_WEBHOOK_SECRET && !input.STRIPE_SECRET_KEY) {

controlplane/src/core/sentry.config.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,33 @@ import * as Sentry from '@sentry/node';
22
import { nodeProfilingIntegration } from '@sentry/profiling-node';
33
import { eventLoopBlockIntegration } from '@sentry/node-native';
44
import { fastifyIntegration, pinoIntegration } from '@sentry/node';
5+
import { envVariables } from "./env.schema.js";
56

6-
if (process.env.SENTRY_ENABLED === 'true' && process.env.SENTRY_DSN) {
7+
const {
8+
SENTRY_ENABLED,
9+
SENTRY_DSN,
10+
SENTRY_SEND_DEFAULT_PII,
11+
SENTRY_TRACES_SAMPLE_RATE,
12+
SENTRY_PROFILE_SESSION_SAMPLE_RATE,
13+
SENTRY_PROFILE_LIFECYCLE,
14+
SENTRY_EVENT_LOOP_BLOCK_THRESHOLD_MS,
15+
SENTRY_ENABLE_LOGS
16+
} = envVariables.parse(process.env);
17+
18+
if (SENTRY_ENABLED && SENTRY_DSN) {
719
Sentry.init({
8-
dsn: process.env.SENTRY_DSN,
20+
dsn: SENTRY_DSN,
921
integrations: [
1022
fastifyIntegration(),
11-
eventLoopBlockIntegration({ threshold: Number(process.env.SENTRY_EVENT_LOOP_BLOCK_THRESHOLD_MS ?? 100) }),
23+
eventLoopBlockIntegration({ threshold: SENTRY_EVENT_LOOP_BLOCK_THRESHOLD_MS }),
1224
nodeProfilingIntegration(),
1325
pinoIntegration({ log: { levels: ['info', 'warn', 'error'] } }),
1426
],
15-
profileSessionSampleRate: Number(process.env.SENTRY_PROFILE_SESSION_SAMPLE_RATE ?? 1),
16-
sendDefaultPii: (process.env.SENTRY_SEND_DEFAULT_PII ?? 'true') === 'true',
17-
tracesSampleRate: Number(process.env.SENTRY_TRACES_SAMPLE_RATE ?? 1),
18-
profileLifecycle: (process.env.SENTRY_PROFILE_LIFECYCLE as 'trace' | 'manual') ?? 'trace',
19-
enableLogs: (process.env.SENTRY_ENABLE_LOGS ?? 'false') === 'true',
27+
profileSessionSampleRate: SENTRY_PROFILE_SESSION_SAMPLE_RATE,
28+
sendDefaultPii: SENTRY_SEND_DEFAULT_PII,
29+
tracesSampleRate: SENTRY_TRACES_SAMPLE_RATE,
30+
profileLifecycle: SENTRY_PROFILE_LIFECYCLE,
31+
enableLogs: SENTRY_ENABLE_LOGS,
2032
});
2133
console.log('Sentry is initialized.');
2234
}

controlplane/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// eslint-disable-next-line import/order
21
import './core/sentry.config.js';
32
import * as Sentry from '@sentry/node';
43
// eslint-disable-next-line import/order
@@ -71,6 +70,8 @@ const {
7170
REDIS_PASSWORD,
7271
AUTH_ADMISSION_JWT_SECRET,
7372
CDN_BASE_URL,
73+
SENTRY_ENABLED,
74+
SENTRY_DSN,
7475
} = envVariables.parse(process.env);
7576

7677
const options: BuildConfig = {
@@ -176,9 +177,8 @@ if (STRIPE_SECRET_KEY) {
176177
};
177178
}
178179

179-
180180
const app = await build(options);
181-
if (process.env.SENTRY_ENABLED === 'true' && process.env.SENTRY_DSN) {
181+
if (SENTRY_ENABLED && SENTRY_DSN) {
182182
Sentry.setupFastifyErrorHandler(app);
183183
}
184184
await app.listen({

0 commit comments

Comments
 (0)