From e75fcfc7500baa37d4036e72989b41763368c47f Mon Sep 17 00:00:00 2001 From: giancarlo Date: Mon, 15 Apr 2024 14:30:23 +0800 Subject: [PATCH] Refactor monitoring instrumentation code across modules The monitoring instrumentation methodology has been simplified for both Baselime and Sentry providers by aligning their registration functions and error handling processes. Specifically, function names have been standardized to 'registerInstrumentation' and handling for the absence of the INSTRUMENTATION_SERVICE_NAME environment variable is now conducted within these functions. In addition, the MONITORING_INSTRUMENTATION_PROVIDER variable has been renamed to MONITORING_PROVIDER. --- apps/web/instrumentation.ts | 3 +-- .../baselime/src/instrumentation.ts | 25 ++++++++++--------- packages/monitoring/sentry/src/index.ts | 2 +- .../monitoring/sentry/src/instrumentation.ts | 24 +++++++++--------- packages/monitoring/src/instrumentation.ts | 8 +++--- 5 files changed, 31 insertions(+), 31 deletions(-) diff --git a/apps/web/instrumentation.ts b/apps/web/instrumentation.ts index 9c4237a0e..954d16634 100644 --- a/apps/web/instrumentation.ts +++ b/apps/web/instrumentation.ts @@ -15,8 +15,7 @@ export async function register() { '@kit/monitoring/instrumentation' ); - // Register monitoring instrumentation based on the - // MONITORING_INSTRUMENTATION_PROVIDER environment variable. + // Register monitoring instrumentation based on the MONITORING_PROVIDER environment variable. return registerMonitoringInstrumentation(); } } diff --git a/packages/monitoring/baselime/src/instrumentation.ts b/packages/monitoring/baselime/src/instrumentation.ts index 590437486..b570cc28a 100644 --- a/packages/monitoring/baselime/src/instrumentation.ts +++ b/packages/monitoring/baselime/src/instrumentation.ts @@ -1,25 +1,26 @@ -const INSTRUMENTATION_SERVICE_NAME = process.env.INSTRUMENTATION_SERVICE_NAME; - -if (!INSTRUMENTATION_SERVICE_NAME) { - throw new Error(` - You have set the Baselime instrumentation provider, but have not set the INSTRUMENTATION_SERVICE_NAME environment variable. Please set the INSTRUMENTATION_SERVICE_NAME environment variable. - `); -} - /** - * @name registerBaselimeInstrumentation + * @name registerInstrumentation * @description This file is used to register Baselime instrumentation for your Next.js application. * - * Please set the MONITORING_INSTRUMENTATION_PROVIDER environment variable to 'baselime' to register Baselime instrumentation. + * Please set the MONITORING_PROVIDER environment variable to 'baselime' to register Baselime instrumentation. */ -export async function registerBaselimeInstrumentation() { +export async function registerInstrumentation() { + const serviceName = process.env.INSTRUMENTATION_SERVICE_NAME; + + if (!serviceName) { + throw new Error(` + You have set the Baselime instrumentation provider, but have not set the INSTRUMENTATION_SERVICE_NAME environment variable. + Please set the INSTRUMENTATION_SERVICE_NAME environment variable. + `); + } + const { BaselimeSDK, BetterHttpInstrumentation, VercelPlugin } = await import( '@baselime/node-opentelemetry' ); const sdk = new BaselimeSDK({ serverless: true, - service: INSTRUMENTATION_SERVICE_NAME, + service: serviceName, instrumentations: [ new BetterHttpInstrumentation({ plugins: [new VercelPlugin()], diff --git a/packages/monitoring/sentry/src/index.ts b/packages/monitoring/sentry/src/index.ts index 7000d7f04..c258b27a8 100644 --- a/packages/monitoring/sentry/src/index.ts +++ b/packages/monitoring/sentry/src/index.ts @@ -1 +1 @@ -export { captureException } from './capture-exception'; +export * from './capture-exception'; diff --git a/packages/monitoring/sentry/src/instrumentation.ts b/packages/monitoring/sentry/src/instrumentation.ts index d2b87772c..141a1ecf2 100644 --- a/packages/monitoring/sentry/src/instrumentation.ts +++ b/packages/monitoring/sentry/src/instrumentation.ts @@ -1,18 +1,18 @@ -const INSTRUMENTATION_SERVICE_NAME = process.env.INSTRUMENTATION_SERVICE_NAME; - -if (!INSTRUMENTATION_SERVICE_NAME) { - throw new Error(` - You have set the Sentry instrumentation provider, but have not set the INSTRUMENTATION_SERVICE_NAME environment variable. Please set the INSTRUMENTATION_SERVICE_NAME environment variable. - `); -} - /** - * @name registerSentryInstrumentation + * @name registerInstrumentation * @description This file is used to register Sentry instrumentation for your Next.js application. * - * Please set the MONITORING_INSTRUMENTATION_PROVIDER environment variable to 'sentry' to register Sentry instrumentation. + * Please set the MONITORING_PROVIDER environment variable to 'sentry' to register Sentry instrumentation. */ -export async function registerSentryInstrumentation() { +export async function registerInstrumentation() { + const serviceName = process.env.INSTRUMENTATION_SERVICE_NAME; + + if (!serviceName) { + throw new Error( + `You have set the Sentry instrumentation provider, but have not set the INSTRUMENTATION_SERVICE_NAME environment variable. Please set the INSTRUMENTATION_SERVICE_NAME environment variable.`, + ); + } + const { Resource } = await import('@opentelemetry/resources'); const { NodeSDK } = await import('@opentelemetry/sdk-node'); @@ -26,7 +26,7 @@ export async function registerSentryInstrumentation() { const sdk = new NodeSDK({ resource: new Resource({ - [SEMRESATTRS_SERVICE_NAME]: INSTRUMENTATION_SERVICE_NAME, + [SEMRESATTRS_SERVICE_NAME]: serviceName, }), spanProcessor: new SentrySpanProcessor(), textMapPropagator: new SentryPropagator(), diff --git a/packages/monitoring/src/instrumentation.ts b/packages/monitoring/src/instrumentation.ts index de4c9b5e4..3cfe70c3b 100644 --- a/packages/monitoring/src/instrumentation.ts +++ b/packages/monitoring/src/instrumentation.ts @@ -23,19 +23,19 @@ export async function registerMonitoringInstrumentation() { switch (MONITORING_PROVIDER) { case InstrumentationProvider.Baselime: { - const { registerBaselimeInstrumentation } = await import( + const { registerInstrumentation } = await import( '@kit/baselime/instrumentation' ); - return registerBaselimeInstrumentation(); + return registerInstrumentation(); } case InstrumentationProvider.Sentry: { - const { registerSentryInstrumentation } = await import( + const { registerInstrumentation } = await import( '@kit/sentry/instrumentation' ); - return registerSentryInstrumentation(); + return registerInstrumentation(); } default: