fix(core): update
This commit is contained in:
@ -20,43 +20,21 @@ export class SmartMetrics {
|
||||
this.setup();
|
||||
}
|
||||
|
||||
public async start() {
|
||||
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(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;
|
||||
}
|
||||
|
||||
const usage = process.cpuUsage();
|
||||
const currentCPUUsage = (usage.user + usage.system);
|
||||
|
||||
// Find out the percentage used for this specific CPU
|
||||
const perc = (currentCPUUsage / total) * 100;
|
||||
|
||||
return perc;
|
||||
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();
|
||||
}
|
||||
|
||||
public formatBytes(bytes: number, decimals = 2) {
|
||||
@ -73,6 +51,26 @@ export class SmartMetrics {
|
||||
|
||||
public async getMetrics() {
|
||||
const originalMetrics = await this.registry.getMetricsAsJSON();
|
||||
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;
|
||||
|
||||
const returnMetrics: interfaces.IMetricsSnapshot = {
|
||||
originalMetrics,
|
||||
process_cpu_seconds_total: (
|
||||
@ -91,22 +89,11 @@ export class SmartMetrics {
|
||||
(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,
|
||||
cpuPercentage,
|
||||
cpuUsageText,
|
||||
memoryPercentage,
|
||||
memoryUsageBytes,
|
||||
memoryUsageText,
|
||||
};
|
||||
return returnMetrics;
|
||||
}
|
||||
|
@ -6,6 +6,8 @@ export interface IMetricsSnapshot {
|
||||
nodejs_active_requests_total: number;
|
||||
nodejs_heap_size_total_bytes: number;
|
||||
cpuPercentage: number;
|
||||
cpuUsageText: string;
|
||||
memoryPercentage: number;
|
||||
memoryUsageBytes: number;
|
||||
memoryUsageText: string;
|
||||
}
|
@ -15,8 +15,12 @@ export {
|
||||
}
|
||||
|
||||
// third party scope
|
||||
import pidusage from 'pidusage';
|
||||
import pidtree from 'pidtree';
|
||||
import * as promClient from 'prom-client';
|
||||
|
||||
export {
|
||||
pidusage,
|
||||
pidtree,
|
||||
promClient
|
||||
}
|
||||
|
Reference in New Issue
Block a user