Refactor monitoring services into separate packages
Separated and isolated the responsibilities of monitoring tools. Reorganized the code by introducing a core package that contains common code related to monitoring and moved all the service operations like error capturing and identification of users into their respective packages. This ensures each tool is independent and easy to maintain.
This commit is contained in:
35
packages/monitoring/api/src/components/error-boundary.tsx
Normal file
35
packages/monitoring/api/src/components/error-boundary.tsx
Normal file
@@ -0,0 +1,35 @@
|
||||
import type { ErrorInfo, ReactNode } from 'react';
|
||||
import { Component } from 'react';
|
||||
|
||||
interface Props {
|
||||
onError?: (error: Error, info: ErrorInfo) => void;
|
||||
fallback: ReactNode;
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
export class ErrorBoundary extends Component<Props> {
|
||||
readonly state = { hasError: false, error: null };
|
||||
|
||||
constructor(props: Props) {
|
||||
super(props);
|
||||
}
|
||||
|
||||
static getDerivedStateFromError(error: unknown) {
|
||||
return {
|
||||
hasError: true,
|
||||
error,
|
||||
};
|
||||
}
|
||||
|
||||
componentDidCatch(error: Error, info: ErrorInfo) {
|
||||
this.props.onError?.(error, info);
|
||||
}
|
||||
|
||||
render() {
|
||||
if (this.state.hasError) {
|
||||
return this.props.fallback;
|
||||
}
|
||||
|
||||
return this.props.children;
|
||||
}
|
||||
}
|
||||
2
packages/monitoring/api/src/components/index.ts
Normal file
2
packages/monitoring/api/src/components/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './error-boundary';
|
||||
export * from './provider';
|
||||
52
packages/monitoring/api/src/components/provider.tsx
Normal file
52
packages/monitoring/api/src/components/provider.tsx
Normal file
@@ -0,0 +1,52 @@
|
||||
'use client';
|
||||
|
||||
import { lazy } from 'react';
|
||||
|
||||
import { getMonitoringProvider } from '../get-monitoring-provider';
|
||||
import { InstrumentationProvider } from '../monitoring-providers.enum';
|
||||
|
||||
const BaselimeProvider = lazy(async () => {
|
||||
const { BaselimeProvider } = await import('@kit/baselime/provider');
|
||||
|
||||
return {
|
||||
default: BaselimeProvider,
|
||||
};
|
||||
});
|
||||
|
||||
const SentryProvider = lazy(async () => {
|
||||
const { SentryProvider } = await import('@kit/sentry/provider');
|
||||
|
||||
return {
|
||||
default: SentryProvider,
|
||||
};
|
||||
});
|
||||
|
||||
type Config = {
|
||||
provider: InstrumentationProvider;
|
||||
providerToken: string;
|
||||
};
|
||||
|
||||
export function MonitoringProvider(
|
||||
props: React.PropsWithChildren<{ config?: Config }>,
|
||||
) {
|
||||
const provider = getMonitoringProvider();
|
||||
|
||||
if (!props.config) {
|
||||
return <>{props.children}</>;
|
||||
}
|
||||
|
||||
switch (provider) {
|
||||
case InstrumentationProvider.Baselime:
|
||||
return (
|
||||
<BaselimeProvider apiKey={props.config?.providerToken} enableWebVitals>
|
||||
{props.children}
|
||||
</BaselimeProvider>
|
||||
);
|
||||
|
||||
case InstrumentationProvider.Sentry:
|
||||
return <SentryProvider>{props.children}</SentryProvider>;
|
||||
|
||||
default:
|
||||
return <>{props.children}</>;
|
||||
}
|
||||
}
|
||||
7
packages/monitoring/api/src/get-monitoring-provider.ts
Normal file
7
packages/monitoring/api/src/get-monitoring-provider.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { InstrumentationProvider } from './monitoring-providers.enum';
|
||||
|
||||
export function getMonitoringProvider() {
|
||||
return process.env.NEXT_PUBLIC_MONITORING_PROVIDER as
|
||||
| InstrumentationProvider
|
||||
| undefined;
|
||||
}
|
||||
2
packages/monitoring/api/src/hooks/index.ts
Normal file
2
packages/monitoring/api/src/hooks/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './use-monitoring';
|
||||
export * from './use-capture-exception';
|
||||
@@ -0,0 +1,7 @@
|
||||
import { useMonitoring } from './use-monitoring';
|
||||
|
||||
export function useCaptureException(error: Error) {
|
||||
const service = useMonitoring();
|
||||
|
||||
return service.captureException(error);
|
||||
}
|
||||
12
packages/monitoring/api/src/hooks/use-monitoring.ts
Normal file
12
packages/monitoring/api/src/hooks/use-monitoring.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { useContext } from 'react';
|
||||
|
||||
import { MonitoringContext } from '@kit/monitoring-core';
|
||||
|
||||
/**
|
||||
* @name useMonitoring
|
||||
* @description Asynchronously load the monitoring service based on the MONITORING_PROVIDER environment variable.
|
||||
* Use Suspense to suspend while loading the service.
|
||||
*/
|
||||
export function useMonitoring() {
|
||||
return useContext(MonitoringContext);
|
||||
}
|
||||
41
packages/monitoring/api/src/instrumentation.ts
Normal file
41
packages/monitoring/api/src/instrumentation.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { getMonitoringProvider } from './get-monitoring-provider';
|
||||
import { InstrumentationProvider } from './monitoring-providers.enum';
|
||||
|
||||
const PROVIDER = getMonitoringProvider();
|
||||
|
||||
/**
|
||||
* @name registerMonitoringInstrumentation
|
||||
* @description Register monitoring instrumentation based on the MONITORING_PROVIDER environment variable.
|
||||
*
|
||||
* Please set the MONITORING_PROVIDER environment variable to register the monitoring instrumentation provider.
|
||||
*/
|
||||
export async function registerMonitoringInstrumentation() {
|
||||
if (!PROVIDER) {
|
||||
console.info(`No instrumentation provider specified. Skipping...`);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
switch (PROVIDER) {
|
||||
case InstrumentationProvider.Baselime: {
|
||||
const { registerInstrumentation } = await import(
|
||||
'@kit/baselime/instrumentation'
|
||||
);
|
||||
|
||||
return registerInstrumentation();
|
||||
}
|
||||
|
||||
case InstrumentationProvider.Sentry: {
|
||||
const { registerInstrumentation } = await import(
|
||||
'@kit/sentry/instrumentation'
|
||||
);
|
||||
|
||||
return registerInstrumentation();
|
||||
}
|
||||
|
||||
default:
|
||||
throw new Error(
|
||||
`Unknown instrumentation provider: ${process.env.MONITORING_PROVIDER}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
4
packages/monitoring/api/src/monitoring-providers.enum.ts
Normal file
4
packages/monitoring/api/src/monitoring-providers.enum.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export enum InstrumentationProvider {
|
||||
Baselime = 'baselime',
|
||||
Sentry = 'sentry',
|
||||
}
|
||||
1
packages/monitoring/api/src/server.ts
Normal file
1
packages/monitoring/api/src/server.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './services/get-server-monitoring-service';
|
||||
@@ -0,0 +1,44 @@
|
||||
import { ConsoleMonitoringService } from '@kit/monitoring-core';
|
||||
|
||||
import { getMonitoringProvider } from '../get-monitoring-provider';
|
||||
import { InstrumentationProvider } from '../monitoring-providers.enum';
|
||||
|
||||
const MONITORING_PROVIDER = getMonitoringProvider();
|
||||
|
||||
/**
|
||||
* @name getServerMonitoringService
|
||||
* @description Get the monitoring service based on the MONITORING_PROVIDER environment variable.
|
||||
*/
|
||||
export async function getServerMonitoringService() {
|
||||
if (!MONITORING_PROVIDER) {
|
||||
console.info(
|
||||
`No instrumentation provider specified. Returning console service...`,
|
||||
);
|
||||
|
||||
return new ConsoleMonitoringService();
|
||||
}
|
||||
|
||||
switch (MONITORING_PROVIDER) {
|
||||
case InstrumentationProvider.Baselime: {
|
||||
const { BaselimeServerMonitoringService } = await import(
|
||||
'@kit/baselime/server'
|
||||
);
|
||||
|
||||
return new BaselimeServerMonitoringService();
|
||||
}
|
||||
|
||||
case InstrumentationProvider.Sentry: {
|
||||
const { SentryServerMonitoringService } = await import(
|
||||
'@kit/sentry/server'
|
||||
);
|
||||
|
||||
return new SentryServerMonitoringService();
|
||||
}
|
||||
|
||||
default: {
|
||||
throw new Error(
|
||||
`Please set the MONITORING_PROVIDER environment variable to register the monitoring instrumentation provider.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user