Add event capturing to monitoring services

Updated the monitoring services - baselime-server and sentry-server, to capture specific events. This includes modifications in their respective fetch methods for logging these events. Also, expanded the core monitoring service to allow for event tracking.
This commit is contained in:
giancarlo
2024-04-25 15:03:20 +07:00
parent a0fe5a3794
commit e8d34ce77e
3 changed files with 54 additions and 1 deletions

View File

@@ -31,7 +31,7 @@ export class BaselimeServerMonitoringService implements MonitoringService {
message: error ? `${error.name}: ${error.message}` : `Unknown error`,
};
const response = await fetch(`https://events.baselime.io/v1/web`, {
const response = await fetch(`https://events.baselime.io/v1/logs`, {
method: 'POST',
headers: {
contentType: 'application/json',
@@ -60,6 +60,42 @@ export class BaselimeServerMonitoringService implements MonitoringService {
}
}
async captureEvent<
Extra extends {
sessionId?: string;
namespace?: string;
service?: string;
},
>(event: string, extra?: Extra) {
const response = await fetch(`https://events.baselime.io/v1/logs`, {
method: 'POST',
headers: {
contentType: 'application/json',
'x-api-key': apiKey,
'x-service': extra?.service ?? '',
'x-namespace': extra?.namespace ?? '',
},
body: JSON.stringify([
{
userId: this.userId,
sessionId: extra?.sessionId,
namespace: extra?.namespace,
message: event,
},
]),
});
if (!response.ok) {
console.error(
{
response,
event,
},
'Failed to send event to Baselime',
);
}
}
identifyUser<Info extends { id: string }>(info: Info) {
this.userId = info.id;
}

View File

@@ -14,6 +14,16 @@ export abstract class MonitoringService {
extra?: Extra,
): unknown;
/**
* Track an event
* @param event
* @param extra
*/
abstract captureEvent<Extra extends object>(
event: string,
extra?: Extra,
): unknown;
/**
* Identify a user in the monitoring service - used for tracking user actions
* @param info

View File

@@ -12,6 +12,13 @@ export class SentryServerMonitoringService implements MonitoringService {
return Sentry.captureException(error);
}
captureEvent<Extra extends Sentry.Event>(event: string, extra?: Extra) {
return Sentry.captureEvent({
message: event,
...(extra ?? {}),
});
}
identifyUser(user: Sentry.User) {
Sentry.setUser(user);
}