fix(smartmetrics): Refactor metrics calculation and update Prometheus integration documentation

This commit is contained in:
Juergen Kunz
2025-06-09 12:07:24 +00:00
parent 34b09ed7a7
commit 30992ea44b
6 changed files with 112 additions and 24 deletions

View File

@ -96,6 +96,33 @@ The `getMetrics` method returns a snapshot of various system metrics, including
- `memoryUsageBytes`: Total memory used in bytes.
- `memoryUsageText`: Readable string representation of memory usage.
### Prometheus Integration
`smartmetrics` can expose metrics in Prometheus format for scraping:
```typescript
// Enable Prometheus endpoint on port 9090 (default)
smartMetrics.enablePrometheusEndpoint();
// Or specify a custom port
smartMetrics.enablePrometheusEndpoint(3000);
// The metrics will be available at http://localhost:9090/metrics
// To get Prometheus-formatted metrics programmatically
const prometheusMetrics = await smartMetrics.getPrometheusFormattedMetrics();
console.log(prometheusMetrics);
// Disable the endpoint when done
smartMetrics.disablePrometheusEndpoint();
```
The Prometheus endpoint exposes both default Node.js metrics (via prom-client) and custom calculated metrics:
- `smartmetrics_cpu_percentage` - Current CPU usage percentage
- `smartmetrics_memory_percentage` - Current memory usage percentage
- `smartmetrics_memory_usage_bytes` - Current memory usage in bytes
- Plus all default Node.js metrics collected by prom-client
### Conclusion
`@push.rocks/smartmetrics` offers a straightforward and efficient way to monitor essential system metrics of your Node.js application. By integrating it, you gain valuable insights into the performance and health of your system, aiding in diagnosis and optimization efforts.