add pagerduty

This commit is contained in:
Alex Holliday committed 2025-06-10 13:44:15 +08:00
1 parent c20b8c665a
commit 08875b07ec
3 files changed
+57 -16

No files matched your search

+17 -14
View File
@@ -85,30 +85,33 @@ class NotificationController {
} }
testNotification = async (req, res, next) => { testNotification = async (req, res, next) => {
console.log("TESTING");
try { try {
const notification = req.body; const notification = req.body;
let success;
if (notification?.type === "email") { if (notification?.type === "email") {
const result = await this.notificationService.sendTestEmail(notification); success = await this.notificationService.sendTestEmail(notification);
return res.success({
msg: this.stringService.emailSendSuccess,
});
} }
if (notification?.type === "webhook") { if (notification?.type === "webhook") {
const success = success =
await this.notificationService.sendTestWebhookNotification(notification); await this.notificationService.sendTestWebhookNotification(notification);
if (!success) { }
return res.error({
msg: this.stringService.webhookSendFailed, if (notification?.type === "pager_duty") {
status: 400, success =
}); await this.notificationService.sendTestPagerDutyNotification(notification);
} }
return res.success({ if (!success) {
msg: this.stringService.webhookSendSuccess, return res.error({
msg: "Sending notification failed",
status: 400,
}); });
} }
return res.success({
msg: "Notification sent successfully",
});
} catch (error) { } catch (error) {
next(handleError(error, SERVICE_NAME, "testWebhook")); next(handleError(error, SERVICE_NAME, "testWebhook"));
} }
+23
View File
@@ -463,6 +463,29 @@ class NetworkService {
} }
} }
async requestPagerDuty({ message, routingKey, monitorUrl }) {
try {
const response = await this.axios.post(`https://events.pagerduty.com/v2/enqueue`, {
routing_key: routingKey,
event_action: "trigger",
payload: {
summary: message,
severity: "critical",
source: monitorUrl,
timestamp: new Date().toISOString(),
},
});
if (response?.data?.status !== "success") return false;
return true;
} catch (error) {
error.details = error.response?.data;
error.service = this.SERVICE_NAME;
error.method = "requestPagerDuty";
throw error;
}
}
/** /**
* Gets the status of a job based on its type and returns the appropriate response. * Gets the status of a job based on its type and returns the appropriate response.
* *
+17 -2
View File
@@ -211,7 +211,11 @@ class NotificationService {
to, to,
subject subject
); );
console.log(messageId);
if (messageId) {
return true;
}
return false;
}; };
/** /**
@@ -223,6 +227,7 @@ class NotificationService {
* @param {string} address - Email address to send the notification to * @param {string} address - Email address to send the notification to
* @returns {Promise<boolean>} - Indicates email was sent successfully * @returns {Promise<boolean>} - Indicates email was sent successfully
*/ */
async sendEmail(networkResponse, address) { async sendEmail(networkResponse, address) {
const { monitor, status, prevStatus } = networkResponse; const { monitor, status, prevStatus } = networkResponse;
const template = prevStatus === false ? "serverIsUpTemplate" : "serverIsDownTemplate"; const template = prevStatus === false ? "serverIsUpTemplate" : "serverIsDownTemplate";
@@ -232,6 +237,16 @@ class NotificationService {
return true; return true;
} }
async sendTestPagerDutyNotification(notification) {
const { routingKey } = notification.config;
const response = await this.networkService.requestPagerDuty({
message: "This is a test notification",
monitorUrl: "Test notification",
routingKey,
});
return response;
}
async sendPagerDutyNotification(networkResponse, notification) { async sendPagerDutyNotification(networkResponse, notification) {
const { monitor, status, code } = networkResponse; const { monitor, status, code } = networkResponse;
const { routingKey, platform } = notification.config; const { routingKey, platform } = notification.config;
@@ -251,7 +266,7 @@ class NotificationService {
routingKey, routingKey,
monitorUrl: monitor.url, monitorUrl: monitor.url,
}); });
return response.status; return response;
} catch (error) { } catch (error) {
this.logger.error({ this.logger.error({
message: "Failed to send PagerDuty notification", message: "Failed to send PagerDuty notification",