Skip to content

Commit

Permalink
Few fixes
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Phara0h committed Nov 17, 2020
1 parent 84ba89d commit ec30c55
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 219 deletions.
2 changes: 2 additions & 0 deletions src/config.js
Expand Up @@ -6,6 +6,8 @@ class Config {
constructor() {
this.version = require(path.dirname(require.main.filename) +
'/../package.json').version;
require('nstats')(null, null, this.version);

process.title = 'Sky Puppy v' + this.version;
try {
this.path =
Expand Down
5 changes: 3 additions & 2 deletions src/health-check.js
Expand Up @@ -278,14 +278,15 @@ class HealthCheck {
service.status.last_unhealthy = process.hrtime.bigint();
}
}
this.stats.updateService(service.name, service.status);
await this.stats.updateService(service.name, service.status);

this.alerts.alert(service);
await this.alerts.alert(service);
service.status.last_status = service.status.up;
const tout =
service.config.interval -
Number(process.hrtime.bigint() - startTime) / 1000000;

log.debug('tout: ' + (tout > 0 ? tout : 0));
this.services[service.name]._sTimeoutHandler = setTimeout(
async () => {
this._runCheck(service);
Expand Down
3 changes: 1 addition & 2 deletions src/index.js
@@ -1,14 +1,13 @@
#!/usr/bin/env node

const nstats = require('nstats')();
const Stats = require('./misc/stats.js');
const stats = new Stats();

async function start() {
const { NBars } = await import('nbars');
const HealthCheck = require('./health-check.js');
const healthCheck = new HealthCheck(stats, NBars);

const nstats = require('nstats')();
const app = require('fastify')({
logger: false
});
Expand Down
19 changes: 10 additions & 9 deletions src/misc/stats.js
Expand Up @@ -7,24 +7,25 @@ class Stats {
for (var i = 0; i < this.services.length; i++) {
if (this.services[i].name == name) {
this.services[i].status = status;
this.services.date = Date.now();
return;
}
}
this.services.push({ name, status });
this.services.push({ name, status, date: Date.now() });
}

toPrometheus() {
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}", message="${this.services[i].status.message}", name="${this.services[i].name}"} ${Number(this.services[i].status.code) || -1} ${Date.now()} \n`;
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`;
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`;
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`;
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`;
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`;
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`;
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`;
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`;
}
return pstring;
}
Expand Down

0 comments on commit ec30c55

Please sign in to comment.