224 lines
7.4 KiB
TypeScript
224 lines
7.4 KiB
TypeScript
import * as plugins from './smartmetrics.plugins.js';
|
|
import * as interfaces from './smartmetrics.interfaces.js';
|
|
|
|
export class SmartMetrics {
|
|
public started = false;
|
|
public sourceNameArg: string;
|
|
public logger: plugins.smartlog.Smartlog;
|
|
public registry: plugins.promClient.Registry;
|
|
public maxMemoryMB: number;
|
|
|
|
// Prometheus gauges for custom metrics
|
|
private cpuPercentageGauge: plugins.promClient.Gauge<string>;
|
|
private memoryPercentageGauge: plugins.promClient.Gauge<string>;
|
|
private memoryUsageBytesGauge: plugins.promClient.Gauge<string>;
|
|
|
|
// HTTP server for Prometheus endpoint
|
|
private prometheusServer?: plugins.http.Server;
|
|
private prometheusPort?: number;
|
|
|
|
public async setup() {
|
|
const collectDefaultMetrics = plugins.promClient.collectDefaultMetrics;
|
|
this.registry = new plugins.promClient.Registry();
|
|
collectDefaultMetrics({ register: this.registry });
|
|
|
|
// Initialize custom gauges
|
|
this.cpuPercentageGauge = new plugins.promClient.Gauge({
|
|
name: 'smartmetrics_cpu_percentage',
|
|
help: 'Current CPU usage percentage',
|
|
registers: [this.registry]
|
|
});
|
|
|
|
this.memoryPercentageGauge = new plugins.promClient.Gauge({
|
|
name: 'smartmetrics_memory_percentage',
|
|
help: 'Current memory usage percentage',
|
|
registers: [this.registry]
|
|
});
|
|
|
|
this.memoryUsageBytesGauge = new plugins.promClient.Gauge({
|
|
name: 'smartmetrics_memory_usage_bytes',
|
|
help: 'Current memory usage in bytes',
|
|
registers: [this.registry]
|
|
});
|
|
}
|
|
|
|
constructor(loggerArg: plugins.smartlog.Smartlog, sourceNameArg: string) {
|
|
this.logger = loggerArg;
|
|
this.sourceNameArg = sourceNameArg;
|
|
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() {
|
|
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(20000, null, true);
|
|
}
|
|
};
|
|
unattendedStart();
|
|
}
|
|
|
|
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 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;
|
|
cpuPercentage += stats[stat].cpu;
|
|
}
|
|
let cpuUsageText = `${Math.round(cpuPercentage * 100) / 100} %`;
|
|
|
|
let memoryUsageBytes = 0;
|
|
for (const stat of Object.keys(stats)) {
|
|
if (!stats[stat]) continue;
|
|
memoryUsageBytes += stats[stat].memory;
|
|
}
|
|
|
|
let memoryPercentage =
|
|
Math.round((memoryUsageBytes / (this.maxMemoryMB * 1024 * 1024)) * 100 * 100) / 100;
|
|
let memoryUsageText = `${memoryPercentage}% | ${this.formatBytes(
|
|
memoryUsageBytes
|
|
)} / ${this.formatBytes(this.maxMemoryMB * 1024 * 1024)}`;
|
|
|
|
console.log(`${cpuUsageText} ||| ${memoryUsageText} `);
|
|
|
|
// Update Prometheus gauges with current values
|
|
if (this.cpuPercentageGauge) {
|
|
this.cpuPercentageGauge.set(cpuPercentage);
|
|
}
|
|
if (this.memoryPercentageGauge) {
|
|
this.memoryPercentageGauge.set(memoryPercentage);
|
|
}
|
|
if (this.memoryUsageBytesGauge) {
|
|
this.memoryUsageBytesGauge.set(memoryUsageBytes);
|
|
}
|
|
|
|
// Calculate Node.js metrics directly
|
|
const cpuUsage = process.cpuUsage();
|
|
const process_cpu_seconds_total = (cpuUsage.user + cpuUsage.system) / 1000000; // Convert from microseconds to seconds
|
|
|
|
const heapStats = plugins.v8.getHeapStatistics();
|
|
const nodejs_heap_size_total_bytes = heapStats.total_heap_size;
|
|
|
|
// Note: Active handles and requests are internal Node.js metrics that require deprecated APIs
|
|
// We return 0 here, but the Prometheus default collectors will track the real values
|
|
const nodejs_active_handles_total = 0;
|
|
const nodejs_active_requests_total = 0;
|
|
|
|
const returnMetrics: interfaces.IMetricsSnapshot = {
|
|
process_cpu_seconds_total,
|
|
nodejs_active_handles_total,
|
|
nodejs_active_requests_total,
|
|
nodejs_heap_size_total_bytes,
|
|
cpuPercentage,
|
|
cpuUsageText,
|
|
memoryPercentage,
|
|
memoryUsageBytes,
|
|
memoryUsageText,
|
|
};
|
|
return returnMetrics;
|
|
}
|
|
|
|
public async getPrometheusFormattedMetrics(): Promise<string> {
|
|
// Update metrics to ensure gauges have latest values
|
|
await this.getMetrics();
|
|
|
|
// Return Prometheus text exposition format
|
|
return await this.registry.metrics();
|
|
}
|
|
|
|
public enablePrometheusEndpoint(port: number = 9090): void {
|
|
if (this.prometheusServer) {
|
|
this.logger.log('warn', 'Prometheus endpoint is already running');
|
|
return;
|
|
}
|
|
|
|
this.prometheusServer = plugins.http.createServer(async (req, res) => {
|
|
if (req.url === '/metrics' && req.method === 'GET') {
|
|
try {
|
|
const metrics = await this.getPrometheusFormattedMetrics();
|
|
res.writeHead(200, { 'Content-Type': 'text/plain; version=0.0.4' });
|
|
res.end(metrics);
|
|
} catch (error) {
|
|
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
|
res.end('Error generating metrics');
|
|
this.logger.log('error', 'Error generating Prometheus metrics', error);
|
|
}
|
|
} else {
|
|
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
res.end('Not Found');
|
|
}
|
|
});
|
|
|
|
this.prometheusPort = port;
|
|
this.prometheusServer.listen(port, () => {
|
|
this.logger.log('info', `Prometheus metrics endpoint available at http://localhost:${port}/metrics`);
|
|
});
|
|
}
|
|
|
|
public disablePrometheusEndpoint(): void {
|
|
if (!this.prometheusServer) {
|
|
return;
|
|
}
|
|
|
|
const port = this.prometheusPort;
|
|
this.prometheusServer.close(() => {
|
|
this.logger.log('info', `Prometheus metrics endpoint on port ${port} has been shut down`);
|
|
});
|
|
|
|
this.prometheusServer = undefined;
|
|
this.prometheusPort = undefined;
|
|
}
|
|
|
|
public stop() {
|
|
this.started = false;
|
|
this.disablePrometheusEndpoint();
|
|
}
|
|
}
|