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();
|
|
|
|
}
|
|
|
|
|
2021-08-14 15:01:54 +00:00
|
|
|
public start() {
|
|
|
|
const unattendedStart = async () => {
|
|
|
|
if (this.started) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.started = true;
|
|
|
|
while (this.started) {
|
|
|
|
this.logger.log('info', `sending heartbeat for ${this.sourceNameArg} with metrics`, {
|
|
|
|
eventType: 'heartbeat',
|
|
|
|
metrics: await this.getMetrics(),
|
|
|
|
});
|
|
|
|
await plugins.smartdelay.delayFor(10000, null, true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
unattendedStart();
|
2021-08-12 21:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2021-08-14 15:01:54 +00:00
|
|
|
const pids = await plugins.pidtree(process.pid);
|
|
|
|
const stats = await plugins.pidusage([process.pid, ...pids]);
|
|
|
|
|
|
|
|
// lets compute cpu usage
|
|
|
|
let cpuPercentage = 0;
|
|
|
|
for (const stat of Object.keys(stats)) {
|
|
|
|
if (!stats[stat]) continue;
|
|
|
|
cpuPercentage += stats[stat].cpu / 100;
|
|
|
|
}
|
|
|
|
let cpuUsageText = `${cpuPercentage * 100} %`;
|
|
|
|
|
|
|
|
// lets compute memory usage
|
|
|
|
let memoryUsageBytes = 0;
|
|
|
|
for (const stat of Object.keys(stats)) {
|
|
|
|
if (!stats[stat]) continue;
|
|
|
|
memoryUsageBytes += stats[stat].memory;
|
|
|
|
}
|
|
|
|
let memoryUsageText = this.formatBytes(memoryUsageBytes);
|
|
|
|
let memoryPercentage = Math.round((memoryUsageBytes / plugins.os.totalmem()) * 100) / 100;
|
|
|
|
|
2021-08-12 21:19:39 +00:00
|
|
|
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,
|
2021-08-14 15:01:54 +00:00
|
|
|
cpuPercentage,
|
|
|
|
cpuUsageText,
|
|
|
|
memoryPercentage,
|
|
|
|
memoryUsageBytes,
|
|
|
|
memoryUsageText,
|
2021-08-12 21:19:39 +00:00
|
|
|
};
|
|
|
|
return returnMetrics;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async stop() {}
|
|
|
|
}
|