Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
8454244f3b | |||
4b6d6feea1 | |||
01306ed3f9 | |||
2a2ca66708 | |||
299e50cbf7 | |||
1e6af24df0 | |||
66f0dc6815 | |||
7d703fe57e |
15165
package-lock.json
generated
15165
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
21
package.json
21
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartmetrics",
|
"name": "@pushrocks/smartmetrics",
|
||||||
"version": "2.0.1",
|
"version": "2.0.5",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "easy system metrics",
|
"description": "easy system metrics",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
@ -13,11 +13,12 @@
|
|||||||
"buildDocs": "tsdoc"
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.63",
|
"@gitzone/tsbuild": "^2.1.66",
|
||||||
"@gitzone/tsbundle": "^2.0.6",
|
"@gitzone/tsbundle": "^2.0.8",
|
||||||
"@gitzone/tstest": "^1.0.72",
|
"@gitzone/tsrun": "^1.2.42",
|
||||||
"@pushrocks/tapbundle": "^5.0.4",
|
"@gitzone/tstest": "^1.0.74",
|
||||||
"@types/node": "^18.6.1"
|
"@pushrocks/tapbundle": "^5.0.8",
|
||||||
|
"@types/node": "^20.3.3"
|
||||||
},
|
},
|
||||||
"browserslist": [
|
"browserslist": [
|
||||||
"last 1 chrome versions"
|
"last 1 chrome versions"
|
||||||
@ -35,12 +36,12 @@
|
|||||||
"readme.md"
|
"readme.md"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartdelay": "^2.0.13",
|
"@pushrocks/smartdelay": "^3.0.1",
|
||||||
"@pushrocks/smartlog": "^3.0.1",
|
"@pushrocks/smartlog": "^3.0.2",
|
||||||
"@types/pidusage": "^2.0.2",
|
"@types/pidusage": "^2.0.2",
|
||||||
"pidtree": "^0.6.0",
|
"pidtree": "^0.6.0",
|
||||||
"pidusage": "^3.0.0",
|
"pidusage": "^3.0.2",
|
||||||
"prom-client": "^14.0.1"
|
"prom-client": "^14.2.0"
|
||||||
},
|
},
|
||||||
"type": "module"
|
"type": "module"
|
||||||
}
|
}
|
||||||
|
4605
pnpm-lock.yaml
generated
Normal file
4605
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@pushrocks/smartmetrics',
|
name: '@pushrocks/smartmetrics',
|
||||||
version: '2.0.1',
|
version: '2.0.5',
|
||||||
description: 'easy system metrics'
|
description: 'easy system metrics'
|
||||||
}
|
}
|
||||||
|
@ -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,29 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the maximum memory to the lower value between the Docker limit and the total system memory
|
||||||
|
this.maxMemoryMB = Math.min(totalSystemMemoryMB, dockerMemoryLimitMB, maxHeapSizeMB);
|
||||||
|
|
||||||
|
// If the maximum old space size limit is greater than the maximum available memory, throw an error
|
||||||
|
if (maxHeapSizeMB > this.maxMemoryMB) {
|
||||||
|
throw new Error('Node.js process can use more memory than is available');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public start() {
|
public start() {
|
||||||
@ -53,7 +77,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 +84,14 @@ 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(
|
let memoryPercentage = Math.round((memoryUsageBytes / (this.maxMemoryMB * 1024 * 1024)) * 100 * 100) / 100;
|
||||||
memoryUsageBytes
|
let memoryUsageText = `${memoryPercentage}% | ${this.formatBytes(memoryUsageBytes)} / ${this.formatBytes(this.maxMemoryMB * 1024 * 1024)}`;
|
||||||
)} / ${this.formatBytes(1000000000)}`;
|
|
||||||
|
|
||||||
console.log(`${cpuUsageText} ||| ${memoryUsageText} `);
|
console.log(`${cpuUsageText} ||| ${memoryUsageText} `);
|
||||||
|
|
||||||
|
@ -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';
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
"target": "ES2022",
|
"target": "ES2022",
|
||||||
"module": "ES2022",
|
"module": "ES2022",
|
||||||
"moduleResolution": "nodenext",
|
"moduleResolution": "nodenext",
|
||||||
"esModuleInterop": true
|
"esModuleInterop": true,
|
||||||
|
"verbatimModuleSyntax": true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user