Add ready method to monitoring services

Implemented a ready method for all monitoring services to standardize initialization readiness checks. Updated initialization logic in SentryMonitoringService to determine environment and invoke appropriate client initialization.
This commit is contained in:
gbuomprisco
2024-08-13 12:56:51 +02:00
parent 29e23c837d
commit 2d6d95d72a
6 changed files with 39 additions and 5 deletions

View File

@@ -22,6 +22,9 @@ export function useBaselime(): MonitoringService {
captureEvent<Extra extends object>(event: string, extra?: Extra) { captureEvent<Extra extends object>(event: string, extra?: Extra) {
return sendEvent(event, extra); return sendEvent(event, extra);
}, },
ready() {
return Promise.resolve();
}
} satisfies MonitoringService; } satisfies MonitoringService;
}, [captureException, sendEvent, setUser]); }, [captureException, sendEvent, setUser]);
} }

View File

@@ -5,6 +5,7 @@ import { MonitoringService } from '@kit/monitoring-core';
const apiKey = z const apiKey = z
.string({ .string({
required_error: 'NEXT_PUBLIC_BASELIME_KEY is required', required_error: 'NEXT_PUBLIC_BASELIME_KEY is required',
description: 'The Baseline API key',
}) })
.parse(process.env.NEXT_PUBLIC_BASELIME_KEY); .parse(process.env.NEXT_PUBLIC_BASELIME_KEY);
@@ -99,6 +100,10 @@ export class BaselimeServerMonitoringService implements MonitoringService {
identifyUser<Info extends { id: string }>(info: Info) { identifyUser<Info extends { id: string }>(info: Info) {
this.userId = info.id; this.userId = info.id;
} }
ready() {
return Promise.resolve();
}
} }
function getFormattedError(error: Error) { function getFormattedError(error: Error) {

View File

@@ -14,4 +14,8 @@ export class ConsoleMonitoringService implements MonitoringService {
captureEvent(event: string) { captureEvent(event: string) {
console.log(`[Console Monitoring] Captured event: ${event}`); console.log(`[Console Monitoring] Captured event: ${event}`);
} }
ready() {
return Promise.resolve();
}
} }

View File

@@ -29,4 +29,9 @@ export abstract class MonitoringService {
* @param info * @param info
*/ */
abstract identifyUser<Info extends { id: string }>(info: Info): unknown; abstract identifyUser<Info extends { id: string }>(info: Info): unknown;
/**
* Wait for the monitoring service to be ready
*/
abstract ready(): Promise<unknown>;
} }

View File

@@ -8,10 +8,21 @@ import { MonitoringService } from '@kit/monitoring-core';
* ServerSentryMonitoringService is responsible for capturing exceptions and identifying users using the Sentry monitoring service. * ServerSentryMonitoringService is responsible for capturing exceptions and identifying users using the Sentry monitoring service.
*/ */
export class SentryMonitoringService implements MonitoringService { export class SentryMonitoringService implements MonitoringService {
private readonly readyPromise: Promise<unknown>;
private readyResolver?: (value?: unknown) => void;
constructor() { constructor() {
this.readyPromise = new Promise(
(resolve) => (this.readyResolver = resolve),
);
void this.initialize(); void this.initialize();
} }
async ready() {
return this.readyPromise;
}
captureException(error: Error | null) { captureException(error: Error | null) {
return Sentry.captureException(error); return Sentry.captureException(error);
} }
@@ -27,17 +38,21 @@ export class SentryMonitoringService implements MonitoringService {
Sentry.setUser(user); Sentry.setUser(user);
} }
private initialize() { private async initialize() {
return this.initializeIfBrowser();
}
private async initializeIfBrowser() {
if (typeof document !== 'undefined') { if (typeof document !== 'undefined') {
const { initializeSentryBrowserClient } = await import( const { initializeSentryBrowserClient } = await import(
'../sentry.client.config' '../sentry.client.config'
); );
initializeSentryBrowserClient(); initializeSentryBrowserClient();
} else {
const { initializeSentryServerClient } = await import(
'../sentry.server.config'
);
initializeSentryServerClient();
} }
this.readyResolver?.();
} }
} }

View File

@@ -18,6 +18,8 @@ export async function captureException(exception: unknown) {
const service = await getServerMonitoringService(); const service = await getServerMonitoringService();
await service.ready();
const error = const error =
exception instanceof Error ? exception : new Error(exception as string); exception instanceof Error ? exception : new Error(exception as string);