Skip to content

Commit

Permalink
New feature: Messages
Browse files Browse the repository at this point in the history
* Added the ability to add messages from checkers
* Messages can be accessed in alterters message / viewed in prom
* Added the ability to map codes to messages in a global setting via checkers settings EX:
```json
"sky-puppy-checker-template": {
            "foo": "bar",
            "code_messages": {
                "200": "Override me plz",
                "500": "Yikes its down"
            }
        }
```
* Added the ability to override those code_messages inside each service as well EX:

```json
"checker": {
                "name": "sky-puppy-checker-template",
                "settings": {
                    "bar": "test"
                },
                "code_messages": {
                    "200": "Yup its up"
                }
            }
```
  • Loading branch information
Phara0h committed Nov 2, 2020
1 parent e618d1f commit 7f27201
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 25 deletions.
15 changes: 8 additions & 7 deletions src/alerts.js
Expand Up @@ -157,16 +157,17 @@ class Alerts {
{
alert_type: alert.type,
service_name: service.name,
message: service.status.message,
timestamp: new Date().toISOString(),
last_unhealthy_total_duration:
service.status.last_unhealthy_total_duration || 'Unknown',
last_healthy_total_duration: service.status.last_healthy
? (
Number(
process.hrtime.bigint() - service.status.last_healthy
) / 1000000000
).toFixed(2)
: 'Unknown'
last_healthy_total_duration: service.status.last_healthy ?
(
Number(
process.hrtime.bigint() - service.status.last_healthy
) / 1000000000
).toFixed(2) :
'Unknown'
}
);

Expand Down
3 changes: 2 additions & 1 deletion src/checkers/request.js
Expand Up @@ -43,7 +43,8 @@ class Request {
var res = await fasquest.request(this.settings);

return {
code: res.statusCode
code: res.statusCode,
message: res.body
};
} catch (e) {
throw e.err;
Expand Down
36 changes: 35 additions & 1 deletion src/health-check.js
Expand Up @@ -135,6 +135,7 @@ class HealthCheck {
};
var checker = this.checkers[nService.config.checker.name];
var checkerSettings = {};
var checkerCodeMessages = null;

if (checker.settings) {
checkerSettings = {
Expand All @@ -147,6 +148,21 @@ class HealthCheck {
};
}

if (
checker.settings &&
checker.settings.code_messages &&
nService.config.checker.code_messages
) {
checkerCodeMessages = {
...checker.settings.code_messages,
...nService.config.checker.code_messages
};
} else if (nService.config.checker.code_messages) {
checkerCodeMessages = {
...nService.config.checker.code_messages
};
}
nService.code_messages = checkerCodeMessages;
nService.checker = await checker.mod(config, nService, checkerSettings);
await nService.checker.init();

Expand All @@ -164,17 +180,35 @@ class HealthCheck {
return config;
}

_mapMessages(code, message, service) {
if (service.code_messages) {
var codes = Object.keys(service.code_messages);

for (var i = 0; i < codes.length; i++) {
if (codes[i] == code) {
return service.code_messages[codes[i]];
}
}
}
return message || '';
}

async _runCheck(service) {
if (service && service.enabled) {
const startTime = process.hrtime.bigint();
const oldStatus = service.status.up;
// const oldStatus = service.status.up;

try {
var res = await service.checker.check();

service.status.time =
Number(process.hrtime.bigint() - startTime) / 1000000;
service.status.code = res.code;
service.status.message = this._mapMessages(
res.code,
res.message,
service
);
service.status.up = 1;

if (service.config.expected_status != service.status.code) {
Expand Down
22 changes: 11 additions & 11 deletions src/misc/stats.js
Expand Up @@ -5,26 +5,26 @@ class Stats {

updateService(name, status) {
for (var i = 0; i < this.services.length; i++) {
if(this.services[i].name == name) {
if (this.services[i].name == name) {
this.services[i].status = status;
return;
}
}
this.services.push({name,status})
this.services.push({ name, status });
}

toPrometheus() {
var pstring = ``;
var pstring = '';

for (var i = 0; i < this.services.length; i++) {
pstring += `sky_puppy_service_status{service="${this.services[i].name}" name="${this.services[i].name}"} ${this.services[i].status.up} ${Date.now()} \n`;
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}" status="healthy"} ${this.services[i].status.count.healthy} ${Date.now()} \n`;
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}" status="unhealthy"} ${this.services[i].status.count.unhealthy} ${Date.now()} \n`;
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}" status="unhealthy_status"} ${this.services[i].status.count.unhealthy_status} ${Date.now()} \n`;
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}" status="unhealthy_response_time"} ${this.services[i].status.count.unhealthy_response_time} ${Date.now()} \n`;
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}" status="down"} ${this.services[i].status.count.down} ${Date.now()} \n`;
pstring += `sky_puppy_service_response_time{service="${this.services[i].name}" name="${this.services[i].name}"} ${this.services[i].status.time} ${Date.now()} \n`;
pstring += `sky_puppy_service_response_code{service="${this.services[i].name}" name="${this.services[i].name}"} ${this.services[i].status.code} ${Date.now()} \n`;
pstring += `sky_puppy_service_status{service="${this.services[i].name}", name="${this.services[i].name}" } ${this.services[i].status.up} ${Date.now()} \n`;
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="healthy"} ${this.services[i].status.count.healthy} ${Date.now()} \n`;
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="unhealthy"} ${this.services[i].status.count.unhealthy} ${Date.now()} \n`;
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="unhealthy_status"} ${this.services[i].status.count.unhealthy_status} ${Date.now()} \n`;
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="unhealthy_response_time"} ${this.services[i].status.count.unhealthy_response_time} ${Date.now()} \n`;
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="down"} ${this.services[i].status.count.down} ${Date.now()} \n`;
pstring += `sky_puppy_service_response_time{service="${this.services[i].name}", name="${this.services[i].name}"} ${this.services[i].status.time} ${Date.now()} \n`;
pstring += `sky_puppy_service_response_code{service="${this.services[i].name}", message="${this.services[i].status.message}", name="${this.services[i].name}"} ${Number(this.services[i].status.code) || -1} ${Date.now()} \n`;
}
return pstring;
}
Expand Down
17 changes: 12 additions & 5 deletions test/sky-puppy-config.json
Expand Up @@ -2,7 +2,11 @@
"checkers": {
"request": {},
"sky-puppy-checker-template": {
"foo": "bar"
"foo": "bar",
"code_messages": {
"200": "Override me plz",
"500": "Yikes its down"
}
}
},
"alerters": {
Expand All @@ -14,7 +18,7 @@
"embeds": [
{
"title": "{{service_name}} is {{alert_type}}!",
"description": "This service was healthy for {{last_healthy_total_duration}} seconds!",
"description": "This service was healthy for {{last_healthy_total_duration}} seconds! {{message}}",
"color": 14828098,
"footer": {
"text": ""
Expand All @@ -34,7 +38,7 @@
"embeds": [
{
"title": "{{service_name}} is {{alert_type}}!",
"description": "This service was healthy for {{last_healthy_total_duration}} seconds!",
"description": "This service was healthy for {{last_healthy_total_duration}} seconds! {{message}}",
"color": 14852674,
"footer": {
"text": ""
Expand All @@ -54,7 +58,7 @@
"embeds": [
{
"title": "{{service_name}} is {{alert_type}}!",
"description": "Carry on, looks like things are back! We were down for {{last_unhealthy_total_duration}} seconds.",
"description": "Carry on, looks like things are back! We were down for {{last_unhealthy_total_duration}} seconds. {{message}}",
"color": 6480450,
"footer": {
"text": ""
Expand Down Expand Up @@ -152,6 +156,9 @@
"name": "sky-puppy-checker-template",
"settings": {
"bar": "test"
},
"code_messages": {
"200": "Yup its up"
}
},
"expected_response_time": 500,
Expand All @@ -175,4 +182,4 @@
"skypuppy": {
"version": "1.0.0"
}
}
}

0 comments on commit 7f27201

Please sign in to comment.