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

@@ -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?.();
}
}