smartmetrics/ts/smartmetrics.classes.smartmetrics.ts

134 lines
4.4 KiB
TypeScript
Raw Normal View History

2022-07-27 10:00:38 +00:00
import * as plugins from './smartmetrics.plugins.js';
import * as interfaces from './smartmetrics.interfaces.js';
2021-08-12 21:19:39 +00:00
export class SmartMetrics {
public started = false;
public sourceNameArg: string;
public logger: plugins.smartlog.Smartlog;
public registry: plugins.promClient.Registry;
2023-07-02 20:17:27 +00:00
public maxMemoryMB: number;
2021-08-12 21:19:39 +00:00
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();
2023-07-02 20:17:27 +00:00
this.checkMemoryLimits();
}
private checkMemoryLimits() {
let heapStats = plugins.v8.getHeapStatistics();
let maxHeapSizeMB = heapStats.heap_size_limit / 1024 / 1024;
let totalSystemMemoryMB = plugins.os.totalmem() / 1024 / 1024;
2023-07-03 09:08:27 +00:00
let dockerMemoryLimitMB = totalSystemMemoryMB;
2023-07-02 20:17:27 +00:00
try {
2023-07-03 09:08:27 +00:00
let dockerMemoryLimitBytes = plugins.fs.readFileSync(
'/sys/fs/cgroup/memory/memory.limit_in_bytes',
'utf8'
);
2023-07-02 20:17:27 +00:00
dockerMemoryLimitMB = parseInt(dockerMemoryLimitBytes, 10) / 1024 / 1024;
} catch (error) {
// Ignore - this will fail if not running in a Docker container
}
2023-07-02 20:23:46 +00:00
// 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
2023-07-02 20:17:27 +00:00
if (maxHeapSizeMB > this.maxMemoryMB) {
throw new Error('Node.js process can use more memory than is available');
}
2021-08-12 21:19:39 +00:00
}
2021-08-14 15:01:54 +00:00
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(),
});
2021-08-19 17:22:29 +00:00
await plugins.smartdelay.delayFor(20000, null, true);
2021-08-14 15:01:54 +00:00
}
};
unattendedStart();
2021-08-12 21:19:39 +00:00
}
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();
2021-08-14 15:01:54 +00:00
const pids = await plugins.pidtree(process.pid);
const stats = await plugins.pidusage([process.pid, ...pids]);
let cpuPercentage = 0;
for (const stat of Object.keys(stats)) {
if (!stats[stat]) continue;
2021-08-14 20:47:31 +00:00
cpuPercentage += stats[stat].cpu;
2021-08-14 15:01:54 +00:00
}
2021-08-14 20:47:31 +00:00
let cpuUsageText = `${Math.round(cpuPercentage * 100) / 100} %`;
2021-08-14 15:01:54 +00:00
let memoryUsageBytes = 0;
for (const stat of Object.keys(stats)) {
if (!stats[stat]) continue;
memoryUsageBytes += stats[stat].memory;
}
2023-07-02 20:17:27 +00:00
2023-07-03 09:08:27 +00:00
let memoryPercentage =
Math.round((memoryUsageBytes / (this.maxMemoryMB * 1024 * 1024)) * 100 * 100) / 100;
let memoryUsageText = `${memoryPercentage}% | ${this.formatBytes(
memoryUsageBytes
)} / ${this.formatBytes(this.maxMemoryMB * 1024 * 1024)}`;
2021-08-14 15:01:54 +00:00
2021-08-14 19:41:06 +00:00
console.log(`${cpuUsageText} ||| ${memoryUsageText} `);
2021-08-14 19:37:52 +00:00
2021-08-12 21:19:39 +00:00
const returnMetrics: interfaces.IMetricsSnapshot = {
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,
2021-08-14 15:01:54 +00:00
cpuPercentage,
cpuUsageText,
memoryPercentage,
memoryUsageBytes,
memoryUsageText,
2021-08-12 21:19:39 +00:00
};
return returnMetrics;
}
2021-08-14 15:02:40 +00:00
public stop() {
this.started = false;
}
2021-08-12 21:19:39 +00:00
}