Files
myeasycms-v2/packages/monitoring/sentry/src/instrumentation.ts
giancarlo f1e3a44c89 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.
2024-04-15 14:41:48 +08:00

37 lines
1.2 KiB
TypeScript

/**
* @name registerInstrumentation
* @description This file is used to register Sentry instrumentation for your Next.js application.
*
* Please set the MONITORING_PROVIDER environment variable to 'sentry' to register Sentry instrumentation.
*/
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');
const { SEMRESATTRS_SERVICE_NAME } = await import(
'@opentelemetry/semantic-conventions'
);
const { SentrySpanProcessor, SentryPropagator } = await import(
'@sentry/opentelemetry-node'
);
const sdk = new NodeSDK({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: serviceName,
}),
spanProcessors: [new SentrySpanProcessor()],
textMapPropagator: new SentryPropagator(),
});
sdk.start();
}