fix(core): update
This commit is contained in:
2
ts/index.ts
Normal file
2
ts/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './smartmetrics.interfaces';
|
||||
export * from './smartmetrics.classes.smartmetrics';
|
116
ts/smartmetrics.classes.smartmetrics.ts
Normal file
116
ts/smartmetrics.classes.smartmetrics.ts
Normal file
@ -0,0 +1,116 @@
|
||||
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) {
|
||||
this.logger.log('info', `sending heartbeat for ${this.sourceNameArg}`, {
|
||||
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;
|
||||
}
|
||||
|
||||
// Normalize the one returned by process.cpuUsage()
|
||||
// (microseconds VS miliseconds)
|
||||
const usage = process.cpuUsage();
|
||||
const currentCPUUsage = (usage.user + usage.system) * 1000;
|
||||
|
||||
// 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() {}
|
||||
}
|
11
ts/smartmetrics.interfaces.ts
Normal file
11
ts/smartmetrics.interfaces.ts
Normal file
@ -0,0 +1,11 @@
|
||||
// this might be extracted into a package @pushrocks/smartmetrics-interfaces in the future
|
||||
export interface IMetricsSnapshot {
|
||||
originalMetrics: any[];
|
||||
process_cpu_seconds_total: number;
|
||||
nodejs_active_handles_total: number;
|
||||
nodejs_active_requests_total: number;
|
||||
nodejs_heap_size_total_bytes: number;
|
||||
cpuPercentage: number;
|
||||
memoryPercentage: number;
|
||||
memoryUsageText: string;
|
||||
}
|
22
ts/smartmetrics.plugins.ts
Normal file
22
ts/smartmetrics.plugins.ts
Normal file
@ -0,0 +1,22 @@
|
||||
// node native
|
||||
import * as os from 'os';
|
||||
|
||||
export {
|
||||
os
|
||||
}
|
||||
|
||||
// pushrocks scope
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartlog from '@pushrocks/smartlog';
|
||||
|
||||
export {
|
||||
smartdelay,
|
||||
smartlog
|
||||
}
|
||||
|
||||
// third party scope
|
||||
import * as promClient from 'prom-client';
|
||||
|
||||
export {
|
||||
promClient
|
||||
}
|
Reference in New Issue
Block a user