Skip to content

Commit 7f27201

Browse files
committedNov 2, 2020
New feature: Messages
* 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" } } ```
1 parent e618d1f commit 7f27201

File tree

5 files changed

+68
-25
lines changed

5 files changed

+68
-25
lines changed
 

‎src/alerts.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,17 @@ class Alerts {
157157
{
158158
alert_type: alert.type,
159159
service_name: service.name,
160+
message: service.status.message,
160161
timestamp: new Date().toISOString(),
161162
last_unhealthy_total_duration:
162163
service.status.last_unhealthy_total_duration || 'Unknown',
163-
last_healthy_total_duration: service.status.last_healthy
164-
? (
165-
Number(
166-
process.hrtime.bigint() - service.status.last_healthy
167-
) / 1000000000
168-
).toFixed(2)
169-
: 'Unknown'
164+
last_healthy_total_duration: service.status.last_healthy ?
165+
(
166+
Number(
167+
process.hrtime.bigint() - service.status.last_healthy
168+
) / 1000000000
169+
).toFixed(2) :
170+
'Unknown'
170171
}
171172
);
172173

‎src/checkers/request.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class Request {
4343
var res = await fasquest.request(this.settings);
4444

4545
return {
46-
code: res.statusCode
46+
code: res.statusCode,
47+
message: res.body
4748
};
4849
} catch (e) {
4950
throw e.err;

‎src/health-check.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class HealthCheck {
135135
};
136136
var checker = this.checkers[nService.config.checker.name];
137137
var checkerSettings = {};
138+
var checkerCodeMessages = null;
138139

139140
if (checker.settings) {
140141
checkerSettings = {
@@ -147,6 +148,21 @@ class HealthCheck {
147148
};
148149
}
149150

151+
if (
152+
checker.settings &&
153+
checker.settings.code_messages &&
154+
nService.config.checker.code_messages
155+
) {
156+
checkerCodeMessages = {
157+
...checker.settings.code_messages,
158+
...nService.config.checker.code_messages
159+
};
160+
} else if (nService.config.checker.code_messages) {
161+
checkerCodeMessages = {
162+
...nService.config.checker.code_messages
163+
};
164+
}
165+
nService.code_messages = checkerCodeMessages;
150166
nService.checker = await checker.mod(config, nService, checkerSettings);
151167
await nService.checker.init();
152168

@@ -164,17 +180,35 @@ class HealthCheck {
164180
return config;
165181
}
166182

183+
_mapMessages(code, message, service) {
184+
if (service.code_messages) {
185+
var codes = Object.keys(service.code_messages);
186+
187+
for (var i = 0; i < codes.length; i++) {
188+
if (codes[i] == code) {
189+
return service.code_messages[codes[i]];
190+
}
191+
}
192+
}
193+
return message || '';
194+
}
195+
167196
async _runCheck(service) {
168197
if (service && service.enabled) {
169198
const startTime = process.hrtime.bigint();
170-
const oldStatus = service.status.up;
199+
// const oldStatus = service.status.up;
171200

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

175204
service.status.time =
176205
Number(process.hrtime.bigint() - startTime) / 1000000;
177206
service.status.code = res.code;
207+
service.status.message = this._mapMessages(
208+
res.code,
209+
res.message,
210+
service
211+
);
178212
service.status.up = 1;
179213

180214
if (service.config.expected_status != service.status.code) {

‎src/misc/stats.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ class Stats {
55

66
updateService(name, status) {
77
for (var i = 0; i < this.services.length; i++) {
8-
if(this.services[i].name == name) {
8+
if (this.services[i].name == name) {
99
this.services[i].status = status;
1010
return;
1111
}
1212
}
13-
this.services.push({name,status})
13+
this.services.push({ name, status });
1414
}
1515

1616
toPrometheus() {
17-
var pstring = ``;
17+
var pstring = '';
1818

1919
for (var i = 0; i < this.services.length; i++) {
20-
pstring += `sky_puppy_service_status{service="${this.services[i].name}" name="${this.services[i].name}"} ${this.services[i].status.up} ${Date.now()} \n`;
21-
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}" status="healthy"} ${this.services[i].status.count.healthy} ${Date.now()} \n`;
22-
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}" status="unhealthy"} ${this.services[i].status.count.unhealthy} ${Date.now()} \n`;
23-
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`;
24-
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`;
25-
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}" status="down"} ${this.services[i].status.count.down} ${Date.now()} \n`;
26-
pstring += `sky_puppy_service_response_time{service="${this.services[i].name}" name="${this.services[i].name}"} ${this.services[i].status.time} ${Date.now()} \n`;
27-
pstring += `sky_puppy_service_response_code{service="${this.services[i].name}" name="${this.services[i].name}"} ${this.services[i].status.code} ${Date.now()} \n`;
20+
pstring += `sky_puppy_service_status{service="${this.services[i].name}", name="${this.services[i].name}" } ${this.services[i].status.up} ${Date.now()} \n`;
21+
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="healthy"} ${this.services[i].status.count.healthy} ${Date.now()} \n`;
22+
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="unhealthy"} ${this.services[i].status.count.unhealthy} ${Date.now()} \n`;
23+
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`;
24+
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`;
25+
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="down"} ${this.services[i].status.count.down} ${Date.now()} \n`;
26+
pstring += `sky_puppy_service_response_time{service="${this.services[i].name}", name="${this.services[i].name}"} ${this.services[i].status.time} ${Date.now()} \n`;
27+
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`;
2828
}
2929
return pstring;
3030
}

‎test/sky-puppy-config.json

+12-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"checkers": {
33
"request": {},
44
"sky-puppy-checker-template": {
5-
"foo": "bar"
5+
"foo": "bar",
6+
"code_messages": {
7+
"200": "Override me plz",
8+
"500": "Yikes its down"
9+
}
610
}
711
},
812
"alerters": {
@@ -14,7 +18,7 @@
1418
"embeds": [
1519
{
1620
"title": "{{service_name}} is {{alert_type}}!",
17-
"description": "This service was healthy for {{last_healthy_total_duration}} seconds!",
21+
"description": "This service was healthy for {{last_healthy_total_duration}} seconds! {{message}}",
1822
"color": 14828098,
1923
"footer": {
2024
"text": ""
@@ -34,7 +38,7 @@
3438
"embeds": [
3539
{
3640
"title": "{{service_name}} is {{alert_type}}!",
37-
"description": "This service was healthy for {{last_healthy_total_duration}} seconds!",
41+
"description": "This service was healthy for {{last_healthy_total_duration}} seconds! {{message}}",
3842
"color": 14852674,
3943
"footer": {
4044
"text": ""
@@ -54,7 +58,7 @@
5458
"embeds": [
5559
{
5660
"title": "{{service_name}} is {{alert_type}}!",
57-
"description": "Carry on, looks like things are back! We were down for {{last_unhealthy_total_duration}} seconds.",
61+
"description": "Carry on, looks like things are back! We were down for {{last_unhealthy_total_duration}} seconds. {{message}}",
5862
"color": 6480450,
5963
"footer": {
6064
"text": ""
@@ -152,6 +156,9 @@
152156
"name": "sky-puppy-checker-template",
153157
"settings": {
154158
"bar": "test"
159+
},
160+
"code_messages": {
161+
"200": "Yup its up"
155162
}
156163
},
157164
"expected_response_time": 500,
@@ -175,4 +182,4 @@
175182
"skypuppy": {
176183
"version": "1.0.0"
177184
}
178-
}
185+
}

0 commit comments

Comments
 (0)
Please sign in to comment.