Introduce error boundary mechanism and exception capture with Baselime and Sentry

Deleted the ErrorBoundary component from the makerkit package and introduced new exception capture mechanisms for Baselime and Sentry monitoring providers. The code now captures all exceptions thrown within components and sends them to the configured monitoring provider, which in turn logs the errors for debugging purposes. Updated packages and environment variables accordingly to support this feature.
This commit is contained in:
giancarlo
2024-04-15 14:14:08 +08:00
parent bb19d6d207
commit 07ff9a7f8e
31 changed files with 327 additions and 131 deletions

View File

@@ -11,6 +11,7 @@
"prettier": "@kit/prettier-config",
"exports": {
".": "./src/index.ts",
"./instrumentation": "./src/instrumentation.ts",
"./config/client": "./src/config/sentry.client.config.ts",
"./config/server": "./src/config/sentry.server.config.ts",
"./config/edge": "./src/config/sentry.server.edge.ts"

View File

@@ -0,0 +1,5 @@
import * as Sentry from '@sentry/nextjs';
export function captureException(error: Error & { digest?: string }) {
return Sentry.captureException(error);
}

View File

@@ -1,36 +1 @@
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
* @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.
*/
export async function registerSentryInstrumentation() {
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]: INSTRUMENTATION_SERVICE_NAME,
}),
spanProcessor: new SentrySpanProcessor(),
textMapPropagator: new SentryPropagator(),
});
sdk.start();
}
export { captureException } from './capture-exception';

View File

@@ -0,0 +1,36 @@
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
* @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.
*/
export async function registerSentryInstrumentation() {
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]: INSTRUMENTATION_SERVICE_NAME,
}),
spanProcessor: new SentrySpanProcessor(),
textMapPropagator: new SentryPropagator(),
});
sdk.start();
}