4 Commits

Author SHA1 Message Date
66f0dc6815 2.0.2 2023-07-02 22:17:27 +02:00
7d703fe57e fix(core): update 2023-07-02 22:17:27 +02:00
2ddccdaab1 2.0.1 2022-07-27 12:30:50 +02:00
173b3e2b19 fix(core): update 2022-07-27 12:30:49 +02:00
5 changed files with 34 additions and 14 deletions

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@pushrocks/smartmetrics", "name": "@pushrocks/smartmetrics",
"version": "2.0.0", "version": "2.0.2",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@pushrocks/smartmetrics", "name": "@pushrocks/smartmetrics",
"version": "2.0.0", "version": "2.0.2",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@pushrocks/smartdelay": "^2.0.13", "@pushrocks/smartdelay": "^2.0.13",

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartmetrics", "name": "@pushrocks/smartmetrics",
"version": "2.0.0", "version": "2.0.2",
"private": false, "private": false,
"description": "easy system metrics", "description": "easy system metrics",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
@ -17,9 +17,7 @@
"@gitzone/tsbundle": "^2.0.6", "@gitzone/tsbundle": "^2.0.6",
"@gitzone/tstest": "^1.0.72", "@gitzone/tstest": "^1.0.72",
"@pushrocks/tapbundle": "^5.0.4", "@pushrocks/tapbundle": "^5.0.4",
"@types/node": "^18.6.1", "@types/node": "^18.6.1"
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.15.0"
}, },
"browserslist": [ "browserslist": [
"last 1 chrome versions" "last 1 chrome versions"

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@pushrocks/smartmetrics', name: '@pushrocks/smartmetrics',
version: '2.0.0', version: '2.0.2',
description: 'easy system metrics' description: 'easy system metrics'
} }

View File

@ -6,6 +6,7 @@ export class SmartMetrics {
public sourceNameArg: string; public sourceNameArg: string;
public logger: plugins.smartlog.Smartlog; public logger: plugins.smartlog.Smartlog;
public registry: plugins.promClient.Registry; public registry: plugins.promClient.Registry;
public maxMemoryMB: number;
public async setup() { public async setup() {
const collectDefaultMetrics = plugins.promClient.collectDefaultMetrics; const collectDefaultMetrics = plugins.promClient.collectDefaultMetrics;
@ -17,6 +18,27 @@ export class SmartMetrics {
this.logger = loggerArg; this.logger = loggerArg;
this.sourceNameArg = sourceNameArg; this.sourceNameArg = sourceNameArg;
this.setup(); this.setup();
this.checkMemoryLimits();
}
private checkMemoryLimits() {
let heapStats = plugins.v8.getHeapStatistics();
let maxHeapSizeMB = heapStats.heap_size_limit / 1024 / 1024;
let totalSystemMemoryMB = plugins.os.totalmem() / 1024 / 1024;
let dockerMemoryLimitMB = totalSystemMemoryMB;
try {
let dockerMemoryLimitBytes = plugins.fs.readFileSync('/sys/fs/cgroup/memory/memory.limit_in_bytes', 'utf8');
dockerMemoryLimitMB = parseInt(dockerMemoryLimitBytes, 10) / 1024 / 1024;
} catch (error) {
// Ignore - this will fail if not running in a Docker container
}
this.maxMemoryMB = Math.min(totalSystemMemoryMB, dockerMemoryLimitMB);
if (maxHeapSizeMB > this.maxMemoryMB) {
throw new Error('Node.js process can use more memory than is available');
}
} }
public start() { public start() {
@ -53,7 +75,6 @@ export class SmartMetrics {
const pids = await plugins.pidtree(process.pid); const pids = await plugins.pidtree(process.pid);
const stats = await plugins.pidusage([process.pid, ...pids]); const stats = await plugins.pidusage([process.pid, ...pids]);
// lets compute cpu usage
let cpuPercentage = 0; let cpuPercentage = 0;
for (const stat of Object.keys(stats)) { for (const stat of Object.keys(stats)) {
if (!stats[stat]) continue; if (!stats[stat]) continue;
@ -61,16 +82,15 @@ export class SmartMetrics {
} }
let cpuUsageText = `${Math.round(cpuPercentage * 100) / 100} %`; let cpuUsageText = `${Math.round(cpuPercentage * 100) / 100} %`;
// lets compute memory usage
let memoryUsageBytes = 0; let memoryUsageBytes = 0;
for (const stat of Object.keys(stats)) { for (const stat of Object.keys(stats)) {
if (!stats[stat]) continue; if (!stats[stat]) continue;
memoryUsageBytes += stats[stat].memory; memoryUsageBytes += stats[stat].memory;
} }
let memoryPercentage = Math.round((memoryUsageBytes / 1000000000) * 100 * 100) / 100;
let memoryUsageText = `${memoryPercentage}% | ${this.formatBytes( // Correct memory usage percentage calculation
memoryUsageBytes let memoryPercentage = Math.round((memoryUsageBytes / (this.maxMemoryMB * 1024 * 1024)) * 100 * 100) / 100;
)} / ${this.formatBytes(1000000000)}`; let memoryUsageText = `${memoryPercentage}% | ${this.formatBytes(memoryUsageBytes)} / ${this.formatBytes(this.maxMemoryMB * 1024 * 1024)}`;
console.log(`${cpuUsageText} ||| ${memoryUsageText} `); console.log(`${cpuUsageText} ||| ${memoryUsageText} `);

View File

@ -1,7 +1,9 @@
// node native // node native
import * as v8 from 'v8';
import * as os from 'os'; import * as os from 'os';
import * as fs from 'fs';
export { os }; export { v8, os, fs };
// pushrocks scope // pushrocks scope
import * as smartdelay from '@pushrocks/smartdelay'; import * as smartdelay from '@pushrocks/smartdelay';