Skip to content

Commit 8ee96d1

Browse files
committed
feat: setup sentry for the controlplane
1 parent 0574b99 commit 8ee96d1

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

controlplane/src/core/env.schema.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,31 @@ export const envVariables = z
178178
* Admission Webhook
179179
*/
180180
AUTH_ADMISSION_JWT_SECRET: z.string(),
181+
182+
/**
183+
* Sentry
184+
*/
185+
SENTRY_ENABLED: z
186+
.string()
187+
.optional()
188+
.transform((val) => val === 'true')
189+
.default('false'),
190+
SENTRY_DSN: z.string().optional(),
191+
SENTRY_SEND_DEFAULT_PII: z
192+
.string()
193+
.optional()
194+
.transform((val) => val === 'true'),
195+
SENTRY_TRACES_SAMPLE_RATE: z
196+
.string()
197+
.transform((val) => Number.parseInt(val))
198+
.optional()
199+
.default('0.0'),
200+
SENTRY_PROFILE_SESSION_SAMPLE_RATE: z
201+
.string()
202+
.transform((val) => Number.parseInt(val))
203+
.optional()
204+
.default('0.0'),
205+
SENTRY_EVENT_LOOP_BLOCK_THRESHOLD_MS: z.coerce.number().optional(),
181206
})
182207
.refine((input) => {
183208
if (input.STRIPE_WEBHOOK_SECRET && !input.STRIPE_SECRET_KEY) {

controlplane/src/core/sentry.config.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,28 @@ import * as Sentry from '@sentry/node';
44
import { nodeProfilingIntegration } from '@sentry/profiling-node';
55
import { eventLoopBlockIntegration } from '@sentry/node-native';
66

7-
if (process.env.SENTRY_ENABLED) {
8-
Sentry.init({
9-
dsn: process.env.SENTRY_DSN,
10-
integrations: [
11-
eventLoopBlockIntegration({ threshold: Number(process.env.SENTRY_EVENT_LOOP_BLOCK_THRESHOLD_MS) }),
12-
nodeProfilingIntegration(),
13-
],
14-
profileSessionSampleRate: Number(process.env.SENTRY_PROFILE_SESSION_SAMPLE_RATE),
15-
sendDefaultPii: Boolean(process.env.SENTRY_SEND_DEFAULT_PII),
16-
tracesSampleRate: Number(process.env.SENTRY_TRACES_SAMPLE_RATE),
17-
});
7+
export interface SentryConfig {
8+
sentry: {
9+
enabled: boolean;
10+
dsn: string;
11+
eventLoopBlockIntegrationThresholdMs?: number;
12+
profileSessionSampleRate?: number;
13+
sendDefaultPii?: boolean;
14+
tracesSampleRate?: number;
15+
};
1816
}
1917

20-
export * as Sentry from '@sentry/node';
18+
export function init(opts: SentryConfig) {
19+
if (process.env.SENTRY_ENABLED) {
20+
Sentry.init({
21+
dsn: opts.sentry.dsn,
22+
integrations: [
23+
eventLoopBlockIntegration({ threshold: opts.sentry.eventLoopBlockIntegrationThresholdMs }),
24+
nodeProfilingIntegration(),
25+
],
26+
profileSessionSampleRate: opts.sentry.profileSessionSampleRate,
27+
sendDefaultPii: opts.sentry.sendDefaultPii,
28+
tracesSampleRate: opts.sentry.tracesSampleRate,
29+
});
30+
}
31+
}

controlplane/src/index.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'dotenv/config';
55

66
import build, { BuildConfig } from './core/build-server.js';
77
import { envVariables } from './core/env.schema.js';
8+
import { SentryConfig } from './core/sentry.config.js';
89

910
const {
1011
LOG_LEVEL,
@@ -66,6 +67,12 @@ const {
6667
REDIS_PASSWORD,
6768
AUTH_ADMISSION_JWT_SECRET,
6869
CDN_BASE_URL,
70+
SENTRY_ENABLED,
71+
SENTRY_DSN,
72+
SENTRY_SEND_DEFAULT_PII,
73+
SENTRY_TRACES_SAMPLE_RATE,
74+
SENTRY_PROFILE_SESSION_SAMPLE_RATE,
75+
SENTRY_EVENT_LOOP_BLOCK_THRESHOLD_MS,
6976
} = envVariables.parse(process.env);
7077

7178
const options: BuildConfig = {
@@ -170,9 +177,19 @@ if (STRIPE_SECRET_KEY) {
170177
};
171178
}
172179

173-
if (process.env.SENTRY_ENABLED === 'true') {
174-
if (process.env.SENTRY_DSN) {
175-
await import('./core/sentry.config.js');
180+
if (SENTRY_ENABLED) {
181+
if (SENTRY_DSN) {
182+
const sentryConfig: SentryConfig = {
183+
sentry: {
184+
enabled: SENTRY_ENABLED,
185+
dsn: SENTRY_DSN,
186+
eventLoopBlockIntegrationThresholdMs: SENTRY_EVENT_LOOP_BLOCK_THRESHOLD_MS,
187+
profileSessionSampleRate: SENTRY_PROFILE_SESSION_SAMPLE_RATE,
188+
sendDefaultPii: SENTRY_SEND_DEFAULT_PII,
189+
tracesSampleRate: SENTRY_TRACES_SAMPLE_RATE,
190+
},
191+
};
192+
await import('./core/sentry.config.js').then((sentry) => sentry.init(sentryConfig));
176193
} else {
177194
throw new Error('SENTRY_ENABLED is set but SENTRY_DSN is not');
178195
}

0 commit comments

Comments
 (0)