5 Commits

6 changed files with 3414 additions and 1640 deletions

View File

@@ -1,5 +1,22 @@
# Changelog # Changelog
## 2026-02-19 - 2.0.11 - fix(deps)
bump dependencies, update build script, expand README and npm metadata
- Bumped runtime deps: @push.rocks/smartdelay ^3.0.5, @push.rocks/smartlog ^3.1.11
- Updated devDependencies: @git.zone/tsbuild, tsbundle, tsrun, tstest and @types/node versions
- Changed build script: "(tsbuild --web)" → "(tsbuild tsfolders)"
- Updated npmextra.json: renamed keys (gitzone → @git.zone/cli, tsdoc → @git.zone/tsdoc), added release registries and accessLevel, and added @ship.zone/szci entry
- Extensive README improvements: installation notes (pnpm), clearer API docs, examples, added Issue Reporting & Security section and utility docs (formatBytes)
## 2025-06-09 - 2.0.9 - fix(readme)
Update documentation with detailed usage instructions, API references and integration examples.
- Overhauled README to provide a clearer explanation of SmartMetrics features and API.
- Added a quick start guide, detailed examples, and code snippets for various integrations (Express, PM2, custom dashboards).
- Reorganized documentation sections to better highlight core concepts including process aggregation and memory limit detection.
- Updated installation instructions and usage examples to reflect the latest functionality.
## 2025-06-09 - 2.0.8 - fix(smartmetrics) ## 2025-06-09 - 2.0.8 - fix(smartmetrics)
Refactor metrics calculation and update Prometheus integration documentation Refactor metrics calculation and update Prometheus integration documentation

View File

@@ -1,5 +1,5 @@
{ {
"gitzone": { "@git.zone/cli": {
"projectType": "npm", "projectType": "npm",
"module": { "module": {
"githost": "code.foss.global", "githost": "code.foss.global",
@@ -25,13 +25,19 @@
"log reporting", "log reporting",
"operational insights" "operational insights"
] ]
},
"release": {
"registries": [
"https://verdaccio.lossless.digital",
"https://registry.npmjs.org"
],
"accessLevel": "public"
} }
}, },
"npmci": { "@git.zone/tsdoc": {
"npmGlobalTools": [],
"npmAccessLevel": "public"
},
"tsdoc": {
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n" "legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
},
"@ship.zone/szci": {
"npmGlobalTools": []
} }
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartmetrics", "name": "@push.rocks/smartmetrics",
"version": "2.0.8", "version": "2.0.11",
"private": false, "private": false,
"description": "A package for easy collection and reporting of system and process metrics.", "description": "A package for easy collection and reporting of system and process metrics.",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
@@ -9,15 +9,15 @@
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"test": "(tstest test/ --verbose)", "test": "(tstest test/ --verbose)",
"build": "(tsbuild --web)", "build": "(tsbuild tsfolders)",
"buildDocs": "tsdoc" "buildDocs": "tsdoc"
}, },
"devDependencies": { "devDependencies": {
"@git.zone/tsbuild": "^2.6.4", "@git.zone/tsbuild": "^4.1.2",
"@git.zone/tsbundle": "^2.0.8", "@git.zone/tsbundle": "^2.8.3",
"@git.zone/tsrun": "^1.2.44", "@git.zone/tsrun": "^2.0.1",
"@git.zone/tstest": "^2.3.1", "@git.zone/tstest": "^3.1.8",
"@types/node": "^22.15.30" "@types/node": "^25.3.0"
}, },
"browserslist": [ "browserslist": [
"last 1 chrome versions" "last 1 chrome versions"
@@ -35,8 +35,8 @@
"readme.md" "readme.md"
], ],
"dependencies": { "dependencies": {
"@push.rocks/smartdelay": "^3.0.1", "@push.rocks/smartdelay": "^3.0.5",
"@push.rocks/smartlog": "^3.0.2", "@push.rocks/smartlog": "^3.1.11",
"@types/pidusage": "^2.0.2", "@types/pidusage": "^2.0.2",
"pidtree": "^0.6.0", "pidtree": "^0.6.0",
"pidusage": "^4.0.1", "pidusage": "^4.0.1",

4675
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

324
readme.md
View File

@@ -1,147 +1,285 @@
# @push.rocks/smartmetrics # @push.rocks/smartmetrics
easy system metrics **Powerful system metrics collection for Node.js applications with Prometheus integration** 🚀
## Install
To include `@push.rocks/smartmetrics` in your project, you need a Node.js environment with support for ES Modules. Make sure that your `package.json` contains `"type": "module"`. You can install `@push.rocks/smartmetrics` using npm: ## Issue Reporting and Security
For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
## What is SmartMetrics?
SmartMetrics is a comprehensive metrics collection library that monitors your Node.js application's resource usage in real-time. It tracks CPU usage, memory consumption, and system metrics across your main process and all child processes, providing insights through both JSON and Prometheus formats.
## Key Features
- 📊 **Real-time Metrics Collection** Monitor CPU and memory usage across all processes
- 🔄 **Automatic Child Process Tracking** Aggregates metrics from main and child processes via `pidtree` + `pidusage`
- 🐳 **Docker-Aware** Detects container memory limits from cgroup automatically
- 📈 **Prometheus Integration** Built-in HTTP endpoint for Prometheus scraping with `prom-client`
- 🔧 **Flexible Output Formats** Get metrics as JSON objects or Prometheus text exposition format
- 📝 **Automatic Heartbeat Logging** Optional periodic metrics logging via `@push.rocks/smartlog`
- 🚀 **Zero Configuration** Works out of the box with sensible defaults
## Installation
```bash ```bash
npm install @push.rocks/smartmetrics --save pnpm install @push.rocks/smartmetrics
# or
npm install @push.rocks/smartmetrics
``` ```
Or using yarn: ## Quick Start
```bash
yarn add @push.rocks/smartmetrics
```
## Usage
The `@push.rocks/smartmetrics` package provides an easy way to collect and monitor system metrics such as CPU and memory usage within your Node.js applications. It's built with TypeScript, offering type safety and IntelliSense in editors that support it.
### Getting Started
Before diving into the code, ensure you have `@push.rocks/smartmetrics` installed in your project and your environment is configured to use ES Modules (ESM) and TypeScript.
#### Initialization
To begin using `smartmetrics`, you need to import the module and initialize it:
```typescript ```typescript
// Import dependencies
import { SmartMetrics } from '@push.rocks/smartmetrics'; import { SmartMetrics } from '@push.rocks/smartmetrics';
import { Smartlog } from '@push.rocks/smartlog'; import { Smartlog } from '@push.rocks/smartlog';
// Setup a logger (or use your existing logger if applicable) // Create a logger instance
const logger = new Smartlog({ const logger = new Smartlog({
logContext: 'applicationName', logContext: null,
minimumLogLevel: 'info', minimumLogLevel: 'info',
}); });
logger.enableConsole();
// Initialize smartmetrics with the prepared logger // Initialize SmartMetrics
const smartMetrics = new SmartMetrics(logger, 'sourceName'); const metrics = new SmartMetrics(logger, 'my-service');
// Get metrics on-demand
const currentMetrics = await metrics.getMetrics();
console.log(`CPU Usage: ${currentMetrics.cpuUsageText}`);
console.log(`Memory: ${currentMetrics.memoryUsageText}`);
// Enable automatic heartbeat logging (every 20 seconds)
metrics.start();
// Enable Prometheus endpoint
metrics.enablePrometheusEndpoint(9090);
// Metrics now available at http://localhost:9090/metrics
// Clean shutdown
metrics.stop();
``` ```
In this snippet, `Smartlog` is used for logging purposes. Replace `'applicationName'` and `'sourceName'` with appropriate values for your application. ## Core Concepts
#### Starting Metrics Monitoring ### Process Aggregation
With `smartMetrics` initialized, you can start the metrics monitoring process: SmartMetrics doesn't just monitor your main process it automatically discovers and aggregates metrics from all child processes spawned by your application using `pidtree`. This gives you a complete picture of your application's resource footprint, not just the parent process.
### Memory Limit Detection
The library automatically detects available memory whether running on bare metal, in Docker containers, or with Node.js heap restrictions. It picks the most restrictive of:
1. **System total memory** (`os.totalmem()`)
2. **Docker cgroup limit** (`/sys/fs/cgroup/memory/memory.limit_in_bytes`)
3. **V8 heap size limit** (`v8.getHeapStatistics().heap_size_limit`)
This ensures accurate percentage calculations regardless of environment.
### Dual Output Formats
- **JSON Format** (`getMetrics()`) Ideal for application monitoring, custom dashboards, and programmatic access
- **Prometheus Format** (`getPrometheusFormattedMetrics()`) Perfect for integration with Prometheus/Grafana monitoring stacks
## API Reference
### `new SmartMetrics(logger, sourceName)`
Creates a new SmartMetrics instance.
| Parameter | Type | Description |
|-----------|------|-------------|
| `logger` | `Smartlog` | A `@push.rocks/smartlog` logger instance |
| `sourceName` | `string` | Identifier for your service/application |
### `async getMetrics(): Promise<IMetricsSnapshot>`
Retrieves current system metrics as a structured object.
**Returns `IMetricsSnapshot`:**
```typescript
{
process_cpu_seconds_total: number; // Total CPU time in seconds
nodejs_active_handles_total: number; // Active handles count
nodejs_active_requests_total: number; // Active requests count
nodejs_heap_size_total_bytes: number; // V8 heap size in bytes
cpuPercentage: number; // Aggregated CPU usage across all child processes
cpuUsageText: string; // Human-readable CPU usage (e.g. "12.5 %")
memoryPercentage: number; // Memory usage as percentage of detected limit
memoryUsageBytes: number; // Total memory in bytes across all child processes
memoryUsageText: string; // Human-readable (e.g. "45% | 920 MB / 2 GB")
}
```
**Example:**
```typescript
const metrics = await smartMetrics.getMetrics();
if (metrics.cpuPercentage > 80) {
console.warn('High CPU usage detected!');
}
```
### `start(): void`
Starts automatic metrics collection and heartbeat logging every 20 seconds via the provided `Smartlog` instance.
```typescript ```typescript
smartMetrics.start(); smartMetrics.start();
// Logs: "sending heartbeat for my-service with metrics" every 20 seconds
``` ```
This starts the collection and monitoring of various system metrics. By default, `smartMetrics` sends heartbeat messages including these metrics at a regular interval. ### `stop(): void`
#### Metrics Collection Stops automatic metrics collection, closes heartbeat loop, and shuts down any open Prometheus endpoints.
To manually collect metrics at any point, you can call the `getMetrics` method. This could be useful for logging or sending metrics to a monitoring tool: ### `async getPrometheusFormattedMetrics(): Promise<string>`
Returns all metrics in Prometheus text exposition format, including default Node.js collectors and custom SmartMetrics gauges.
```typescript ```typescript
async function logMetrics() { const promMetrics = await smartMetrics.getPrometheusFormattedMetrics();
const metrics = await smartMetrics.getMetrics(); // Returns:
console.log(metrics); // # HELP smartmetrics_cpu_percentage Current CPU usage percentage
} // # TYPE smartmetrics_cpu_percentage gauge
// smartmetrics_cpu_percentage 15.2
logMetrics(); // # HELP smartmetrics_memory_percentage Current memory usage percentage
// # TYPE smartmetrics_memory_percentage gauge
// smartmetrics_memory_percentage 45.3
// # HELP smartmetrics_memory_usage_bytes Current memory usage in bytes
// # TYPE smartmetrics_memory_usage_bytes gauge
// smartmetrics_memory_usage_bytes 965214208
// ... plus all default Node.js metrics from prom-client
``` ```
#### Customizing Monitoring ### `enablePrometheusEndpoint(port?: number): void`
The `start` method begins an unattended process of collecting and logging metrics. If you require more control over how and when metrics are collected or reported, you can use the `getMetrics` method in combination with your logic. Starts an HTTP server that exposes metrics at `/metrics` for Prometheus scraping.
#### Stopping Metrics Monitoring | Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
To stop the automatic metrics monitoring, simply call: | `port` | `number` | `9090` | Port to listen on |
```typescript ```typescript
smartMetrics.stop();
```
This stops the internal loop that periodically collects and logs metrics. It's useful for clean shutdowns of your application or when metrics monitoring is only needed during specific intervals.
### Understanding the Metrics
The `getMetrics` method returns a snapshot of various system metrics, including CPU and memory usage. Here's a brief overview of the information provided:
- `process_cpu_seconds_total`: Total CPU time spent by the process.
- `nodejs_active_handles_total`: Number of active handles.
- `nodejs_active_requests_total`: Number of active requests.
- `nodejs_heap_size_total_bytes`: Total size of the heap.
- `cpuPercentage`: Overall CPU usage percentage.
- `cpuUsageText`: Readable string representation of CPU usage.
- `memoryPercentage`: Percentage of memory used.
- `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); smartMetrics.enablePrometheusEndpoint(3000);
// GET http://localhost:3000/metrics → Prometheus text format
// The metrics will be available at http://localhost:9090/metrics // GET http://localhost:3000/anything-else → 404
// 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: ### `disablePrometheusEndpoint(): void`
- `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 Gracefully shuts down the Prometheus HTTP server.
`@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. ### `formatBytes(bytes, decimals?): string`
Utility method to convert byte values to human-readable strings.
```typescript
smartMetrics.formatBytes(1073741824); // "1 GB"
smartMetrics.formatBytes(1536, 1); // "1.5 KB"
```
## Use Cases
### 🔍 Application Performance Monitoring
```typescript
const metricsBefore = await smartMetrics.getMetrics();
await performHeavyOperation();
const metricsAfter = await smartMetrics.getMetrics();
console.log(`Operation consumed ${
metricsAfter.process_cpu_seconds_total - metricsBefore.process_cpu_seconds_total
} CPU seconds`);
```
### 🛡️ Resource Limit Enforcement
```typescript
async function checkResources() {
const metrics = await smartMetrics.getMetrics();
if (metrics.memoryPercentage > 90) {
throw new Error('Memory usage too high, refusing new operations');
}
if (metrics.cpuPercentage > 95) {
await delay(1000); // Back off when CPU is stressed
}
}
```
### 📈 Prometheus + Grafana Stack
```typescript
smartMetrics.enablePrometheusEndpoint(9090);
// prometheus.yml:
// scrape_configs:
// - job_name: 'my-app'
// scrape_interval: 15s
// static_configs:
// - targets: ['localhost:9090']
```
### 🐳 Container Resource Monitoring
```typescript
// Automatically detects Docker/cgroup memory limits
const metrics = await smartMetrics.getMetrics();
console.log(metrics.memoryUsageText);
// Output: "45% | 920 MB / 2 GB" (container limit detected)
```
### 🔄 Health Check Endpoint
```typescript
import express from 'express';
const app = express();
app.get('/health', async (req, res) => {
const metrics = await smartMetrics.getMetrics();
res.json({
status: metrics.memoryPercentage < 90 ? 'healthy' : 'degraded',
cpu: metrics.cpuUsageText,
memory: metrics.memoryUsageText,
});
});
```
### 🔁 Graceful Restart on High Memory (PM2)
```typescript
setInterval(async () => {
const metrics = await smartMetrics.getMetrics();
if (metrics.memoryPercentage > 95) {
console.error('Memory limit reached, requesting restart');
process.exit(0); // PM2 will restart the process
}
}, 10000);
```
## License and Legal Information ## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file. **Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks ### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH. This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
### Company Information ### Company Information
Task Venture Capital GmbH Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany Registered at District Court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc. For any legal inquiries or further information, please contact us via email at hello@task.vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works. By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.

View File

@@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartmetrics', name: '@push.rocks/smartmetrics',
version: '2.0.8', version: '2.0.11',
description: 'A package for easy collection and reporting of system and process metrics.' description: 'A package for easy collection and reporting of system and process metrics.'
} }