2024-05-14 19:38:45 +00:00
|
|
|
# Example trigger code
|
|
|
|
trigger: |
|
|
|
|
import kv from 'zdravko/kv';
|
2024-05-18 20:18:28 +00:00
|
|
|
import incidents, { severity } from 'zdravko/incidents';
|
2024-05-14 19:38:45 +00:00
|
|
|
|
2024-05-18 20:18:28 +00:00
|
|
|
// Only execute on this specific targets.
|
|
|
|
export function filter(target) {
|
|
|
|
return target.tags.kind === 'http';
|
|
|
|
}
|
|
|
|
|
|
|
|
const getMinute = (date) => {
|
|
|
|
return Math.floor(date.getTime() / 1000 / 60);
|
|
|
|
}
|
|
|
|
|
|
|
|
const get5LastMinutes = (date) => {
|
|
|
|
const currentMinute = getMinute(date);
|
|
|
|
return Array.from({ length: 5 }, (_, i) => {
|
|
|
|
const minute = currentMinute - i;
|
|
|
|
if (minute < 0) {
|
|
|
|
return 60 - minute;
|
|
|
|
}
|
|
|
|
return minute;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// This trigger will check if there were more than 5 issues in last
|
|
|
|
// 5 minutes, if so it will create a critical incident.
|
|
|
|
export default function (target, monitor, outcome) {
|
|
|
|
// If the outcome is not failure, we close any potential incidents.
|
2024-05-14 19:38:45 +00:00
|
|
|
if (outcome.status !== 'FAILURE') {
|
2024-05-18 20:18:28 +00:00
|
|
|
incidents.close(target, monitor);
|
|
|
|
return;
|
2024-05-14 19:38:45 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 20:18:28 +00:00
|
|
|
const date = new Date();
|
|
|
|
|
|
|
|
let total = 0;
|
|
|
|
for (const minute of get5LastMinutes(date)) {
|
|
|
|
const count = kv.get(`${monitor.name}:issues:${minute}`) || 0;
|
|
|
|
total += count;
|
|
|
|
}
|
2024-05-14 19:38:45 +00:00
|
|
|
|
2024-05-18 20:18:28 +00:00
|
|
|
// If there are more than 5 issues in the last 5 minutes, create a critical incident.
|
|
|
|
if (total > 5) {
|
|
|
|
incidents.create(
|
|
|
|
target,
|
|
|
|
monitor,
|
|
|
|
severity.CRITICAL,
|
|
|
|
`More than 5 issues in the last 5 minutes.`,
|
|
|
|
{ special: "tags" }
|
|
|
|
);
|
|
|
|
// Else we would close any potential incidents. If non exist, that's ok.
|
|
|
|
} else {
|
|
|
|
incidents.close(target, monitor);
|
2024-05-14 19:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Increment and set TTL to 5 minutes
|
2024-05-18 20:18:28 +00:00
|
|
|
const minute = getMinute(date);
|
|
|
|
kv.increment(`${monitor.name}:issues:${minute}`, 5 * 60);
|
2024-05-14 19:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Example monitor code
|
2024-05-17 19:54:14 +00:00
|
|
|
check: |
|
2024-05-14 19:38:45 +00:00
|
|
|
import http from 'k6/http';
|
|
|
|
|
|
|
|
export const options = {
|
|
|
|
thresholds: {
|
|
|
|
// http errors should be less than 1%
|
|
|
|
http_req_failed: ['rate<0.01'],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-05-18 20:18:28 +00:00
|
|
|
// Filter out only HTTP targets.
|
|
|
|
export function filter(target) {
|
|
|
|
return target.tags.kind === "http";
|
|
|
|
};
|
|
|
|
|
|
|
|
// Execute the check on the targets.
|
|
|
|
export default function (target) {
|
|
|
|
http.get(target.url);
|
2024-05-14 19:38:45 +00:00
|
|
|
}
|