Skip to content

Commit ec30c55

Browse files
committedNov 17, 2020
Few fixes
- Added date to each prom stats only when it was updated - Fixed bug with version not showing in prom stats - Fixed possible bug around alerts
1 parent 84ba89d commit ec30c55

File tree

5 files changed

+222
-219
lines changed

5 files changed

+222
-219
lines changed
 

‎src/config.js

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class Config {
66
constructor() {
77
this.version = require(path.dirname(require.main.filename) +
88
'/../package.json').version;
9+
require('nstats')(null, null, this.version);
10+
911
process.title = 'Sky Puppy v' + this.version;
1012
try {
1113
this.path =

‎src/health-check.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,15 @@ class HealthCheck {
278278
service.status.last_unhealthy = process.hrtime.bigint();
279279
}
280280
}
281-
this.stats.updateService(service.name, service.status);
281+
await this.stats.updateService(service.name, service.status);
282282

283-
this.alerts.alert(service);
283+
await this.alerts.alert(service);
284284
service.status.last_status = service.status.up;
285285
const tout =
286286
service.config.interval -
287287
Number(process.hrtime.bigint() - startTime) / 1000000;
288288

289+
log.debug('tout: ' + (tout > 0 ? tout : 0));
289290
this.services[service.name]._sTimeoutHandler = setTimeout(
290291
async () => {
291292
this._runCheck(service);

‎src/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#!/usr/bin/env node
22

3-
const nstats = require('nstats')();
43
const Stats = require('./misc/stats.js');
54
const stats = new Stats();
65

76
async function start() {
87
const { NBars } = await import('nbars');
98
const HealthCheck = require('./health-check.js');
109
const healthCheck = new HealthCheck(stats, NBars);
11-
10+
const nstats = require('nstats')();
1211
const app = require('fastify')({
1312
logger: false
1413
});

‎src/misc/stats.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,25 @@ class Stats {
77
for (var i = 0; i < this.services.length; i++) {
88
if (this.services[i].name == name) {
99
this.services[i].status = status;
10+
this.services.date = Date.now();
1011
return;
1112
}
1213
}
13-
this.services.push({ name, status });
14+
this.services.push({ name, status, date: Date.now() });
1415
}
1516

1617
toPrometheus() {
1718
var pstring = '';
1819

1920
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}", message="${this.services[i].status.message}", name="${this.services[i].name}"} ${Number(this.services[i].status.code) || -1} ${Date.now()} \n`;
21+
pstring += `sky_puppy_service_status{service="${this.services[i].name}", name="${this.services[i].name}" } ${this.services[i].status.up} ${this.services[i].date} \n`;
22+
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="healthy"} ${this.services[i].status.count.healthy} ${this.services[i].date} \n`;
23+
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="unhealthy"} ${this.services[i].status.count.unhealthy} ${this.services[i].date} \n`;
24+
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="unhealthy_status"} ${this.services[i].status.count.unhealthy_status} ${this.services[i].date} \n`;
25+
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="unhealthy_response_time"} ${this.services[i].status.count.unhealthy_response_time} ${this.services[i].date} \n`;
26+
pstring += `sky_puppy_service_status_count_total{service="${this.services[i].name}", status="down"} ${this.services[i].status.count.down} ${this.services[i].date} \n`;
27+
pstring += `sky_puppy_service_response_time{service="${this.services[i].name}", name="${this.services[i].name}"} ${this.services[i].status.time} ${this.services[i].date} \n`;
28+
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} ${this.services[i].date} \n`;
2829
}
2930
return pstring;
3031
}

‎test/sky-puppy-config.json

+206-206
Original file line numberDiff line numberDiff line change
@@ -1,220 +1,220 @@
11
{
2-
"checkers": {
3-
"request": {},
4-
"sky-puppy-checker-template": {
5-
"foo": "bar",
6-
"code_messages": {
7-
"200": "Override me plz",
8-
"500": "Yikes its down"
9-
}
10-
}
11-
},
12-
"alerters": {
13-
"discord_down": {
14-
"uri": "http://127.0.0.1:4270/alert/test",
15-
"json": true,
16-
"method": "POST",
17-
"body": {
18-
"embeds": [
19-
{
20-
"title": "{{service_name}} is {{alert_type}}!",
21-
"description": "This service was healthy for {{last_healthy_total_duration}} seconds! {{message}}",
22-
"color": 14828098,
23-
"footer": {
24-
"text": ""
25-
},
26-
"timestamp": "{{timestamp}}"
27-
}
28-
],
29-
"username": "Sky Puppy",
30-
"avatar_url": "https://i.imgur.com/J5vIVSt.png"
31-
}
32-
},
33-
"discord_unhealthy": {
34-
"uri": "http://127.0.0.1:4270/alert/test",
35-
"json": true,
36-
"method": "POST",
37-
"body": {
38-
"embeds": [
39-
{
40-
"title": "{{service_name}} is {{alert_type}}!",
41-
"description": "This service was healthy for {{last_healthy_total_duration}} seconds! {{message}}",
42-
"color": 14852674,
43-
"footer": {
44-
"text": ""
45-
},
46-
"timestamp": "{{timestamp}}"
47-
}
48-
],
49-
"username": "Sky Puppy",
50-
"avatar_url": "https://i.imgur.com/J5vIVSt.png"
51-
}
52-
},
53-
"discord_healthy": {
54-
"uri": "http://127.0.0.1:4270/alert/test",
55-
"json": true,
56-
"method": "POST",
57-
"body": {
58-
"embeds": [
59-
{
60-
"title": "{{service_name}} is {{alert_type}}!",
61-
"description": "Carry on, looks like things are back! We were down for {{last_unhealthy_total_duration}} seconds. {{message}}",
62-
"color": 6480450,
63-
"footer": {
64-
"text": ""
65-
},
66-
"timestamp": "{{timestamp}}"
67-
}
68-
],
69-
"username": "Sky Puppy",
70-
"avatar_url": "https://i.imgur.com/3rfFeOu.png"
71-
}
72-
}
73-
},
74-
"services": {
75-
"timeout-test": {
76-
"interval": 5,
77-
"checker": {
78-
"name": "request",
79-
"settings": {
80-
"uri": "http://127.0.0.1:4270/wait/random/0/1",
81-
"timeout": 2
2+
"checkers": {
3+
"request": {},
4+
"sky-puppy-checker-template": {
5+
"foo": "bar",
6+
"code_messages": {
7+
"200": "Override me plz",
8+
"500": "Yikes its down"
9+
}
8210
}
83-
},
84-
"expected_response_time": 500,
85-
"alerts": [
86-
{
87-
"type": "down",
88-
"alerter": "discord_down"
89-
},
90-
{
91-
"type": "unhealthy_response_time",
92-
"for": 1,
93-
"alerter": "discord_unhealthy"
94-
},
95-
{
96-
"type": "healthy",
97-
"alerter": "discord_healthy"
98-
}
99-
]
10011
},
101-
"status-test": {
102-
"interval": 5,
103-
"checker": {
104-
"name": "request",
105-
"settings": {
106-
"uri": "http://127.0.0.1:4270/error/random",
107-
"timeout": 2
108-
}
109-
},
110-
"start_delay": 1,
111-
"alerts": [
112-
{
113-
"type": "down",
114-
"alerter": "discord_down"
12+
"alerters": {
13+
"discord_down": {
14+
"uri": "http://127.0.0.1:4270/alert/test",
15+
"json": true,
16+
"method": "POST",
17+
"body": {
18+
"embeds": [
19+
{
20+
"title": "{{service_name}} is {{alert_type}}!",
21+
"description": "This service was healthy for {{last_healthy_total_duration}} seconds! {{message}}",
22+
"color": 14828098,
23+
"footer": {
24+
"text": ""
25+
},
26+
"timestamp": "{{timestamp}}"
27+
}
28+
],
29+
"username": "Sky Puppy",
30+
"avatar_url": "https://i.imgur.com/J5vIVSt.png"
31+
}
11532
},
116-
{
117-
"type": "unhealthy_status",
118-
"for": 4,
119-
"alerter": "discord_unhealthy"
33+
"discord_unhealthy": {
34+
"uri": "http://127.0.0.1:4270/alert/test",
35+
"json": true,
36+
"method": "POST",
37+
"body": {
38+
"embeds": [
39+
{
40+
"title": "{{service_name}} is {{alert_type}}!",
41+
"description": "This service was healthy for {{last_healthy_total_duration}} seconds! {{message}}",
42+
"color": 14852674,
43+
"footer": {
44+
"text": ""
45+
},
46+
"timestamp": "{{timestamp}}"
47+
}
48+
],
49+
"username": "Sky Puppy",
50+
"avatar_url": "https://i.imgur.com/J5vIVSt.png"
51+
}
12052
},
121-
{
122-
"type": "healthy",
123-
"alerter": "discord_healthy"
53+
"discord_healthy": {
54+
"uri": "http://127.0.0.1:4270/alert/test",
55+
"json": true,
56+
"method": "POST",
57+
"body": {
58+
"embeds": [
59+
{
60+
"title": "{{service_name}} is {{alert_type}}!",
61+
"description": "Carry on, looks like things are back! We were down for {{last_unhealthy_total_duration}} seconds. {{message}}",
62+
"color": 6480450,
63+
"footer": {
64+
"text": ""
65+
},
66+
"timestamp": "{{timestamp}}"
67+
}
68+
],
69+
"username": "Sky Puppy",
70+
"avatar_url": "https://i.imgur.com/3rfFeOu.png"
71+
}
12472
}
125-
]
12673
},
127-
"put-test": {
128-
"interval": 3,
129-
"checker": {
130-
"name": "request",
131-
"settings": {
132-
"uri": "http://127.0.0.1:4270/error/flipflop",
133-
"timeout": 2,
134-
"json": true,
135-
"method": "PUT",
136-
"body": {
137-
"test": "sweet"
138-
}
139-
}
140-
},
141-
"start_delay": 2,
142-
"alerts": [
143-
{
144-
"type": "unhealthy",
145-
"alerter": "discord_unhealthy"
74+
"services": {
75+
"timeout-test": {
76+
"interval": 5,
77+
"checker": {
78+
"name": "request",
79+
"settings": {
80+
"uri": "http://127.0.0.1:4270/wait/random/0/1",
81+
"timeout": 2
82+
}
83+
},
84+
"expected_response_time": 500,
85+
"alerts": [
86+
{
87+
"type": "down",
88+
"alerter": "discord_down"
89+
},
90+
{
91+
"type": "unhealthy_response_time",
92+
"for": 1,
93+
"alerter": "discord_unhealthy"
94+
},
95+
{
96+
"type": "healthy",
97+
"alerter": "discord_healthy"
98+
}
99+
]
146100
},
147-
{
148-
"type": "healthy",
149-
"alerter": "discord_healthy"
150-
}
151-
]
152-
},
153-
"alert-error-status-test": {
154-
"interval": 1,
155-
"checker": {
156-
"name": "request",
157-
"settings": {
158-
"uri": "http://127.0.0.1:4270/error/in/5",
159-
"timeout": 2,
160-
"json": true,
161-
"method": "POST",
162-
"body": {
163-
"test": "sweet"
164-
}
165-
}
166-
},
167-
"start_delay": 0,
168-
"alerts": [
169-
{
170-
"type": "down",
171-
"alerter": "discord_down"
101+
"status-test": {
102+
"interval": 5,
103+
"checker": {
104+
"name": "request",
105+
"settings": {
106+
"uri": "http://127.0.0.1:4270/error/random",
107+
"timeout": 2
108+
}
109+
},
110+
"start_delay": 1,
111+
"alerts": [
112+
{
113+
"type": "down",
114+
"alerter": "discord_down"
115+
},
116+
{
117+
"type": "unhealthy_status",
118+
"for": 4,
119+
"alerter": "discord_unhealthy"
120+
},
121+
{
122+
"type": "healthy",
123+
"alerter": "discord_healthy"
124+
}
125+
]
172126
},
173-
{
174-
"type": "unhealthy_status",
175-
"alerter": "discord_unhealthy"
127+
"put-test": {
128+
"interval": 3,
129+
"checker": {
130+
"name": "request",
131+
"settings": {
132+
"uri": "http://127.0.0.1:4270/error/flipflop",
133+
"timeout": 2,
134+
"json": true,
135+
"method": "PUT",
136+
"body": {
137+
"test": "sweet"
138+
}
139+
}
140+
},
141+
"start_delay": 2,
142+
"alerts": [
143+
{
144+
"type": "unhealthy",
145+
"alerter": "discord_unhealthy"
146+
},
147+
{
148+
"type": "healthy",
149+
"alerter": "discord_healthy"
150+
}
151+
]
176152
},
177-
{
178-
"type": "healthy",
179-
"alerter": "discord_healthy"
180-
}
181-
]
182-
},
183-
"sky-puppy-checker-template-test": {
184-
"interval": 2,
185-
"checker": {
186-
"name": "sky-puppy-checker-template",
187-
"settings": {
188-
"bar": "test"
153+
"alert-error-status-test": {
154+
"interval": 1,
155+
"checker": {
156+
"name": "request",
157+
"settings": {
158+
"uri": "http://127.0.0.1:4270/error/in/5",
159+
"timeout": 2,
160+
"json": true,
161+
"method": "POST",
162+
"body": {
163+
"test": "sweet"
164+
}
165+
}
166+
},
167+
"start_delay": 0,
168+
"alerts": [
169+
{
170+
"type": "down",
171+
"alerter": "discord_down"
172+
},
173+
{
174+
"type": "unhealthy_status",
175+
"alerter": "discord_unhealthy"
176+
},
177+
{
178+
"type": "healthy",
179+
"alerter": "discord_healthy"
180+
}
181+
]
189182
},
190-
"code_messages": {
191-
"200": "Yup its up"
183+
"sky-puppy-checker-template-test": {
184+
"interval": 2,
185+
"checker": {
186+
"name": "sky-puppy-checker-template",
187+
"settings": {
188+
"bar": "test"
189+
},
190+
"code_messages": {
191+
"200": "Yup its up"
192+
}
193+
},
194+
"expected_response_time": 500,
195+
"alerts": [
196+
{
197+
"type": "down",
198+
"alerter": "discord_down"
199+
},
200+
{
201+
"type": "unhealthy_response_time",
202+
"for": 1,
203+
"alerter": "discord_unhealthy"
204+
},
205+
{
206+
"type": "healthy",
207+
"alerter": "discord_healthy"
208+
}
209+
]
192210
}
193-
},
194-
"expected_response_time": 500,
195-
"alerts": [
196-
{
197-
"type": "down",
198-
"alerter": "discord_down"
199-
},
200-
{
201-
"type": "unhealthy_response_time",
202-
"for": 1,
203-
"alerter": "discord_unhealthy"
204-
},
205-
{
206-
"type": "healthy",
207-
"alerter": "discord_healthy"
211+
},
212+
"skypuppy": {
213+
"version": "1.0.0",
214+
"log": {
215+
"enable": true,
216+
"colors": true,
217+
"level": "info"
208218
}
209-
]
210-
}
211-
},
212-
"skypuppy": {
213-
"version": "1.0.0",
214-
"log": {
215-
"enable": true,
216-
"colors": true,
217-
"level": "info"
218219
}
219-
}
220-
}
220+
}

0 commit comments

Comments
 (0)
Please sign in to comment.