1 Commits

Author SHA1 Message Date
77675c6367 2.0.9 2025-06-09 12:34:06 +00:00
6 changed files with 1632 additions and 3274 deletions

View File

@@ -1,14 +1,5 @@
# 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) ## 2025-06-09 - 2.0.9 - fix(readme)
Update documentation with detailed usage instructions, API references and integration examples. Update documentation with detailed usage instructions, API references and integration examples.

View File

@@ -1,5 +1,5 @@
{ {
"@git.zone/cli": { "gitzone": {
"projectType": "npm", "projectType": "npm",
"module": { "module": {
"githost": "code.foss.global", "githost": "code.foss.global",
@@ -25,19 +25,13 @@
"log reporting", "log reporting",
"operational insights" "operational insights"
] ]
},
"release": {
"registries": [
"https://verdaccio.lossless.digital",
"https://registry.npmjs.org"
],
"accessLevel": "public"
} }
}, },
"@git.zone/tsdoc": { "npmci": {
"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" "npmGlobalTools": [],
"npmAccessLevel": "public"
}, },
"@ship.zone/szci": { "tsdoc": {
"npmGlobalTools": [] "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"
} }
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartmetrics", "name": "@push.rocks/smartmetrics",
"version": "2.0.11", "version": "2.0.9",
"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 tsfolders)", "build": "(tsbuild --web)",
"buildDocs": "tsdoc" "buildDocs": "tsdoc"
}, },
"devDependencies": { "devDependencies": {
"@git.zone/tsbuild": "^4.1.2", "@git.zone/tsbuild": "^2.6.4",
"@git.zone/tsbundle": "^2.8.3", "@git.zone/tsbundle": "^2.0.8",
"@git.zone/tsrun": "^2.0.1", "@git.zone/tsrun": "^1.2.44",
"@git.zone/tstest": "^3.1.8", "@git.zone/tstest": "^2.3.1",
"@types/node": "^25.3.0" "@types/node": "^22.15.30"
}, },
"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.5", "@push.rocks/smartdelay": "^3.0.1",
"@push.rocks/smartlog": "^3.1.11", "@push.rocks/smartlog": "^3.0.2",
"@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",

4637
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

210
readme.md
View File

@@ -1,10 +1,6 @@
# @push.rocks/smartmetrics # @push.rocks/smartmetrics
**Powerful system metrics collection for Node.js applications with Prometheus integration** 🚀 **Powerful system metrics collection for Node.js applications with Prometheus integration**
## 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? ## What is SmartMetrics?
@@ -12,19 +8,17 @@ SmartMetrics is a comprehensive metrics collection library that monitors your No
## Key Features ## Key Features
- 📊 **Real-time Metrics Collection** Monitor CPU and memory usage across all processes - 📊 **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` - 🔄 **Automatic Child Process Tracking** - Aggregates metrics from main and child processes
- 🐳 **Docker-Aware** Detects container memory limits from cgroup automatically - 🐳 **Docker-Aware** - Detects container memory limits automatically
- 📈 **Prometheus Integration** Built-in HTTP endpoint for Prometheus scraping with `prom-client` - 📈 **Prometheus Integration** - Built-in HTTP endpoint for Prometheus scraping
- 🔧 **Flexible Output Formats** Get metrics as JSON objects or Prometheus text exposition format - 🔧 **Flexible Output Formats** - Get metrics as JSON objects or Prometheus text
- 📝 **Automatic Heartbeat Logging** Optional periodic metrics logging via `@push.rocks/smartlog` - 📝 **Automatic Heartbeat Logging** - Optional periodic metrics logging
- 🚀 **Zero Configuration** Works out of the box with sensible defaults - 🚀 **Zero Configuration** - Works out of the box with sensible defaults
## Installation ## Installation
```bash ```bash
pnpm install @push.rocks/smartmetrics
# or
npm install @push.rocks/smartmetrics npm install @push.rocks/smartmetrics
``` ```
@@ -36,10 +30,9 @@ import { Smartlog } from '@push.rocks/smartlog';
// Create a logger instance // Create a logger instance
const logger = new Smartlog({ const logger = new Smartlog({
logContext: null, logContext: 'my-app',
minimumLogLevel: 'info', minimumLogLevel: 'info'
}); });
logger.enableConsole();
// Initialize SmartMetrics // Initialize SmartMetrics
const metrics = new SmartMetrics(logger, 'my-service'); const metrics = new SmartMetrics(logger, 'my-service');
@@ -53,8 +46,7 @@ console.log(`Memory: ${currentMetrics.memoryUsageText}`);
metrics.start(); metrics.start();
// Enable Prometheus endpoint // Enable Prometheus endpoint
metrics.enablePrometheusEndpoint(9090); metrics.enablePrometheusEndpoint(9090); // Metrics available at http://localhost:9090/metrics
// Metrics now available at http://localhost:9090/metrics
// Clean shutdown // Clean shutdown
metrics.stop(); metrics.stop();
@@ -63,57 +55,46 @@ metrics.stop();
## Core Concepts ## Core Concepts
### Process Aggregation ### Process Aggregation
SmartMetrics doesn't just monitor your main process - it automatically discovers and aggregates metrics from all child processes spawned by your application. This gives you a complete picture of your application's resource footprint.
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 ### Memory Limit Detection
The library automatically detects available memory whether running on bare metal, in Docker containers, or with Node.js heap restrictions. It uses the most restrictive limit to ensure accurate percentage calculations.
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 ### Dual Output Formats
- **JSON Format**: Ideal for application monitoring, custom dashboards, and programmatic access
- **JSON Format** (`getMetrics()`) Ideal for application monitoring, custom dashboards, and programmatic access - **Prometheus Format**: Perfect for integration with Prometheus/Grafana monitoring stacks
- **Prometheus Format** (`getPrometheusFormattedMetrics()`) Perfect for integration with Prometheus/Grafana monitoring stacks
## API Reference ## API Reference
### `new SmartMetrics(logger, sourceName)` ### Constructor
Creates a new SmartMetrics instance. ```typescript
new SmartMetrics(logger: Smartlog, sourceName: string)
```
- `logger`: Smartlog instance for output
- `sourceName`: Identifier for your service/application
| Parameter | Type | Description | ### Methods
|-----------|------|-------------|
| `logger` | `Smartlog` | A `@push.rocks/smartlog` logger instance |
| `sourceName` | `string` | Identifier for your service/application |
### `async getMetrics(): Promise<IMetricsSnapshot>` #### `async getMetrics(): Promise<IMetricsSnapshot>`
Retrieves current system metrics as a JSON object.
Retrieves current system metrics as a structured object.
**Returns `IMetricsSnapshot`:**
**Returns:**
```typescript ```typescript
{ {
process_cpu_seconds_total: number; // Total CPU time in seconds process_cpu_seconds_total: number; // Total CPU time in seconds
nodejs_active_handles_total: number; // Active handles count nodejs_active_handles_total: number; // Active handles count
nodejs_active_requests_total: number; // Active requests count nodejs_active_requests_total: number; // Active requests count
nodejs_heap_size_total_bytes: number; // V8 heap size in bytes nodejs_heap_size_total_bytes: number; // Heap size in bytes
cpuPercentage: number; // Aggregated CPU usage across all child processes cpuPercentage: number; // Current CPU usage (0-100)
cpuUsageText: string; // Human-readable CPU usage (e.g. "12.5 %") cpuUsageText: string; // Human-readable CPU usage
memoryPercentage: number; // Memory usage as percentage of detected limit memoryPercentage: number; // Memory usage percentage
memoryUsageBytes: number; // Total memory in bytes across all child processes memoryUsageBytes: number; // Memory usage in bytes
memoryUsageText: string; // Human-readable (e.g. "45% | 920 MB / 2 GB") memoryUsageText: string; // Human-readable memory usage
} }
``` ```
**Example:** **Example:**
```typescript ```typescript
const metrics = await smartMetrics.getMetrics(); const metrics = await smartMetrics.getMetrics();
if (metrics.cpuPercentage > 80) { if (metrics.cpuPercentage > 80) {
@@ -121,70 +102,51 @@ if (metrics.cpuPercentage > 80) {
} }
``` ```
### `start(): void` #### `start(): void`
Starts automatic metrics collection and heartbeat logging. Logs metrics every 20 seconds.
Starts automatic metrics collection and heartbeat logging every 20 seconds via the provided `Smartlog` instance.
**Example:**
```typescript ```typescript
smartMetrics.start(); smartMetrics.start();
// Logs: "sending heartbeat for my-service with metrics" every 20 seconds // Logs: "sending heartbeat for my-service with metrics" every 20 seconds
``` ```
### `stop(): void` #### `stop(): void`
Stops automatic metrics collection and closes any open endpoints.
Stops automatic metrics collection, closes heartbeat loop, and shuts down any open Prometheus endpoints. #### `async getPrometheusFormattedMetrics(): Promise<string>`
Returns metrics in Prometheus text exposition format.
### `async getPrometheusFormattedMetrics(): Promise<string>`
Returns all metrics in Prometheus text exposition format, including default Node.js collectors and custom SmartMetrics gauges.
**Example:**
```typescript ```typescript
const promMetrics = await smartMetrics.getPrometheusFormattedMetrics(); const promMetrics = await smartMetrics.getPrometheusFormattedMetrics();
// Returns: // Returns:
// # HELP smartmetrics_cpu_percentage Current CPU usage percentage // # HELP smartmetrics_cpu_percentage Current CPU usage percentage
// # TYPE smartmetrics_cpu_percentage gauge // # TYPE smartmetrics_cpu_percentage gauge
// smartmetrics_cpu_percentage 15.2 // smartmetrics_cpu_percentage 15.2
// # 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
``` ```
### `enablePrometheusEndpoint(port?: number): void` #### `enablePrometheusEndpoint(port?: number): void`
Starts an HTTP server that exposes metrics for Prometheus scraping.
Starts an HTTP server that exposes metrics at `/metrics` for Prometheus scraping. **Parameters:**
- `port`: Port number (default: 9090)
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `port` | `number` | `9090` | Port to listen on |
**Example:**
```typescript ```typescript
smartMetrics.enablePrometheusEndpoint(3000); smartMetrics.enablePrometheusEndpoint(3000);
// GET http://localhost:3000/metrics → Prometheus text format // Metrics now available at http://localhost:3000/metrics
// GET http://localhost:3000/anything-else → 404
``` ```
### `disablePrometheusEndpoint(): void` #### `disablePrometheusEndpoint(): void`
Stops the Prometheus HTTP endpoint.
Gracefully shuts down the Prometheus HTTP server.
### `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 ## Use Cases
### 🔍 Application Performance Monitoring ### 1. Application Performance Monitoring
```typescript ```typescript
// Monitor performance during critical operations
const metricsBefore = await smartMetrics.getMetrics(); const metricsBefore = await smartMetrics.getMetrics();
await performHeavyOperation(); await performHeavyOperation();
const metricsAfter = await smartMetrics.getMetrics(); const metricsAfter = await smartMetrics.getMetrics();
@@ -194,9 +156,9 @@ console.log(`Operation consumed ${
} CPU seconds`); } CPU seconds`);
``` ```
### 🛡️ Resource Limit Enforcement ### 2. Resource Limit Enforcement
```typescript ```typescript
// Prevent operations when resources are constrained
async function checkResources() { async function checkResources() {
const metrics = await smartMetrics.getMetrics(); const metrics = await smartMetrics.getMetrics();
@@ -210,30 +172,38 @@ async function checkResources() {
} }
``` ```
### 📈 Prometheus + Grafana Stack ### 3. Prometheus + Grafana Monitoring
```typescript ```typescript
smartMetrics.enablePrometheusEndpoint(9090); // Expose metrics for Prometheus
smartMetrics.enablePrometheusEndpoint();
// prometheus.yml: // In your Prometheus config:
// scrape_configs: // scrape_configs:
// - job_name: 'my-app' // - job_name: 'my-app'
// scrape_interval: 15s
// static_configs: // static_configs:
// - targets: ['localhost:9090'] // - targets: ['localhost:9090']
``` ```
### 🐳 Container Resource Monitoring ### 4. Development and Debugging
```typescript ```typescript
// Automatically detects Docker/cgroup memory limits // Track memory leaks during development
const metrics = await smartMetrics.getMetrics(); setInterval(async () => {
console.log(metrics.memoryUsageText); const metrics = await smartMetrics.getMetrics();
// Output: "45% | 920 MB / 2 GB" (container limit detected) console.log(`Heap: ${metrics.nodejs_heap_size_total_bytes / 1024 / 1024}MB`);
}, 5000);
``` ```
### 🔄 Health Check Endpoint ### 5. Container Resource Monitoring
```typescript
// Automatically detects container limits
const metrics = await smartMetrics.getMetrics();
console.log(metrics.memoryUsageText);
// Output: "45% | 920 MB / 2 GB" (detects container limit)
```
## Integration Examples
### With Express
```typescript ```typescript
import express from 'express'; import express from 'express';
@@ -244,15 +214,17 @@ app.get('/health', async (req, res) => {
res.json({ res.json({
status: metrics.memoryPercentage < 90 ? 'healthy' : 'degraded', status: metrics.memoryPercentage < 90 ? 'healthy' : 'degraded',
cpu: metrics.cpuUsageText, metrics: {
memory: metrics.memoryUsageText, cpu: metrics.cpuUsageText,
memory: metrics.memoryUsageText
}
}); });
}); });
``` ```
### 🔁 Graceful Restart on High Memory (PM2) ### With PM2
```typescript ```typescript
// Graceful shutdown on high memory
setInterval(async () => { setInterval(async () => {
const metrics = await smartMetrics.getMetrics(); const metrics = await smartMetrics.getMetrics();
@@ -263,23 +235,37 @@ setInterval(async () => {
}, 10000); }, 10000);
``` ```
### With Custom Dashboards
```typescript
// Stream metrics to your monitoring service
setInterval(async () => {
const metrics = await smartMetrics.getMetrics();
await sendToMonitoringService({
timestamp: Date.now(),
service: 'my-service',
cpu: metrics.cpuPercentage,
memory: metrics.memoryUsageBytes,
memoryLimit: metrics.memoryUsageBytes / (metrics.memoryPercentage / 100)
});
}, 60000);
```
## License and Legal Information ## License and Legal Information
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [LICENSE](./LICENSE) file. 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.
**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 or third parties, and are not included within the scope of the MIT license granted herein. 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.
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 further information, please contact us via email at hello@task.vc. For any legal inquiries or if you require 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.11', version: '2.0.9',
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.'
} }