2024-05-14 19:38:45 +00:00
|
|
|
# Example trigger code
|
|
|
|
trigger: |
|
2024-05-23 16:33:30 +00:00
|
|
|
import kv from 'k6/x/zdravko/kv';
|
|
|
|
import incidents, { severity } from 'k6/x/zdravko/incidents';
|
|
|
|
import { getTarget, getMonitor, getOutcome } from 'k6/x/zdravko';
|
2024-05-18 20:18:28 +00:00
|
|
|
|
|
|
|
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.
|
2024-05-23 16:33:30 +00:00
|
|
|
export default function () {
|
|
|
|
const target = getTarget();
|
|
|
|
const monitor = getMonitor();
|
|
|
|
const outcome = getOutcome();
|
|
|
|
|
2024-05-18 20:18:28 +00:00
|
|
|
// 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';
|
2024-05-23 16:33:30 +00:00
|
|
|
import { getTarget } from 'k6/x/zdravko';
|
2024-05-14 19:38:45 +00:00
|
|
|
|
|
|
|
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
|
|
|
// Execute the check on the targets.
|
2024-05-23 16:33:30 +00:00
|
|
|
export default function () {
|
|
|
|
const { name, group, metadata } = getTarget();
|
|
|
|
|
|
|
|
console.log(`Running check for ${group}/${name}`)
|
|
|
|
|
|
|
|
http.get(metadata.spec.url);
|
2024-05-14 19:38:45 +00:00
|
|
|
}
|
2024-05-23 16:33:30 +00:00
|
|
|
|
|
|
|
filter: |
|
2024-05-24 20:19:06 +00:00
|
|
|
target.metadata.kind == "Http" && target.metadata.spec.url != ""
|
2024-05-23 16:33:30 +00:00
|
|
|
|
|
|
|
target: |
|
|
|
|
kind: Http
|
|
|
|
tags:
|
|
|
|
production: "true"
|
|
|
|
spec:
|
|
|
|
url: "https://test.k6.io"
|
|
|
|
method: "GET"
|
|
|
|
headers:
|
|
|
|
User-Agent: "Zdravko"
|