Refactor MonitorResourceUtil; improve error handling and streamline logging statements

This commit is contained in:
Simon Larsen 2024-09-26 15:18:21 +01:00
parent ed7a693966
commit b79dafc9df
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA

View File

@ -131,8 +131,7 @@ export default class MonitorResourceUtil {
// get last log. We do this because there are many monitoring steps and we need to store those. // get last log. We do this because there are many monitoring steps and we need to store those.
logger.debug( logger.debug(
`${dataToProcess.monitorId.toString()} - monitor type ${ `${dataToProcess.monitorId.toString()} - monitor type ${monitor.monitorType
monitor.monitorType
}`, }`,
); );
@ -237,11 +236,18 @@ export default class MonitorResourceUtil {
`${dataToProcess.monitorId.toString()} - Saving monitor metrics`, `${dataToProcess.monitorId.toString()} - Saving monitor metrics`,
); );
await this.saveMonitorMetrics({ try {
monitorId: monitor.id!,
projectId: monitor.projectId!, await this.saveMonitorMetrics({
dataToProcess: dataToProcess, monitorId: monitor.id!,
}); projectId: monitor.projectId!,
dataToProcess: dataToProcess,
});
} catch (err) {
logger.error("Unable to save metrics");
logger.error(err);
}
logger.debug( logger.debug(
`${dataToProcess.monitorId.toString()} - Monitor metrics saved`, `${dataToProcess.monitorId.toString()} - Monitor metrics saved`,
@ -291,7 +297,7 @@ export default class MonitorResourceUtil {
if (incidentTemplate.autoResolveIncident) { if (incidentTemplate.autoResolveIncident) {
if ( if (
!autoResolveCriteriaInstanceIdIncidentIdsDictionary[ !autoResolveCriteriaInstanceIdIncidentIdsDictionary[
criteriaInstance.data.id.toString() criteriaInstance.data.id.toString()
] ]
) { ) {
autoResolveCriteriaInstanceIdIncidentIdsDictionary[ autoResolveCriteriaInstanceIdIncidentIdsDictionary[
@ -361,13 +367,11 @@ export default class MonitorResourceUtil {
if (response.criteriaMetId && response.rootCause) { if (response.criteriaMetId && response.rootCause) {
logger.debug( logger.debug(
`${dataToProcess.monitorId.toString()} - Criteria met: ${ `${dataToProcess.monitorId.toString()} - Criteria met: ${response.criteriaMetId
response.criteriaMetId
}`, }`,
); );
logger.debug( logger.debug(
`${dataToProcess.monitorId.toString()} - Root cause: ${ `${dataToProcess.monitorId.toString()} - Root cause: ${response.rootCause
response.rootCause
}`, }`,
); );
@ -407,7 +411,7 @@ export default class MonitorResourceUtil {
!response.criteriaMetId && !response.criteriaMetId &&
monitorSteps.data.defaultMonitorStatusId && monitorSteps.data.defaultMonitorStatusId &&
monitor.currentMonitorStatusId?.toString() !== monitor.currentMonitorStatusId?.toString() !==
monitorSteps.data.defaultMonitorStatusId.toString() monitorSteps.data.defaultMonitorStatusId.toString()
) { ) {
logger.debug( logger.debug(
`${dataToProcess.monitorId.toString()} - No criteria met. Change to default status.`, `${dataToProcess.monitorId.toString()} - No criteria met. Change to default status.`,
@ -444,7 +448,7 @@ export default class MonitorResourceUtil {
lastMonitorStatusTimeline && lastMonitorStatusTimeline &&
lastMonitorStatusTimeline.monitorStatusId && lastMonitorStatusTimeline.monitorStatusId &&
lastMonitorStatusTimeline.monitorStatusId.toString() === lastMonitorStatusTimeline.monitorStatusId.toString() ===
monitorSteps.data.defaultMonitorStatusId.toString() monitorSteps.data.defaultMonitorStatusId.toString()
) { ) {
// status is same as last status. do not create new status timeline. // status is same as last status. do not create new status timeline.
// do nothing! status is same as last status. // do nothing! status is same as last status.
@ -755,7 +759,7 @@ export default class MonitorResourceUtil {
input.criteriaInstance.data?.changeMonitorStatus && input.criteriaInstance.data?.changeMonitorStatus &&
input.criteriaInstance.data?.monitorStatusId && input.criteriaInstance.data?.monitorStatusId &&
input.criteriaInstance.data?.monitorStatusId.toString() !== input.criteriaInstance.data?.monitorStatusId.toString() !==
input.monitor.currentMonitorStatusId?.toString() input.monitor.currentMonitorStatusId?.toString()
) { ) {
logger.debug( logger.debug(
`${input.monitor.id?.toString()} - Change monitor status to ${input.criteriaInstance.data?.monitorStatusId.toString()}`, `${input.monitor.id?.toString()} - Change monitor status to ${input.criteriaInstance.data?.monitorStatusId.toString()}`,
@ -792,7 +796,7 @@ export default class MonitorResourceUtil {
lastMonitorStatusTimeline && lastMonitorStatusTimeline &&
lastMonitorStatusTimeline.monitorStatusId && lastMonitorStatusTimeline.monitorStatusId &&
lastMonitorStatusTimeline.monitorStatusId.toString() === lastMonitorStatusTimeline.monitorStatusId.toString() ===
monitorStatusId.toString() monitorStatusId.toString()
) { ) {
// status is same as last status. do not create new status timeline. // status is same as last status. do not create new status timeline.
return; return;
@ -840,9 +844,9 @@ export default class MonitorResourceUtil {
(incident: Incident) => { (incident: Incident) => {
return ( return (
incident.createdCriteriaId === incident.createdCriteriaId ===
input.criteriaInstance.data?.id.toString() && input.criteriaInstance.data?.id.toString() &&
incident.createdIncidentTemplateId === incident.createdIncidentTemplateId ===
criteriaIncident.id.toString() criteriaIncident.id.toString()
); );
}, },
); );
@ -951,9 +955,9 @@ export default class MonitorResourceUtil {
openIncident: Incident; openIncident: Incident;
rootCause: string; rootCause: string;
dataToProcess: dataToProcess:
| ProbeMonitorResponse | ProbeMonitorResponse
| IncomingMonitorRequest | IncomingMonitorRequest
| DataToProcess; | DataToProcess;
}): Promise<void> { }): Promise<void> {
const resolvedStateId: ObjectID = const resolvedStateId: ObjectID =
await IncidentStateTimelineService.getResolvedStateIdForProject( await IncidentStateTimelineService.getResolvedStateIdForProject(
@ -1013,7 +1017,7 @@ export default class MonitorResourceUtil {
if ( if (
input.autoResolveCriteriaInstanceIdIncidentIdsDictionary[ input.autoResolveCriteriaInstanceIdIncidentIdsDictionary[
input.openIncident.createdCriteriaId?.toString() input.openIncident.createdCriteriaId?.toString()
] ]
) { ) {
if ( if (