fix(core): update

This commit is contained in:
Philipp Kunz 2021-08-14 17:01:54 +02:00
parent 533639b66d
commit 1b8f4f2219
6 changed files with 129 additions and 54 deletions

78
package-lock.json generated
View File

@ -11,6 +11,9 @@
"dependencies": { "dependencies": {
"@pushrocks/smartdelay": "^2.0.13", "@pushrocks/smartdelay": "^2.0.13",
"@pushrocks/smartlog": "^2.0.44", "@pushrocks/smartlog": "^2.0.44",
"@types/pidusage": "^2.0.1",
"pidtree": "^0.5.0",
"pidusage": "^2.0.21",
"prom-client": "^13.2.0" "prom-client": "^13.2.0"
}, },
"devDependencies": { "devDependencies": {
@ -3054,6 +3057,12 @@
"@types/express-serve-static-core": "*" "@types/express-serve-static-core": "*"
} }
}, },
"node_modules/@types/pidusage": {
"version": "2.0.1",
"resolved": "https://verdaccio.lossless.one/@types%2fpidusage/-/pidusage-2.0.1.tgz",
"integrity": "sha512-tYYcz/+5v/EGYT83C0pIXrJGOiVBLksQvxgJboG4nGqx/gZTvq0Ro4SkAjECqMk7L4Ww58VWB4j48qeYh4/YJg==",
"license": "MIT"
},
"node_modules/@types/puppeteer": { "node_modules/@types/puppeteer": {
"version": "5.4.4", "version": "5.4.4",
"resolved": "https://verdaccio.lossless.one/@types%2fpuppeteer/-/puppeteer-5.4.4.tgz", "resolved": "https://verdaccio.lossless.one/@types%2fpuppeteer/-/puppeteer-5.4.4.tgz",
@ -10453,6 +10462,50 @@
"url": "https://github.com/sponsors/jonschlinkert" "url": "https://github.com/sponsors/jonschlinkert"
} }
}, },
"node_modules/pidtree": {
"version": "0.5.0",
"resolved": "https://verdaccio.lossless.one/pidtree/-/pidtree-0.5.0.tgz",
"integrity": "sha512-9nxspIM7OpZuhBxPg73Zvyq7j1QMPMPsGKTqRc2XOaFQauDvoNz9fM1Wdkjmeo7l9GXOZiRs97sPkuayl39wjA==",
"license": "MIT",
"bin": {
"pidtree": "bin/pidtree.js"
},
"engines": {
"node": ">=0.10"
}
},
"node_modules/pidusage": {
"version": "2.0.21",
"resolved": "https://verdaccio.lossless.one/pidusage/-/pidusage-2.0.21.tgz",
"integrity": "sha512-cv3xAQos+pugVX+BfXpHsbyz/dLzX+lr44zNMsYiGxUw+kV5sgQCIcLd1z+0vq+KyC7dJ+/ts2PsfgWfSC3WXA==",
"license": "MIT",
"dependencies": {
"safe-buffer": "^5.2.1"
},
"engines": {
"node": ">=8"
}
},
"node_modules/pidusage/node_modules/safe-buffer": {
"version": "5.2.1",
"resolved": "https://verdaccio.lossless.one/safe-buffer/-/safe-buffer-5.2.1.tgz",
"integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT"
},
"node_modules/pkg-dir": { "node_modules/pkg-dir": {
"version": "4.2.0", "version": "4.2.0",
"resolved": "https://verdaccio.lossless.one/pkg-dir/-/pkg-dir-4.2.0.tgz", "resolved": "https://verdaccio.lossless.one/pkg-dir/-/pkg-dir-4.2.0.tgz",
@ -17032,6 +17085,11 @@
"@types/express-serve-static-core": "*" "@types/express-serve-static-core": "*"
} }
}, },
"@types/pidusage": {
"version": "2.0.1",
"resolved": "https://verdaccio.lossless.one/@types%2fpidusage/-/pidusage-2.0.1.tgz",
"integrity": "sha512-tYYcz/+5v/EGYT83C0pIXrJGOiVBLksQvxgJboG4nGqx/gZTvq0Ro4SkAjECqMk7L4Ww58VWB4j48qeYh4/YJg=="
},
"@types/puppeteer": { "@types/puppeteer": {
"version": "5.4.4", "version": "5.4.4",
"resolved": "https://verdaccio.lossless.one/@types%2fpuppeteer/-/puppeteer-5.4.4.tgz", "resolved": "https://verdaccio.lossless.one/@types%2fpuppeteer/-/puppeteer-5.4.4.tgz",
@ -22566,6 +22624,26 @@
"integrity": "sha512-lY1Q/PiJGC2zOv/z391WOTD+Z02bCgsFfvxoXXf6h7kv9o+WmsmzYqrAwY63sNgOxE4xEdq0WyUnXfKeBrSvYw==", "integrity": "sha512-lY1Q/PiJGC2zOv/z391WOTD+Z02bCgsFfvxoXXf6h7kv9o+WmsmzYqrAwY63sNgOxE4xEdq0WyUnXfKeBrSvYw==",
"dev": true "dev": true
}, },
"pidtree": {
"version": "0.5.0",
"resolved": "https://verdaccio.lossless.one/pidtree/-/pidtree-0.5.0.tgz",
"integrity": "sha512-9nxspIM7OpZuhBxPg73Zvyq7j1QMPMPsGKTqRc2XOaFQauDvoNz9fM1Wdkjmeo7l9GXOZiRs97sPkuayl39wjA=="
},
"pidusage": {
"version": "2.0.21",
"resolved": "https://verdaccio.lossless.one/pidusage/-/pidusage-2.0.21.tgz",
"integrity": "sha512-cv3xAQos+pugVX+BfXpHsbyz/dLzX+lr44zNMsYiGxUw+kV5sgQCIcLd1z+0vq+KyC7dJ+/ts2PsfgWfSC3WXA==",
"requires": {
"safe-buffer": "^5.2.1"
},
"dependencies": {
"safe-buffer": {
"version": "5.2.1",
"resolved": "https://verdaccio.lossless.one/safe-buffer/-/safe-buffer-5.2.1.tgz",
"integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ=="
}
}
},
"pkg-dir": { "pkg-dir": {
"version": "4.2.0", "version": "4.2.0",
"resolved": "https://verdaccio.lossless.one/pkg-dir/-/pkg-dir-4.2.0.tgz", "resolved": "https://verdaccio.lossless.one/pkg-dir/-/pkg-dir-4.2.0.tgz",

View File

@ -38,6 +38,9 @@
"dependencies": { "dependencies": {
"@pushrocks/smartdelay": "^2.0.13", "@pushrocks/smartdelay": "^2.0.13",
"@pushrocks/smartlog": "^2.0.44", "@pushrocks/smartlog": "^2.0.44",
"@types/pidusage": "^2.0.1",
"pidtree": "^0.5.0",
"pidusage": "^2.0.21",
"prom-client": "^13.2.0" "prom-client": "^13.2.0"
} }
} }

View File

@ -19,7 +19,8 @@ tap.test('should start smartmetrics', async () => {
testSmartMetrics.start(); testSmartMetrics.start();
}); });
tap.test('should produce valid metrics', async () => { tap.test('should produce valid metrics', async (tools) => {
console.log('calling .getMetrics from Testfile:')
console.log(await testSmartMetrics.getMetrics()); console.log(await testSmartMetrics.getMetrics());
}) })

View File

@ -20,7 +20,8 @@ export class SmartMetrics {
this.setup(); this.setup();
} }
public async start() { public start() {
const unattendedStart = async () => {
if (this.started) { if (this.started) {
return; return;
} }
@ -30,33 +31,10 @@ export class SmartMetrics {
eventType: 'heartbeat', eventType: 'heartbeat',
metrics: await this.getMetrics(), metrics: await this.getMetrics(),
}); });
await plugins.smartdelay.delayFor(60000, null, true); await plugins.smartdelay.delayFor(10000, null, true);
} }
} };
unattendedStart();
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 formatBytes(bytes: number, decimals = 2) { public formatBytes(bytes: number, decimals = 2) {
@ -73,6 +51,26 @@ export class SmartMetrics {
public async getMetrics() { public async getMetrics() {
const originalMetrics = await this.registry.getMetricsAsJSON(); 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 = { const returnMetrics: interfaces.IMetricsSnapshot = {
originalMetrics, originalMetrics,
process_cpu_seconds_total: ( process_cpu_seconds_total: (
@ -91,22 +89,11 @@ export class SmartMetrics {
(metricSet) => metricSet.name === 'nodejs_heap_size_total_bytes' (metricSet) => metricSet.name === 'nodejs_heap_size_total_bytes'
) as any ) as any
).values[0].value, ).values[0].value,
cpuPercentage: Math.round(this.getCpuUsagePercentage() * 100) / 100, cpuPercentage,
memoryUsageText: this.formatBytes( cpuUsageText,
( memoryPercentage,
originalMetrics.find( memoryUsageBytes,
(metricSet) => metricSet.name === 'nodejs_heap_size_total_bytes' memoryUsageText,
) 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; return returnMetrics;
} }

View File

@ -6,6 +6,8 @@ export interface IMetricsSnapshot {
nodejs_active_requests_total: number; nodejs_active_requests_total: number;
nodejs_heap_size_total_bytes: number; nodejs_heap_size_total_bytes: number;
cpuPercentage: number; cpuPercentage: number;
cpuUsageText: string;
memoryPercentage: number; memoryPercentage: number;
memoryUsageBytes: number;
memoryUsageText: string; memoryUsageText: string;
} }

View File

@ -15,8 +15,12 @@ export {
} }
// third party scope // third party scope
import pidusage from 'pidusage';
import pidtree from 'pidtree';
import * as promClient from 'prom-client'; import * as promClient from 'prom-client';
export { export {
pidusage,
pidtree,
promClient promClient
} }