add pagerduty
This commit is contained in:
1 parent
c20b8c665a
commit
08875b07ec
3 files changed
+57
-16
No files matched your search
@@ -85,30 +85,33 @@ class NotificationController {
|
||||
}
|
||||
|
||||
testNotification = async (req, res, next) => {
|
||||
console.log("TESTING");
|
||||
try {
|
||||
const notification = req.body;
|
||||
let success;
|
||||
|
||||
if (notification?.type === "email") {
|
||||
const result = await this.notificationService.sendTestEmail(notification);
|
||||
return res.success({
|
||||
msg: this.stringService.emailSendSuccess,
|
||||
});
|
||||
success = await this.notificationService.sendTestEmail(notification);
|
||||
}
|
||||
|
||||
if (notification?.type === "webhook") {
|
||||
const success =
|
||||
success =
|
||||
await this.notificationService.sendTestWebhookNotification(notification);
|
||||
if (!success) {
|
||||
return res.error({
|
||||
msg: this.stringService.webhookSendFailed,
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
return res.success({
|
||||
msg: this.stringService.webhookSendSuccess,
|
||||
}
|
||||
|
||||
if (notification?.type === "pager_duty") {
|
||||
success =
|
||||
await this.notificationService.sendTestPagerDutyNotification(notification);
|
||||
}
|
||||
if (!success) {
|
||||
return res.error({
|
||||
msg: "Sending notification failed",
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
return res.success({
|
||||
msg: "Notification sent successfully",
|
||||
});
|
||||
} catch (error) {
|
||||
next(handleError(error, SERVICE_NAME, "testWebhook"));
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -211,7 +211,11 @@ class NotificationService {
|
||||
to,
|
||||
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
|
||||
* @returns {Promise<boolean>} - Indicates email was sent successfully
|
||||
*/
|
||||
|
||||
async sendEmail(networkResponse, address) {
|
||||
const { monitor, status, prevStatus } = networkResponse;
|
||||
const template = prevStatus === false ? "serverIsUpTemplate" : "serverIsDownTemplate";
|
||||
@@ -232,6 +237,16 @@ class NotificationService {
|
||||
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) {
|
||||
const { monitor, status, code } = networkResponse;
|
||||
const { routingKey, platform } = notification.config;
|
||||
@@ -251,7 +266,7 @@ class NotificationService {
|
||||
routingKey,
|
||||
monitorUrl: monitor.url,
|
||||
});
|
||||
return response.status;
|
||||
return response;
|
||||
} catch (error) {
|
||||
this.logger.error({
|
||||
message: "Failed to send PagerDuty notification",
|
||||
|
||||
Reference in new issue
Block a user