From f1e3a44c8936237ba979ff4fd6801e0af62e6467 Mon Sep 17 00:00:00 2001 From: giancarlo Date: Mon, 15 Apr 2024 14:41:48 +0800 Subject: [PATCH] Refactor monitoring instrumentation setup Removed the MONITORING_PROVIDER constant from the monitoring package, replaced it with direct process.env access. This ensures that environment variables are accessed directly at the time of use without intermediate variables. In the apps/web/instrumentation.ts file, refactored condition checks for runtime environment and instrumentation enablement, using direct access environmental variables. This change simplifies the code and improves readability. --- apps/web/.env.test | 6 +++++- apps/web/instrumentation.ts | 10 ++++------ packages/monitoring/sentry/src/instrumentation.ts | 2 +- packages/monitoring/src/instrumentation.ts | 14 +++----------- 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/apps/web/.env.test b/apps/web/.env.test index 9a0355845..291b0d6ac 100644 --- a/apps/web/.env.test +++ b/apps/web/.env.test @@ -53,4 +53,8 @@ NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY= NEXT_PUBLIC_ENABLE_ACCOUNT_DELETION=true NEXT_PUBLIC_ENABLE_PERSONAL_ACCOUNT_BILLING=true NEXT_PUBLIC_ENABLE_ORGANIZATION_DELETION=true -NEXT_PUBLIC_ENABLE_ORGANIZATION_BILLING=true \ No newline at end of file +NEXT_PUBLIC_ENABLE_ORGANIZATION_BILLING=true + +# MONITORING +MONITORING_PROVIDER= +MONITORING_INSTRUMENTATION_ENABLED=false \ No newline at end of file diff --git a/apps/web/instrumentation.ts b/apps/web/instrumentation.ts index 954d16634..f15910dc6 100644 --- a/apps/web/instrumentation.ts +++ b/apps/web/instrumentation.ts @@ -3,14 +3,12 @@ * for your Next.js application. */ -const RUNTIME = process.env.NEXT_RUNTIME; - -const ENABLE_INSTRUMENTATION = - process.env.MONITORING_INSTRUMENTATION_ENABLED === 'true'; - export async function register() { // only run in nodejs runtime - if (RUNTIME === 'nodejs' && ENABLE_INSTRUMENTATION) { + if ( + process.env.NEXT_RUNTIME === 'nodejs' && + process.env.MONITORING_INSTRUMENTATION_ENABLED + ) { const { registerMonitoringInstrumentation } = await import( '@kit/monitoring/instrumentation' ); diff --git a/packages/monitoring/sentry/src/instrumentation.ts b/packages/monitoring/sentry/src/instrumentation.ts index 141a1ecf2..3a15c301c 100644 --- a/packages/monitoring/sentry/src/instrumentation.ts +++ b/packages/monitoring/sentry/src/instrumentation.ts @@ -28,7 +28,7 @@ export async function registerInstrumentation() { resource: new Resource({ [SEMRESATTRS_SERVICE_NAME]: serviceName, }), - spanProcessor: new SentrySpanProcessor(), + spanProcessors: [new SentrySpanProcessor()], textMapPropagator: new SentryPropagator(), }); diff --git a/packages/monitoring/src/instrumentation.ts b/packages/monitoring/src/instrumentation.ts index 3cfe70c3b..355058a75 100644 --- a/packages/monitoring/src/instrumentation.ts +++ b/packages/monitoring/src/instrumentation.ts @@ -1,13 +1,5 @@ import { InstrumentationProvider } from './monitoring-providers.enum'; -/** - * @name MONITORING_PROVIDER - * @description Register monitoring instrumentation based on the MONITORING_PROVIDER environment variable. - */ -const MONITORING_PROVIDER = process.env.MONITORING_PROVIDER as - | InstrumentationProvider - | undefined; - /** * @name registerMonitoringInstrumentation * @description Register monitoring instrumentation based on the MONITORING_PROVIDER environment variable. @@ -15,13 +7,13 @@ const MONITORING_PROVIDER = process.env.MONITORING_PROVIDER as * Please set the MONITORING_PROVIDER environment variable to register the monitoring instrumentation provider. */ export async function registerMonitoringInstrumentation() { - if (!MONITORING_PROVIDER) { + if (!process.env.MONITORING_PROVIDER) { console.info(`No instrumentation provider specified. Skipping...`); return; } - switch (MONITORING_PROVIDER) { + switch (process.env.MONITORING_PROVIDER as InstrumentationProvider) { case InstrumentationProvider.Baselime: { const { registerInstrumentation } = await import( '@kit/baselime/instrumentation' @@ -40,7 +32,7 @@ export async function registerMonitoringInstrumentation() { default: throw new Error( - `Unknown instrumentation provider: ${MONITORING_PROVIDER as string}`, + `Unknown instrumentation provider: ${process.env.MONITORING_PROVIDER}`, ); } }