-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (58 loc) · 2.06 KB
/
index.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
const fetch = require("node-fetch");
const cron = require("node-cron");
const winston = require("winston");
const MONITOR_ENDPOINT =
process.env.MONITOR_ENDPOINT || "http://localhost:12345"; // default to check localhost on port 12345
const MONITOR_SCHEDULE = process.env.MONITOR_SCHEDULE || "*/2 * * * * *"; // schedule a check every 2 seconds
const MONITOR_LOGFILE = process.env.MONITOR_LOGFILE || "monitor.log";
const logger = winston.createLogger({
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: MONITOR_LOGFILE }),
],
});
console.log("Monitor Started...");
console.log(`Endpoint to monitor: ${MONITOR_ENDPOINT}`);
console.log(`Monitor Schedule: ${MONITOR_SCHEDULE}`);
console.log(`Monitor logfile: ${MONITOR_LOGFILE}`);
const RETRY_THRESHOLD = 10;
let numberOfErrorsInARow = 0;
let numberOfRequestsMade = 0;
let numberOfSuccess = 0;
let numberOfError = 0;
cron.schedule(MONITOR_SCHEDULE, () => {
logger.info(
`Health Info for endpoint: ${MONITOR_ENDPOINT}, Total Requests: ${numberOfRequestsMade} Total Successes: ${numberOfSuccess} Total Errors: ${numberOfError} Success Rate: ${
numberOfRequestsMade > 0 ? numberOfSuccess / numberOfRequestsMade * 100 : 0
}% Error Rate: ${
numberOfRequestsMade > 0 ? numberOfError / numberOfRequestsMade * 100 : 0
}%`
);
fetch(MONITOR_ENDPOINT)
.then((res) => {
numberOfRequestsMade++;
if (res.ok) {
numberOfErrorsInARow = 0;
numberOfSuccess++;
return res.text();
}
numberOfErrorsInARow++;
numberOfError++;
if (numberOfErrorsInARow > RETRY_THRESHOLD) {
logger.error(`Error - Endpoint: ${MONITOR_ENDPOINT} is likely down!!!`);
}
throw res;
})
.then((text) => {
logger.info(`Success - endpoint: ${MONITOR_ENDPOINT} - Payload: ${text}`);
})
.catch((err) => {
if (err && err.text) {
err.text().then((errorMessage) => {
logger.error(
`Error - Endpoint: ${MONITOR_ENDPOINT} - Error: ${errorMessage}.`
);
});
}
});
});