Files
myeasycms-v2/packages/monitoring/baselime/src/services/baselime-server-monitoring.service.ts
giancarlo a074e1ec3b Implement Baselime monitoring and update error handling
This commit introduces the integration of Baselime for monitoring, accounting for various error scenarios and improved console error logging. Request handling has been updated to assign unique IDs for each request, aiding in tracing/logs. The environment variable key was updated, and the `MonitoringProvider` was nested in the root providers. In the base monitoring service, a function to format errors for logging was added. The provider logic was updated to create a new instance of service for each request, improving memory efficiency.
2024-04-22 18:41:38 +08:00

76 lines
1.6 KiB
TypeScript

import process from 'node:process';
import { z } from 'zod';
import { MonitoringService } from '../../../src/services/monitoring.service';
const apiKey = z
.string({
required_error: 'API_KEY is required',
})
.parse(process.env.BASELIME_API_KEY);
export class BaselimeServerMonitoringService implements MonitoringService {
userId: string | null = null;
async captureException(
error: Error | null,
extra?: {
requestId?: string;
sessionId?: string;
namespace?: string;
service?: string;
},
) {
const formattedError = error ? getFormattedError(error) : {};
const event = {
level: 'error',
data: { error },
error: {
...formattedError,
},
message: error ? `${error.name}: ${error.message}` : `Unknown error`,
};
const response = await fetch(`https://events.baselime.io/v1/web`, {
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,
...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;
}
}
function getFormattedError(error: Error) {
return {
name: error.name,
message: error.message,
stack: error.stack,
};
}