Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/src/Hooks/useMonitorForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const getBaseDefaults = (data?: Monitor | null) => ({
description: data?.description || "",
interval: data?.interval || 60000,
notifications: data?.notifications || [],
escalationDelayMinutes: data?.escalationDelayMinutes ?? 0,
escalationChannels: data?.escalationChannels || [],
statusWindowSize: data?.statusWindowSize || 5,
statusWindowThreshold: data?.statusWindowThreshold || 60,
geoCheckEnabled: data?.geoCheckEnabled ?? false,
Expand Down
88 changes: 88 additions & 0 deletions client/src/Pages/CreateMonitor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,94 @@ const CreateMonitorPage = () => {
}
/>

<ConfigBox
title={t("pages.createMonitor.form.escalation.title")}
subtitle={t("pages.createMonitor.form.escalation.description")}
rightContent={
<Stack spacing={theme.spacing(LAYOUT.MD)}>
<Controller
name="escalationDelayMinutes"
control={control}
render={({ field, fieldState }) => (
<TextField
{...field}
value={field.value ?? 0}
onChange={(e) => field.onChange(Number(e.target.value))}
type="number"
fieldLabel={t(
"pages.createMonitor.form.escalation.option.delayMinutes.label"
)}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/>
<Controller
name="escalationChannels"
control={control}
render={({ field }) => {
const notificationOptions = (notifications ?? []).map((n) => ({
...n,
name: n.notificationName,
}));
const selectedEscalationChannels = notificationOptions.filter((n) =>
(field.value ?? []).includes(n.id)
);
return (
<Stack spacing={theme.spacing(LAYOUT.MD)}>
<Autocomplete
multiple
options={notificationOptions}
value={selectedEscalationChannels}
getOptionLabel={(option) => option.name}
onChange={(_: unknown, newValue: typeof notificationOptions) => {
field.onChange(newValue.map((n) => n.id));
}}
isOptionEqualToValue={(option, value) => option.id === value.id}
fieldLabel={t(
"pages.createMonitor.form.escalation.option.channels.label"
)}
/>
{selectedEscalationChannels.length > 0 && (
<Stack flex={1} width="100%">
{selectedEscalationChannels.map((notification, index) => (
<Stack
direction="row"
alignItems="center"
key={notification.id}
width="100%"
>
<Typography flexGrow={1}>
{notification.notificationName}
</Typography>
<IconButton
size="small"
onClick={() => {
field.onChange(
(field.value ?? []).filter(
(id: string) => id !== notification.id
)
);
}}
aria-label="Remove escalation channel"
>
<Trash2 size={16} />
</IconButton>
{index < selectedEscalationChannels.length - 1 && (
<Divider />
)}
</Stack>
))}
</Stack>
)}
</Stack>
);
}}
/>
</Stack>
}
/>
{(watchedType === "http" ||
watchedType === "grpc" ||
watchedType === "websocket") && (
Expand Down
2 changes: 2 additions & 0 deletions client/src/Types/Monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export interface Monitor {
interval: number;
uptimePercentage?: number;
notifications: string[];
escalationDelayMinutes?: number;
escalationChannels?: string[];
secret?: string;
cpuAlertThreshold: number;
cpuAlertCounter: number;
Expand Down
2 changes: 2 additions & 0 deletions client/src/Validation/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const baseSchema = z.object({
description: z.string().optional(),
interval: z.number().min(15000, "Interval must be at least 15 seconds"),
notifications: z.array(z.string()),
escalationDelayMinutes: z.number().min(0).optional(),
escalationChannels: z.array(z.string()).optional(),
statusWindowSize: z
.number({ message: "Status window size is required" })
.min(1, "Status window size must be at least 1")
Expand Down
12 changes: 12 additions & 0 deletions client/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,18 @@
"description": "Select the notification channels you want to use",
"title": "Notifications"
},
"escalation": {
"title": "Escalation Rules",
"description": "If the monitor stays down for the specified time, notify additional channels.",
"option": {
"delayMinutes": {
"label": "Escalate after (minutes)"
},
"channels": {
"label": "Escalation notification channels"
}
}
},
"type": {
"description": "Select the type of check to perform",
"optionDockerDescription": "Use Docker to monitor if a container is running.",
Expand Down
13 changes: 12 additions & 1 deletion server/src/db/models/Monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ type CheckSnapshotDocument = Omit<CheckSnapshot, "createdAt"> & { createdAt: Dat

type MonitorDocumentBase = Omit<
Monitor,
"id" | "userId" | "teamId" | "notifications" | "selectedDisks" | "statusWindow" | "recentChecks" | "createdAt" | "updatedAt"
"id" | "userId" | "teamId" | "notifications" | "selectedDisks" | "statusWindow" | "recentChecks" | "createdAt" | "updatedAt"
> & {
statusWindow: boolean[];
recentChecks: CheckSnapshotDocument[];
notifications: Types.ObjectId[];
escalationChannels: Types.ObjectId[];
selectedDisks: string[];
matchMethod?: MonitorMatchMethod;
};
Expand Down Expand Up @@ -284,6 +285,16 @@ const MonitorSchema = new Schema<MonitorDocument>(
ref: "Notification",
},
],
escalationDelayMinutes: {
type: Number,
default: 0,
},
escalationChannels: [
{
type: Schema.Types.ObjectId,
ref: "Notification",
},
],
secret: {
type: String,
},
Expand Down
6 changes: 6 additions & 0 deletions server/src/repositories/monitors/MongoMonitorsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ class MongoMonitorsRepository implements IMonitorsRepository {
};

const notificationIds = (doc.notifications ?? []).map((notification) => toStringId(notification));
const escalationChannelIds = (doc.escalationChannels ?? []).map((id) => toStringId(id));

return {
id: toStringId(doc._id),
Expand All @@ -374,6 +375,8 @@ class MongoMonitorsRepository implements IMonitorsRepository {
interval: doc.interval,
uptimePercentage: doc.uptimePercentage ?? undefined,
notifications: notificationIds,
escalationDelayMinutes: doc.escalationDelayMinutes ?? 0,
escalationChannels: escalationChannelIds,
secret: doc.secret ?? undefined,
cpuAlertThreshold: doc.cpuAlertThreshold,
cpuAlertCounter: doc.cpuAlertCounter,
Expand Down Expand Up @@ -410,6 +413,7 @@ class MongoMonitorsRepository implements IMonitorsRepository {
};

const notificationIds = (doc.notifications ?? []).map((notification: unknown) => toStringId(notification));
const escalationChannelIds = (doc.escalationChannels ?? []).map((id: unknown) => toStringId(id));

return {
id: toStringId(doc._id),
Expand All @@ -433,6 +437,8 @@ class MongoMonitorsRepository implements IMonitorsRepository {
interval: doc.interval,
uptimePercentage: doc.uptimePercentage ?? undefined,
notifications: notificationIds,
escalationDelayMinutes: doc.escalationDelayMinutes ?? 0,
escalationChannels: escalationChannelIds,
secret: doc.secret ?? undefined,
cpuAlertThreshold: doc.cpuAlertThreshold,
cpuAlertCounter: doc.cpuAlertCounter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,43 @@ export class SuperSimpleQueueHelper implements ISuperSimpleQueueHelper {
stack: error instanceof Error ? error.stack : undefined,
});
});

//Step 8. Schedule escalation notification if monitor is down and escalation is configured
if (
decision.shouldCreateIncident &&
monitor.escalationChannels &&
monitor.escalationChannels.length > 0 &&
monitor.escalationDelayMinutes &&
monitor.escalationDelayMinutes > 0
) {
const delayMs = monitor.escalationDelayMinutes * 60 * 1000;
setTimeout(async () => {
try {
const currentMonitor = await this.monitorsRepository.findById(monitor.id, monitor.teamId);
if (currentMonitor.status === "down" || currentMonitor.status === "breached") {
await this.notificationsService.handleEscalationNotifications(currentMonitor, status, decision);
this.logger.info({
message: `Escalation notifications sent for monitor ${monitor.id} after ${monitor.escalationDelayMinutes} minutes`,
service: SERVICE_NAME,
method: "getMonitorJob",
});
} else {
this.logger.debug({
message: `Monitor ${monitor.id} recovered before escalation delay, skipping escalation`,
service: SERVICE_NAME,
method: "getMonitorJob",
});
}
} catch (error: unknown) {
this.logger.error({
message: `Error sending escalation notification for monitor ${monitor.id}: ${error instanceof Error ? error.message : "Unknown error"}`,
service: SERVICE_NAME,
method: "getMonitorJob",
stack: error instanceof Error ? error.stack : undefined,
});
}
}, delayMs);
}
} catch (error: unknown) {
this.logger.warn({
message: error instanceof Error ? error.message : "Unknown error",
Expand Down
36 changes: 35 additions & 1 deletion server/src/service/infrastructure/notificationsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface INotificationsService {
updateById(id: string, teamId: string, updateData: Partial<Notification>): Promise<Notification>;
deleteById: (id: string, teamId: string) => Promise<Notification>;
handleNotifications: (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse, decision: MonitorActionDecision) => Promise<boolean>;

handleEscalationNotifications: (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse, decision: MonitorActionDecision) => Promise<boolean>;
sendTestNotification: (notification: Partial<Notification>) => Promise<boolean>;
testAllNotifications: (notificationIds: string[]) => Promise<boolean>;
}
Expand Down Expand Up @@ -197,4 +197,38 @@ export class NotificationsService implements INotificationsService {
await this.monitorsRepository.removeNotificationFromMonitors(id);
return deleted;
};

private sendEscalationNotifications = async (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse, decision: MonitorActionDecision) => {
const escalationChannelIds = monitor.escalationChannels ?? [];
if (escalationChannelIds.length === 0) {
return false;
}

const notifications = await this.notificationsRepository.findNotificationsByIds(escalationChannelIds);

const settings = this.settingsService.getSettings();
const clientHost = settings.clientHost || "Host not defined";
const notificationMessage = this.notificationMessageBuilder.buildMessage(monitor, monitorStatusResponse, decision, clientHost);

const tasks = notifications.map((notification) => this.send(notification, monitor, monitorStatusResponse, decision, notificationMessage));

const outcomes = await Promise.all(tasks);
const succeeded = outcomes.filter(Boolean).length;
const failed = outcomes.length - succeeded;
if (failed > 0) {
this.logger.warn({
message: `Escalation notification send completed with ${succeeded} success, ${failed} failure(s)`,
service: SERVICE_NAME,
method: "sendEscalationNotifications",
});
}
return succeeded === notifications.length;
};

handleEscalationNotifications = async (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse, decision: MonitorActionDecision) => {
if (!decision.shouldSendNotification) {
return false;
}
return await this.sendEscalationNotifications(monitor, monitorStatusResponse, decision);
};
}
2 changes: 2 additions & 0 deletions server/src/types/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export interface Monitor {
interval: number;
uptimePercentage?: number;
notifications: string[];
escalationDelayMinutes?: number;
escalationChannels?: string[];
secret?: string;
cpuAlertThreshold: number;
cpuAlertCounter: number;
Expand Down
4 changes: 4 additions & 0 deletions server/src/validation/monitorValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export const createMonitorBodyValidation = z.object({
diskAlertThreshold: z.number().optional(),
tempAlertThreshold: z.number().optional(),
notifications: z.array(z.string()).optional(),
escalationDelayMinutes: z.number().min(0).default(0),
escalationChannels: z.array(z.string()).optional(),
secret: z.string().optional(),
jsonPath: z.union([z.string(), z.literal("")]).optional(),
expectedValue: z.union([z.string(), z.literal("")]).optional(),
Expand All @@ -89,6 +91,8 @@ export const editMonitorBodyValidation = z.object({
description: z.union([z.string(), z.literal("")]).optional(),
interval: z.number().optional(),
notifications: z.array(z.string()).optional(),
escalationDelayMinutes: z.number().min(0).optional(),
escalationChannels: z.array(z.string()).optional(),
secret: z.string().optional(),
ignoreTlsErrors: z.boolean().optional(),
useAdvancedMatching: z.boolean().optional(),
Expand Down