-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook_handler.js
79 lines (68 loc) · 2.96 KB
/
webhook_handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const { STACK_TRACE_LINE_REGEX_INDEX_STACK_TRACE_LINE_ONLY } = require("./journalctl_parser");
const FormData = require("form-data");
const axios = require("axios");
/**
* Send the event to a Discord webhook URL
*
* @param webhookUrl {string}
* @param serviceName {string}
* @param stackTrace {array|null}
* @param exception {object|null}
* @param journalctlOutput {string}
* @param includeAttachment {boolean}
* @returns {Promise<void>}
*/
async function sendToDiscordWebhook(webhookUrl, serviceName, stackTrace, exception, journalctlOutput, includeAttachment = false) {
let stackTraceJoined = "*unavailable*";
let stackTraceExceptionLine = "*unavailable*";
let stackTraceExceptionLineNumber = null;
if (Array.isArray(stackTrace)) {
stackTraceJoined = stackTrace.map((stackTraceLine) => stackTraceLine[STACK_TRACE_LINE_REGEX_INDEX_STACK_TRACE_LINE_ONLY]).join("\n\n");
}
if (exception) {
stackTraceExceptionLine = exception.exceptionLine;
stackTraceExceptionLineNumber = exception.exceptionLineNumber;
}
const data = {
"embeds": [
{
"title": `Service "${serviceName}" failed`,
"color": 16711680,
"fields": [
{
"name": `Exception line ${stackTraceExceptionLineNumber !== null ? `(log line #${stackTraceExceptionLineNumber})` : ""}`,
"value": stackTraceExceptionLine,
"inline": false
},
{
"name": `Last stack trace ${stackTraceJoined.length > 1024 ? '(trimmed - last 1024 characters)' : ''}`,
"value": stackTraceJoined.substr(stackTraceJoined.length - 1024),
"inline": false
},
{
"name": `Service log ${journalctlOutput.length > 1024 ? '(trimmed - last 1024 characters)' : ''}`,
"value": journalctlOutput.substr(journalctlOutput.length - 1024),
"inline": false
}
]
}
]
};
const formData = new FormData();
formData.append("payload_json", JSON.stringify(data));
if (includeAttachment) {
formData.append("files[0]", journalctlOutput, "journalctl_output.txt");
if (Array.isArray(stackTrace)) {
formData.append("files[1]", stackTraceJoined, "stack_trace.txt");
}
}
await axios.post(webhookUrl, formData, { headers: formData.getHeaders() })
.catch((err) => {
console.error(err);
console.log("Unable to send notification, please check the error given above for more information.");
process.exit();
});
}
module.exports = {
sendToDiscordWebhook
};