Skip to content
Open

Main #329

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 @@ -17,6 +17,8 @@ const getBaseDefaults = (data?: Monitor | null) => ({
geoCheckEnabled: data?.geoCheckEnabled ?? false,
geoCheckLocations: data?.geoCheckLocations || [],
geoCheckInterval: data?.geoCheckInterval || 300000,
escalationAfterMinutes: data?.escalationAfterMinutes ?? undefined,
escalationNotificationChannels: data?.escalationNotificationChannels || [],
});

export const useMonitorForm = ({
Expand Down
82 changes: 82 additions & 0 deletions client/src/Pages/CreateMonitor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,88 @@ 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="escalationAfterMinutes"
control={control}
render={({ field }) => (
<TextField
type="number"
label={t("pages.createMonitor.form.escalation.afterMinutes")}
value={field.value ?? ""}
onChange={(e) => {
const val = e.target.value;
field.onChange(val === "" ? undefined : Number(val));
}}
placeholder="e.g. 5"
/>
)}
/>
<Controller
name="escalationNotificationChannels"
control={control}
render={({ field }) => {
const notificationOptions = (notifications ?? []).map((n) => ({
...n,
name: n.notificationName,
}));
const selected = notificationOptions.filter((n) =>
(field.value ?? []).includes(n.id)
);
return (
<Stack spacing={theme.spacing(LAYOUT.MD)}>
<Autocomplete
multiple
options={notificationOptions}
value={selected}
getOptionLabel={(option) => option.name}
onChange={(_: unknown, newValue: typeof notificationOptions) => {
field.onChange(newValue.map((n) => n.id));
}}
isOptionEqualToValue={(option, value) => option.id === value.id}
/>
{selected.length > 0 && (
<Stack flex={1} width="100%">
{selected.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 notification"
>
<Trash2 size={16} />
</IconButton>
{index < selected.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 @@ -76,6 +76,8 @@ export interface Monitor {
geoCheckEnabled?: boolean;
geoCheckLocations?: GeoContinent[];
geoCheckInterval?: number;
escalationAfterMinutes?: number;
escalationNotificationChannels?: string[];
recentChecks: CheckSnapshot[];
createdAt: string;
updatedAt: string;
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 @@ -27,6 +27,8 @@ const baseSchema = z.object({
.number()
.min(300000, "Interval must be at least 5 minutes")
.optional(),
escalationAfterMinutes: z.number().min(0).optional(),
escalationNotificationChannels: z.array(z.string()).optional(),
});

// HTTP monitor schema
Expand Down
5 changes: 5 additions & 0 deletions client/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,11 @@
"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",
"afterMinutes": "Escalate after (minutes)"
},
"type": {
"description": "Select the type of check to perform",
"optionDockerDescription": "Use Docker to monitor if a container is running.",
Expand Down
7 changes: 6 additions & 1 deletion server/src/db/models/Incident.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Schema, model, type Types } from "mongoose";
import { IncidentResolutionTypes, type Incident } from "@/types/incident.js";

type IncidentDocumentBase = Omit<Incident, "id" | "monitorId" | "teamId" | "resolvedBy" | "startTime" | "endTime" | "createdAt" | "updatedAt"> & {
type IncidentDocumentBase = Omit<Incident, "id" | "monitorId" | "teamId" | "resolvedBy" | "startTime" | "endTime" | "escalationSentAt" | "createdAt" | "updatedAt"> & {
monitorId: Types.ObjectId;
teamId: Types.ObjectId;
resolvedBy?: Types.ObjectId | null;
startTime: Date;
endTime: Date | null;
escalationSentAt?: Date | null;
createdAt: Date;
updatedAt: Date;
};
Expand Down Expand Up @@ -72,6 +73,10 @@ const IncidentSchema = new Schema<IncidentDocument>(
type: String,
default: null,
},
escalationSentAt: {
type: Date,
default: null,
},
},
{ timestamps: true }
);
Expand Down
8 changes: 8 additions & 0 deletions server/src/db/models/Monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@ const MonitorSchema = new Schema<MonitorDocument>(
type: Number,
default: 300000,
},
escalationAfterMinutes: {
type: Number,
default: undefined,
},
escalationNotificationChannels: {
type: [String],
default: [],
},
recentChecks: {
type: [checkSnapshotSchema],
default: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class MongoIncidentRepository implements IIncidentsRepository {
resolvedBy: doc.resolvedBy ? this.toStringId(doc.resolvedBy) : null,
resolvedByEmail: doc.resolvedByEmail ?? null,
comment: doc.comment ?? null,
escalationSentAt: doc.escalationSentAt ? this.toDateString(doc.escalationSentAt) : null,
createdAt: this.toDateString(doc.createdAt),
updatedAt: this.toDateString(doc.updatedAt),
};
Expand Down
4 changes: 4 additions & 0 deletions server/src/repositories/monitors/MongoMonitorsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ class MongoMonitorsRepository implements IMonitorsRepository {
geoCheckEnabled: doc.geoCheckEnabled ?? false,
geoCheckLocations: doc.geoCheckLocations ?? [],
geoCheckInterval: doc.geoCheckInterval ?? 300000,
escalationAfterMinutes: doc.escalationAfterMinutes ?? undefined,
escalationNotificationChannels: doc.escalationNotificationChannels ?? [],
createdAt: toDateString(doc.createdAt),
updatedAt: toDateString(doc.updatedAt),
};
Expand Down Expand Up @@ -450,6 +452,8 @@ class MongoMonitorsRepository implements IMonitorsRepository {
geoCheckEnabled: doc.geoCheckEnabled ?? false,
geoCheckLocations: doc.geoCheckLocations ?? [],
geoCheckInterval: doc.geoCheckInterval ?? 300000,
escalationAfterMinutes: doc.escalationAfterMinutes ?? undefined,
escalationNotificationChannels: doc.escalationNotificationChannels ?? [],
createdAt: toDateString(doc.createdAt),
updatedAt: toDateString(doc.updatedAt),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
type IGeoChecksService,
} from "@/service/index.js";
import { CHECK_TTL_SENTINEL, type MaintenanceWindow, type StatusChangeResult } from "@/types/index.js";
import type { MonitorStatusResponse } from "@/types/network.js";
import {
IMaintenanceWindowsRepository,
IMonitorsRepository,
Expand All @@ -38,7 +39,7 @@ export interface MonitorActionDecision {
shouldResolveIncident: boolean;
shouldSendNotification: boolean;
incidentReason: "status_down" | "threshold_breach" | null;
notificationReason: "status_change" | "threshold_breach" | null;
notificationReason: "status_change" | "threshold_breach" | "escalation" | null;
thresholdBreaches?: {
cpu?: boolean;
memory?: boolean;
Expand Down Expand Up @@ -177,6 +178,15 @@ export class SuperSimpleQueueHelper implements ISuperSimpleQueueHelper {
stack: error instanceof Error ? error.stack : undefined,
});
});

this.checkEscalation(statusChangeResult.monitor, status).catch((error: unknown) => {
this.logger.warn({
message: `Error checking escalation for monitor ${monitor.id}: ${error instanceof Error ? error.message : "Unknown error"}`,
service: SERVICE_NAME,
method: "getMonitorJob",
stack: error instanceof Error ? error.stack : undefined,
});
});
} catch (error: unknown) {
this.logger.warn({
message: error instanceof Error ? error.message : "Unknown error",
Expand Down Expand Up @@ -418,6 +428,49 @@ export class SuperSimpleQueueHelper implements ISuperSimpleQueueHelper {
};
};

private async checkEscalation(monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) {
if (monitor.status !== "down" && monitor.status !== "breached") {
return;
}

if (!monitor.escalationAfterMinutes || !monitor.escalationNotificationChannels?.length) {
return;
}

const incident = await this.incidentsRepository.findActiveByMonitorId(monitor.id, monitor.teamId);
if (!incident) {
return;
}

if (incident.escalationSentAt) {
return;
}

const incidentStart = new Date(incident.startTime).getTime();
const elapsed = (Date.now() - incidentStart) / 60000;

if (elapsed < monitor.escalationAfterMinutes) {
return;
}

const escalationDecision: MonitorActionDecision = {
shouldCreateIncident: false,
shouldResolveIncident: false,
shouldSendNotification: true,
incidentReason: null,
notificationReason: "escalation",
};

await this.notificationsService.sendEscalationNotifications(
monitor,
monitorStatusResponse,
escalationDecision,
monitor.escalationNotificationChannels
);

await this.incidentsRepository.updateById(incident.id, monitor.teamId, { escalationSentAt: new Date().toISOString() });
}

private evaluateMonitorAction(statusChangeResult: StatusChangeResult): MonitorActionDecision {
const { monitor, statusChanged, prevStatus } = statusChangeResult;

Expand Down
21 changes: 20 additions & 1 deletion server/src/service/infrastructure/notificationMessageBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export class NotificationMessageBuilder implements INotificationMessageBuilder {
}

private determineNotificationType(decision: MonitorActionDecision, monitor: Monitor): NotificationType {
// Down status has highest priority (critical)
if (decision.notificationReason === "escalation") {
return "escalation";
}

if (monitor.status === "down") {
return "monitor_down";
}
Expand All @@ -80,6 +83,7 @@ export class NotificationMessageBuilder implements INotificationMessageBuilder {
private determineSeverity(type: NotificationType): NotificationSeverity {
switch (type) {
case "monitor_down":
case "escalation":
return "critical";
case "threshold_breach":
return "warning";
Expand All @@ -103,6 +107,8 @@ export class NotificationMessageBuilder implements INotificationMessageBuilder {
return this.buildThresholdBreachContent(monitor, monitorStatusResponse as MonitorStatusResponse<HardwareStatusPayload>);
case "threshold_resolved":
return this.buildThresholdResolvedContent(monitor);
case "escalation":
return this.buildEscalationContent(monitor);
default:
return this.buildDefaultContent(monitor);
}
Expand Down Expand Up @@ -173,6 +179,19 @@ export class NotificationMessageBuilder implements INotificationMessageBuilder {
};
}

private buildEscalationContent(monitor: Monitor): NotificationContent {
const title = `Escalation: ${monitor.name}`;
const summary = `Monitor "${monitor.name}" has been down for an extended period.`;
const details = [`URL: ${monitor.url}`, `Status: Down`, `Type: ${monitor.type}`];

return {
title,
summary,
details,
timestamp: new Date(),
};
}

private buildDefaultContent(monitor: Monitor): NotificationContent {
return {
title: `Monitor: ${monitor.name}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export class EmailProvider implements INotificationProvider {
return `Monitor ${message.monitor.name} threshold exceeded`;
case "threshold_resolved":
return `Monitor ${message.monitor.name} thresholds resolved`;
case "escalation":
return `ESCALATION: Monitor ${message.monitor.name} is still down`;
default:
return `Alert: ${message.monitor.name}`;
}
Expand Down
14 changes: 13 additions & 1 deletion server/src/service/infrastructure/notificationsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +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>;
sendEscalationNotifications: (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse, decision: MonitorActionDecision, notificationIds: string[]) => Promise<boolean>;

sendTestNotification: (notification: Partial<Notification>) => Promise<boolean>;
testAllNotifications: (notificationIds: string[]) => Promise<boolean>;
Expand Down Expand Up @@ -137,10 +138,21 @@ export class NotificationsService implements INotificationsService {
return false;
}

// Send notifications based on decision
return await this.sendNotifications(monitor, monitorStatusResponse, decision);
};

sendEscalationNotifications = async (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse, decision: MonitorActionDecision, notificationIds: string[]) => {
const notifications = await this.notificationsRepository.findNotificationsByIds(notificationIds);
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;
return succeeded === notifications.length;
};

sendTestNotification = async (notification: Partial<Notification>) => {
switch (notification.type) {
case "email":
Expand Down
1 change: 1 addition & 0 deletions server/src/types/incident.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface Incident {
resolvedBy?: string | null;
resolvedByEmail?: string | null;
comment?: string | null;
escalationSentAt?: string | null;
createdAt: string;
updatedAt: string;
}
Expand Down
2 changes: 2 additions & 0 deletions server/src/types/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export interface Monitor {
geoCheckEnabled?: boolean;
geoCheckLocations?: GeoContinent[];
geoCheckInterval?: number;
escalationAfterMinutes?: number;
escalationNotificationChannels?: string[];
recentChecks: CheckSnapshot[];
createdAt: string;
updatedAt: string;
Expand Down
2 changes: 1 addition & 1 deletion server/src/types/notificationMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Part of notification system unification effort
*/

export type NotificationType = "monitor_down" | "monitor_up" | "threshold_breach" | "threshold_resolved" | "test";
export type NotificationType = "monitor_down" | "monitor_up" | "threshold_breach" | "threshold_resolved" | "escalation" | "test";

export type NotificationSeverity = "critical" | "warning" | "info" | "success";

Expand Down
Loading