smartmetrics/ts/smartmetrics.classes.smartmetrics.ts

116 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-08-12 21:19:39 +00:00
import * as plugins from './smartmetrics.plugins';
import * as interfaces from './smartmetrics.interfaces';
import { Cipher } from 'crypto';
export class SmartMetrics {
public started = false;
public sourceNameArg: string;
public logger: plugins.smartlog.Smartlog;
public registry: plugins.promClient.Registry;
public async setup() {
const collectDefaultMetrics = plugins.promClient.collectDefaultMetrics;
this.registry = new plugins.promClient.Registry();
collectDefaultMetrics({ register: this.registry });
}
constructor(loggerArg: plugins.smartlog.Smartlog, sourceNameArg: string) {
this.logger = loggerArg;
this.sourceNameArg = sourceNameArg;
this.setup();
}
public async start() {
if (this.started) {
return;
}
this.started = true;
while (this.started) {
2021-08-13 09:55:38 +00:00
this.logger.log('info', `sending heartbeat for ${this.sourceNameArg} with metrics`, {
eventType: 'heartbeat',
2021-08-12 21:19:39 +00:00
metrics: await this.getMetrics(),
});
await plugins.smartdelay.delayFor(60000, null, true);
}
}
public getCpuUsagePercentage() {
// Take the first CPU, considering every CPUs have the same specs
// and every NodeJS process only uses one at a time.
const cpus = plugins.os.cpus();
let total: number = 0;
for (const cpu of cpus) {
total +=
cpu.times.user +
cpu.times.sys +
cpu.times.nice +
cpu.times.nice +
cpu.times.irq +
cpu.times.idle;
}
2021-08-13 11:13:11 +00:00
2021-08-12 21:19:39 +00:00
const usage = process.cpuUsage();
2021-08-13 11:13:11 +00:00
const currentCPUUsage = (usage.user + usage.system);
2021-08-12 21:19:39 +00:00
// Find out the percentage used for this specific CPU
const perc = (currentCPUUsage / total) * 100;
return perc;
}
public formatBytes(bytes: number, decimals = 2) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
public async getMetrics() {
const originalMetrics = await this.registry.getMetricsAsJSON();
const returnMetrics: interfaces.IMetricsSnapshot = {
originalMetrics,
process_cpu_seconds_total: (
originalMetrics.find((metricSet) => metricSet.name === 'process_cpu_seconds_total') as any
).values[0].value,
nodejs_active_handles_total: (
originalMetrics.find((metricSet) => metricSet.name === 'nodejs_active_handles_total') as any
).values[0].value,
nodejs_active_requests_total: (
originalMetrics.find(
(metricSet) => metricSet.name === 'nodejs_active_requests_total'
) as any
).values[0].value,
nodejs_heap_size_total_bytes: (
originalMetrics.find(
(metricSet) => metricSet.name === 'nodejs_heap_size_total_bytes'
) as any
).values[0].value,
cpuPercentage: Math.round(this.getCpuUsagePercentage() * 100) / 100,
memoryUsageText: this.formatBytes(
(
originalMetrics.find(
(metricSet) => metricSet.name === 'nodejs_heap_size_total_bytes'
) as any
).values[0].value
),
memoryPercentage:
Math.round(((
originalMetrics.find(
(metricSet) => metricSet.name === 'nodejs_heap_size_total_bytes'
) as any
).values[0].value /
plugins.os.totalmem()) *
100 * 100) / 100,
};
return returnMetrics;
}
public async stop() {}
}