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:
@@ -22,6 +22,9 @@ export function useBaselime(): MonitoringService {
|
||||
captureEvent<Extra extends object>(event: string, extra?: Extra) {
|
||||
return sendEvent(event, extra);
|
||||
},
|
||||
ready() {
|
||||
return Promise.resolve();
|
||||
}
|
||||
} satisfies MonitoringService;
|
||||
}, [captureException, sendEvent, setUser]);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import { MonitoringService } from '@kit/monitoring-core';
|
||||
const apiKey = z
|
||||
.string({
|
||||
required_error: 'NEXT_PUBLIC_BASELIME_KEY is required',
|
||||
description: 'The Baseline API key',
|
||||
})
|
||||
.parse(process.env.NEXT_PUBLIC_BASELIME_KEY);
|
||||
|
||||
@@ -99,6 +100,10 @@ export class BaselimeServerMonitoringService implements MonitoringService {
|
||||
identifyUser<Info extends { id: string }>(info: Info) {
|
||||
this.userId = info.id;
|
||||
}
|
||||
|
||||
ready() {
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
function getFormattedError(error: Error) {
|
||||
|
||||
@@ -14,4 +14,8 @@ export class ConsoleMonitoringService implements MonitoringService {
|
||||
captureEvent(event: string) {
|
||||
console.log(`[Console Monitoring] Captured event: ${event}`);
|
||||
}
|
||||
|
||||
ready() {
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,4 +29,9 @@ export abstract class MonitoringService {
|
||||
* @param info
|
||||
*/
|
||||
abstract identifyUser<Info extends { id: string }>(info: Info): unknown;
|
||||
|
||||
/**
|
||||
* Wait for the monitoring service to be ready
|
||||
*/
|
||||
abstract ready(): Promise<unknown>;
|
||||
}
|
||||
|
||||
@@ -8,10 +8,21 @@ import { MonitoringService } from '@kit/monitoring-core';
|
||||
* ServerSentryMonitoringService is responsible for capturing exceptions and identifying users using the Sentry monitoring service.
|
||||
*/
|
||||
export class SentryMonitoringService implements MonitoringService {
|
||||
private readonly readyPromise: Promise<unknown>;
|
||||
private readyResolver?: (value?: unknown) => void;
|
||||
|
||||
constructor() {
|
||||
this.readyPromise = new Promise(
|
||||
(resolve) => (this.readyResolver = resolve),
|
||||
);
|
||||
|
||||
void this.initialize();
|
||||
}
|
||||
|
||||
async ready() {
|
||||
return this.readyPromise;
|
||||
}
|
||||
|
||||
captureException(error: Error | null) {
|
||||
return Sentry.captureException(error);
|
||||
}
|
||||
@@ -27,17 +38,21 @@ export class SentryMonitoringService implements MonitoringService {
|
||||
Sentry.setUser(user);
|
||||
}
|
||||
|
||||
private initialize() {
|
||||
return this.initializeIfBrowser();
|
||||
}
|
||||
|
||||
private async initializeIfBrowser() {
|
||||
private async initialize() {
|
||||
if (typeof document !== 'undefined') {
|
||||
const { initializeSentryBrowserClient } = await import(
|
||||
'../sentry.client.config'
|
||||
);
|
||||
|
||||
initializeSentryBrowserClient();
|
||||
} else {
|
||||
const { initializeSentryServerClient } = await import(
|
||||
'../sentry.server.config'
|
||||
);
|
||||
|
||||
initializeSentryServerClient();
|
||||
}
|
||||
|
||||
this.readyResolver?.();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user