fix(smartmetrics): Refactor metrics calculation and update Prometheus integration documentation
This commit is contained in:
@ -104,7 +104,6 @@ export class SmartMetrics {
|
||||
}
|
||||
|
||||
public async getMetrics() {
|
||||
const originalMetrics = await this.registry.getMetricsAsJSON();
|
||||
const pids = await plugins.pidtree(process.pid);
|
||||
const stats = await plugins.pidusage([process.pid, ...pids]);
|
||||
|
||||
@ -140,23 +139,23 @@ export class SmartMetrics {
|
||||
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: (
|
||||
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,
|
||||
process_cpu_seconds_total,
|
||||
nodejs_active_handles_total,
|
||||
nodejs_active_requests_total,
|
||||
nodejs_heap_size_total_bytes,
|
||||
cpuPercentage,
|
||||
cpuUsageText,
|
||||
memoryPercentage,
|
||||
@ -208,8 +207,9 @@ export class SmartMetrics {
|
||||
return;
|
||||
}
|
||||
|
||||
const port = this.prometheusPort;
|
||||
this.prometheusServer.close(() => {
|
||||
this.logger.log('info', `Prometheus metrics endpoint on port ${this.prometheusPort} has been shut down`);
|
||||
this.logger.log('info', `Prometheus metrics endpoint on port ${port} has been shut down`);
|
||||
});
|
||||
|
||||
this.prometheusServer = undefined;
|
||||
|
Reference in New Issue
Block a user