38 Commits

Author SHA1 Message Date
5a22b87592 v3.0.0 2026-02-19 10:10:58 +00:00
b99d2cc04b BREAKING CHANGE(smartmetrics): add system-wide metrics collection, Prometheus gauges, and normalized CPU reporting 2026-02-19 10:10:58 +00:00
32d75804c0 update 2026-02-19 09:51:34 +00:00
7c3197455f update 2026-02-19 09:46:46 +00:00
767a40e01e v2.0.11 2026-02-19 09:16:43 +00:00
fc017d31b0 fix(deps): bump dependencies, update build script, expand README and npm metadata 2026-02-19 09:16:43 +00:00
f67b093127 2.0.10 2025-06-16 23:43:57 +00:00
6bcdf73a97 fix(readme): update documentation with detailed usage instructions, API references and integration examples 2025-06-09 15:07:09 +00:00
c47732c5fc fix(readme): Update documentation with detailed usage instructions, API references and integration examples. 2025-06-09 12:34:06 +00:00
f587884e86 2.0.8 2025-06-09 12:07:24 +00:00
30992ea44b fix(smartmetrics): Refactor metrics calculation and update Prometheus integration documentation 2025-06-09 12:07:24 +00:00
34b09ed7a7 feat: Implement Prometheus metrics exposure in SmartMetrics
- Added Prometheus gauges for CPU and memory metrics.
- Implemented HTTP server to expose metrics at /metrics endpoint.
- Created methods to enable and disable the Prometheus endpoint.
- Updated getMetrics() to set gauge values.
- Added tests for Prometheus metrics functionality.
- Updated documentation plan for Prometheus integration.
2025-06-09 10:31:25 +00:00
9413e0323a update description 2024-05-29 14:14:43 +02:00
e514209fb8 update tsconfig 2024-04-14 17:58:26 +02:00
fc4bc118d6 update npmextra.json: githost 2024-04-01 21:36:34 +02:00
19ce45deaa update npmextra.json: githost 2024-04-01 19:58:57 +02:00
1e582023f7 update npmextra.json: githost 2024-03-30 21:47:57 +01:00
d6877f9adf 2.0.7 2023-08-08 17:54:28 +02:00
861e9ab0be fix(core): update 2023-08-08 17:54:27 +02:00
1460fedbcb switch to new org scheme 2023-07-10 10:16:33 +02:00
537df1d53e 2.0.6 2023-07-03 11:08:27 +02:00
352d0f8a1e fix(core): update 2023-07-03 11:08:27 +02:00
8454244f3b 2.0.5 2023-07-02 23:21:02 +02:00
4b6d6feea1 fix(core): update 2023-07-02 23:21:01 +02:00
01306ed3f9 2.0.4 2023-07-02 23:13:48 +02:00
2a2ca66708 fix(core): update 2023-07-02 23:13:47 +02:00
299e50cbf7 2.0.3 2023-07-02 22:23:47 +02:00
1e6af24df0 fix(core): update 2023-07-02 22:23:46 +02:00
66f0dc6815 2.0.2 2023-07-02 22:17:27 +02:00
7d703fe57e fix(core): update 2023-07-02 22:17:27 +02:00
2ddccdaab1 2.0.1 2022-07-27 12:30:50 +02:00
173b3e2b19 fix(core): update 2022-07-27 12:30:49 +02:00
df7a499c1d 2.0.0 2022-07-27 12:00:39 +02:00
173df91528 BREAKING CHANGE(core): switch to esm 2022-07-27 12:00:38 +02:00
057a0a329e 1.0.17 2021-12-06 20:14:30 +01:00
e53bebf7a1 fix(core): update 2021-12-06 20:14:30 +01:00
c4f5e9338a 1.0.16 2021-08-19 19:22:30 +02:00
dad2bc26fc fix(core): update 2021-08-19 19:22:29 +02:00
25 changed files with 12396 additions and 26153 deletions

View File

@@ -0,0 +1,66 @@
name: Default (not tags)
on:
push:
tags-ignore:
- '**'
env:
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
jobs:
security:
runs-on: ubuntu-latest
continue-on-error: true
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Install pnpm and npmci
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
- name: Run npm prepare
run: npmci npm prepare
- name: Audit production dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --prod
continue-on-error: true
- name: Audit development dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --dev
continue-on-error: true
test:
if: ${{ always() }}
needs: security
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Test stable
run: |
npmci node install stable
npmci npm install
npmci npm test
- name: Test build
run: |
npmci node install stable
npmci npm install
npmci npm build

View File

@@ -0,0 +1,124 @@
name: Default (tags)
on:
push:
tags:
- '*'
env:
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
jobs:
security:
runs-on: ubuntu-latest
continue-on-error: true
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Audit production dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --prod
continue-on-error: true
- name: Audit development dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --dev
continue-on-error: true
test:
if: ${{ always() }}
needs: security
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Test stable
run: |
npmci node install stable
npmci npm install
npmci npm test
- name: Test build
run: |
npmci node install stable
npmci npm install
npmci npm build
release:
needs: test
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Release
run: |
npmci node install stable
npmci npm publish
metadata:
needs: test
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
continue-on-error: true
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Code quality
run: |
npmci command npm install -g typescript
npmci npm install
- name: Trigger
run: npmci trigger
- name: Build docs and upload artifacts
run: |
npmci node install stable
npmci npm install
pnpm install -g @git.zone/tsdoc
npmci command tsdoc
continue-on-error: true

View File

@@ -1,137 +0,0 @@
# gitzone ci_default
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
cache:
paths:
- .npmci_cache/
key: '$CI_BUILD_STAGE'
stages:
- security
- test
- release
- metadata
# ====================
# security stage
# ====================
mirror:
stage: security
script:
- npmci git mirror
only:
- tags
tags:
- lossless
- docker
- notpriv
auditProductionDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security
script:
- npmci npm prepare
- npmci command npm install --production --ignore-scripts
- npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=prod --production
tags:
- docker
auditDevDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security
script:
- npmci npm prepare
- npmci command npm install --ignore-scripts
- npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=dev
tags:
- docker
allow_failure: true
# ====================
# test stage
# ====================
testStable:
stage: test
script:
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
testBuild:
stage: test
script:
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
release:
stage: release
script:
- npmci node install stable
- npmci npm publish
only:
- tags
tags:
- lossless
- docker
- notpriv
# ====================
# metadata stage
# ====================
codequality:
stage: metadata
allow_failure: true
only:
- tags
script:
- npmci command npm install -g tslint typescript
- npmci npm prepare
- npmci npm install
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
tags:
- lossless
- docker
- priv
trigger:
stage: metadata
script:
- npmci trigger
only:
- tags
tags:
- lossless
- docker
- notpriv
pages:
stage: metadata
script:
- npmci node install lts
- npmci command npm install -g @gitzone/tsdoc
- npmci npm prepare
- npmci npm install
- npmci command tsdoc
tags:
- lossless
- docker
- notpriv
only:
- tags
artifacts:
expire_in: 1 week
paths:
- public
allow_failure: true

24
.vscode/launch.json vendored
View File

@@ -2,28 +2,10 @@
"version": "0.2.0",
"configurations": [
{
"name": "current file",
"type": "node",
"command": "npm test",
"name": "Run npm test",
"request": "launch",
"args": [
"${relativeFile}"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": "test.ts",
"type": "node",
"request": "launch",
"args": [
"test/test.ts"
],
"runtimeArgs": ["-r", "@gitzone/tsrun"],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart"
"type": "node-terminal"
}
]
}

58
changelog.md Normal file
View File

@@ -0,0 +1,58 @@
# Changelog
## 2026-02-19 - 3.0.0 - BREAKING CHANGE(smartmetrics)
add system-wide metrics collection, Prometheus gauges, and normalized CPU reporting
- Add new sysusage plugin (ts/smartmetrics.sysusage.ts) that reads /proc/stat and /proc/meminfo (with os fallback) and returns system CPU, memory and load averages.
- Expose system-wide Prometheus gauges: smartmetrics_system_cpu_percent, smartmetrics_system_memory_used_percent, smartmetrics_system_memory_used_bytes, smartmetrics_system_load_avg_1, smartmetrics_system_load_avg_5, smartmetrics_system_load_avg_15.
- Extend IMetricsSnapshot with system fields: systemCpuPercent, systemMemTotalBytes, systemMemAvailableBytes, systemMemUsedBytes, systemMemUsedPercent, systemLoadAvg1, systemLoadAvg5, systemLoadAvg15 (this is a breaking TypeScript API change).
- Normalize per-process CPU in pidusage by adding cpuCoreCount and cpuNormalizedPercent and use cpuNormalizedPercent when aggregating CPU across the process tree.
- Export the new sysusage plugin from ts/smartmetrics.plugins.ts and wire system metrics into metric collection and Prometheus gauge updates.
## 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)
Refactor metrics calculation and update Prometheus integration documentation
- Removed dependency on registry.getMetricsAsJSON by directly calculating Node.js metrics
- Updated getMetrics to compute CPU time, heap size, and set default values for deprecated metrics
- Enhanced documentation with comprehensive Prometheus integration examples
- Improved logging on shutdown of the Prometheus endpoint
## 2025-06-09 - 2.0.7 - Prometheus Metrics Integration
feat: Implement Prometheus metrics exposure in SmartMetrics
- Added Prometheus gauges for CPU and memory metrics.
- Implemented HTTP server to expose metrics at the /metrics endpoint.
- Created methods to enable and disable the Prometheus endpoint.
- Updated getMetrics() to set gauge values.
- Added tests for Prometheus metrics functionality.
- Updated documentation plan for Prometheus integration.
## 2023-07-02 to 2023-08-08 - 2.0.0 to 2.0.6 - Maintenance Updates
Over this period several releases were published with iterative fixes and minor organizational changes.
- Applied multiple core fixes and routine maintenance updates.
- Switched to new organization scheme (recorded on 2023-07-10).
- Performed several version bumps and configuration updates.
## 2021-08-12 to 2022-07-27 - 1.0.1 to 1.0.17 - Maintenance and Breaking Changes
During this interval a series of minor fixes were combined with a significant breaking change.
- BREAKING CHANGE: Switched to ESM in 1.0.17 (2022-07-27).
- Numerous maintenance updates and core fixes were applied.

View File

@@ -1,18 +1,43 @@
{
"gitzone": {
"@git.zone/cli": {
"projectType": "npm",
"module": {
"githost": "gitlab.com",
"gitscope": "pushrocks",
"githost": "code.foss.global",
"gitscope": "push.rocks",
"gitrepo": "smartmetrics",
"shortDescription": "easy system metrics",
"npmPackagename": "@pushrocks/smartmetrics",
"description": "A package for easy collection and reporting of system and process metrics.",
"npmPackagename": "@push.rocks/smartmetrics",
"license": "MIT",
"projectDomain": "push.rocks"
"projectDomain": "push.rocks",
"keywords": [
"system metrics",
"performance monitoring",
"resource usage",
"memory management",
"CPU usage",
"prometheus",
"typescript",
"nodejs",
"process management",
"development tools",
"performance analysis",
"docker environments",
"log reporting",
"operational insights"
]
},
"release": {
"registries": [
"https://verdaccio.lossless.digital",
"https://registry.npmjs.org"
],
"accessLevel": "public"
}
},
"npmci": {
"npmGlobalTools": [],
"npmAccessLevel": "public"
"@git.zone/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"
},
"@ship.zone/szci": {
"npmGlobalTools": []
}
}

25847
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,24 +1,23 @@
{
"name": "@pushrocks/smartmetrics",
"version": "1.0.15",
"name": "@push.rocks/smartmetrics",
"version": "3.0.0",
"private": false,
"description": "easy system metrics",
"description": "A package for easy collection and reporting of system and process metrics.",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"author": "Lossless GmbH",
"license": "MIT",
"scripts": {
"test": "(tstest test/ --web)",
"build": "(tsbuild --web)"
"test": "(tstest test/ --verbose)",
"build": "(tsbuild tsfolders)",
"buildDocs": "tsdoc"
},
"devDependencies": {
"@gitzone/tsbuild": "^2.1.26",
"@gitzone/tsbundle": "^1.0.78",
"@gitzone/tstest": "^1.0.44",
"@pushrocks/tapbundle": "^3.2.9",
"@types/node": "^16.6.1",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.15.0"
"@git.zone/tsbuild": "^4.1.2",
"@git.zone/tsbundle": "^2.8.3",
"@git.zone/tsrun": "^2.0.1",
"@git.zone/tstest": "^3.1.8",
"@types/node": "^25.3.0"
},
"browserslist": [
"last 1 chrome versions"
@@ -36,11 +35,30 @@
"readme.md"
],
"dependencies": {
"@pushrocks/smartdelay": "^2.0.13",
"@pushrocks/smartlog": "^2.0.44",
"@types/pidusage": "^2.0.1",
"pidtree": "^0.5.0",
"pidusage": "^2.0.21",
"prom-client": "^13.2.0"
}
"@push.rocks/smartdelay": "^3.0.5",
"@push.rocks/smartlog": "^3.1.11"
},
"type": "module",
"keywords": [
"system metrics",
"performance monitoring",
"resource usage",
"memory management",
"CPU usage",
"prometheus",
"typescript",
"nodejs",
"process management",
"development tools",
"performance analysis",
"docker environments",
"log reporting",
"operational insights"
],
"homepage": "https://code.foss.global/push.rocks/smartmetrics",
"repository": {
"type": "git",
"url": "https://code.foss.global/push.rocks/smartmetrics.git"
},
"packageManager": "pnpm@10.11.0+sha512.6540583f41cc5f628eb3d9773ecee802f4f9ef9923cc45b69890fb47991d4b092964694ec3a4f738a420c918a333062c8b925d312f42e4f0c263eb603551f977"
}

10396
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

30
readme.hints.md Normal file
View File

@@ -0,0 +1,30 @@
# SmartMetrics Implementation Hints
## Prometheus Integration
The package now supports exposing metrics in Prometheus format through two mechanisms:
1. **Direct Export**: `getPrometheusFormattedMetrics()` returns metrics in Prometheus text exposition format
2. **HTTP Endpoint**: `enablePrometheusEndpoint(port)` starts an HTTP server that exposes metrics on `/metrics`
### Architecture Decisions
- We use the prom-client library's default collectors for standard Node.js metrics
- Custom gauges are created for our calculated metrics (CPU percentage, memory percentage, memory bytes)
- The `getMetrics()` method calculates values directly rather than parsing the Prometheus registry JSON
- For metrics like `nodejs_active_handles_total` and `nodejs_active_requests_total`, we return 0 in `getMetrics()` since these require deprecated Node.js APIs, but the Prometheus collectors still track the real values
### Consolidation Approach
Initially, we were mixing two approaches:
1. Extracting values from the Prometheus registry JSON for default metrics
2. Creating custom gauges for calculated metrics
We consolidated by:
- Calculating all values directly in `getMetrics()` where possible
- Only using the Prometheus registry for the text exposition format
- This eliminates the dependency on parsing registry JSON and makes the code cleaner
### Memory Calculation
The package tracks memory usage across the main process and all child processes using `pidtree` and `pidusage`. This provides a more comprehensive view than just the main process memory.

304
readme.md
View File

@@ -1,39 +1,285 @@
# @pushrocks/smartmetrics
easy system metrics
# @push.rocks/smartmetrics
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartmetrics)
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartmetrics)
* [github.com (source mirror)](https://github.com/pushrocks/smartmetrics)
* [docs (typedoc)](https://pushrocks.gitlab.io/smartmetrics/)
**Powerful system metrics collection for Node.js applications with Prometheus integration** 🚀
## Status for master
## Issue Reporting and Security
Status Category | Status Badge
-- | --
GitLab Pipelines | [![pipeline status](https://gitlab.com/pushrocks/smartmetrics/badges/master/pipeline.svg)](https://lossless.cloud)
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/pushrocks/smartmetrics/badges/master/coverage.svg)](https://lossless.cloud)
npm | [![npm downloads per month](https://badgen.net/npm/dy/@pushrocks/smartmetrics)](https://lossless.cloud)
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/pushrocks/smartmetrics)](https://lossless.cloud)
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@pushrocks/smartmetrics)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@pushrocks/smartmetrics)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@pushrocks/smartmetrics)](https://lossless.cloud)
Platform support | [![Supports Windows 10](https://badgen.net/badge/supports%20Windows%2010/yes/green?icon=windows)](https://lossless.cloud) [![Supports Mac OS X](https://badgen.net/badge/supports%20Mac%20OS%20X/yes/green?icon=apple)](https://lossless.cloud)
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.
## Usage
## What is SmartMetrics?
Use TypeScript for best in class intellisense
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.
## Contribution
## Key Features
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
- 📊 **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
For further information read the linked docs at the top of this readme.
## Installation
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
```bash
pnpm install @push.rocks/smartmetrics
# or
npm install @push.rocks/smartmetrics
```
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)
## Quick Start
```typescript
import { SmartMetrics } from '@push.rocks/smartmetrics';
import { Smartlog } from '@push.rocks/smartlog';
// Create a logger instance
const logger = new Smartlog({
logContext: null,
minimumLogLevel: 'info',
});
logger.enableConsole();
// Initialize SmartMetrics
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();
```
## Core Concepts
### Process Aggregation
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** supports both cgroup v2 (`/sys/fs/cgroup/memory.max`) and cgroup v1 (`/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; // Always 0 (deprecated Node.js API; real values tracked by Prometheus default collectors)
nodejs_active_requests_total: number; // Always 0 (deprecated Node.js API; real values tracked by Prometheus default collectors)
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
smartMetrics.start();
// Logs: "sending heartbeat for my-service with metrics" every 20 seconds
```
### `stop(): void`
Stops automatic metrics collection, closes heartbeat loop, and shuts down any open Prometheus endpoints.
### `async getPrometheusFormattedMetrics(): Promise<string>`
Returns all metrics in Prometheus text exposition format, including default Node.js collectors and custom SmartMetrics gauges.
```typescript
const promMetrics = await smartMetrics.getPrometheusFormattedMetrics();
// Returns:
// # HELP smartmetrics_cpu_percentage Current CPU usage percentage
// # TYPE smartmetrics_cpu_percentage gauge
// 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`
Starts an HTTP server that exposes metrics at `/metrics` for Prometheus scraping.
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `port` | `number` | `9090` | Port to listen on |
```typescript
smartMetrics.enablePrometheusEndpoint(3000);
// GET http://localhost:3000/metrics → Prometheus text format
// GET http://localhost:3000/anything-else → 404
```
### `disablePrometheusEndpoint(): void`
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
### 🔍 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
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.
### 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.
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
Task Venture Capital GmbH
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.
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.

81
readme.plan.md Normal file
View File

@@ -0,0 +1,81 @@
# Prometheus Metrics Implementation Plan
`cat /home/philkunz/.claude/CLAUDE.md`
## Overview
Add Prometheus metrics exposure functionality to SmartMetrics while maintaining backward compatibility with existing `getMetrics()` method.
## Implementation Tasks
### 1. Add HTTP Server Dependencies
- [x] Check if we need to add any HTTP server dependency to package.json
- [x] Import necessary modules in smartmetrics.plugins.ts
### 2. Create Prometheus Gauges in SmartMetrics Class
- [x] Add private properties for custom gauges:
- [x] `private cpuPercentageGauge: plugins.promClient.Gauge<string>`
- [x] `private memoryPercentageGauge: plugins.promClient.Gauge<string>`
- [x] `private memoryUsageBytesGauge: plugins.promClient.Gauge<string>`
- [x] Initialize gauges in `setup()` method with appropriate names and help text:
- [x] `smartmetrics_cpu_percentage` - "Current CPU usage percentage"
- [x] `smartmetrics_memory_percentage` - "Current memory usage percentage"
- [x] `smartmetrics_memory_usage_bytes` - "Current memory usage in bytes"
### 3. Update getMetrics() Method
- [x] After calculating metrics, update the Prometheus gauges:
- [x] `this.cpuPercentageGauge.set(cpuPercentage)`
- [x] `this.memoryPercentageGauge.set(memoryPercentage)`
- [x] `this.memoryUsageBytesGauge.set(memoryUsageBytes)`
- [x] Ensure gauges are only updated if they exist (defensive programming)
### 4. Add getPrometheusFormattedMetrics() Method
- [x] Create new public async method `getPrometheusFormattedMetrics(): Promise<string>`
- [x] Call `this.getMetrics()` to ensure gauges are updated with latest values
- [x] Return `await this.registry.metrics()` to get Prometheus text format
### 5. Add HTTP Server Properties
- [x] Add private property for HTTP server: `private prometheusServer?: any`
- [x] Add private property for server port: `private prometheusPort?: number`
### 6. Implement enablePrometheusEndpoint() Method
- [x] Create new public method `enablePrometheusEndpoint(port: number = 9090): void`
- [x] Check if server is already running, if so, log warning and return
- [x] Create minimal HTTP server using Node.js built-in `http` module:
- [x] Listen on specified port
- [x] Handle GET requests to `/metrics` endpoint
- [x] Return Prometheus-formatted metrics with correct Content-Type header
- [x] Handle other routes with 404
- [x] Store server reference and port for later cleanup
- [x] Log info message about endpoint availability
### 7. Add disablePrometheusEndpoint() Method
- [x] Create new public method `disablePrometheusEndpoint(): void`
- [x] Check if server exists, if not, return
- [x] Close the HTTP server
- [x] Clear server reference and port
- [x] Log info message about endpoint shutdown
### 8. Update stop() Method
- [x] Call `disablePrometheusEndpoint()` to ensure clean shutdown
### 9. Add Tests
- [x] Add test for `getPrometheusFormattedMetrics()`:
- [x] Verify it returns a string
- [x] Verify it contains expected metric names
- [x] Verify format matches Prometheus text exposition format
- [x] Add test for `enablePrometheusEndpoint()`:
- [x] Start endpoint on test port (e.g., 19090)
- [x] Make HTTP request to `/metrics`
- [x] Verify response has correct Content-Type
- [x] Verify response contains metrics
- [x] Clean up by calling `disablePrometheusEndpoint()`
### 10. Update Documentation
- [x] Add usage example in readme.md for Prometheus integration
- [x] Document the new methods in code comments
## Notes
- Using Node.js built-in `http` module to avoid adding unnecessary dependencies
- Default port 9090 is commonly used for metrics endpoints
- Maintaining backward compatibility - existing functionality unchanged
- Prometheus text format example: `metric_name{label="value"} 123.45`

View File

@@ -1,5 +1,3 @@
import * as smartlog from '@pushrocks/smartlog';
import * as smartlog from '@push.rocks/smartlog';
export {
smartlog
}
export { smartlog };

View File

@@ -1,18 +1,18 @@
import * as plugins from './plugins';
import * as plugins from './plugins.js';
import { expect, tap } from '@pushrocks/tapbundle';
import * as smartmetrics from '../ts/index';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as smartmetrics from '../ts/index.js';
let testSmartMetrics: smartmetrics.SmartMetrics;
tap.test('should create a smartmetrics instance', async () => {
const logger = new plugins.smartlog.Smartlog({
logContext: null,
minimumLogLevel: 'silly'
minimumLogLevel: 'silly',
});
logger.enableConsole();
testSmartMetrics = new smartmetrics.SmartMetrics(logger, 'testContainer');
expect(testSmartMetrics).to.be.instanceOf(smartmetrics.SmartMetrics);
expect(testSmartMetrics).toBeInstanceOf(smartmetrics.SmartMetrics);
});
tap.test('should start smartmetrics', async () => {
@@ -20,8 +20,59 @@ tap.test('should start smartmetrics', async () => {
});
tap.test('should produce valid metrics', async (tools) => {
console.log('calling .getMetrics from Testfile:')
console.log('calling .getMetrics from Testfile:');
console.log(await testSmartMetrics.getMetrics());
})
});
tap.start();
tap.test('should return Prometheus formatted metrics', async () => {
const prometheusMetrics = await testSmartMetrics.getPrometheusFormattedMetrics();
expect(prometheusMetrics).toBeTypeofString();
expect(prometheusMetrics).toContain('smartmetrics_cpu_percentage');
expect(prometheusMetrics).toContain('smartmetrics_memory_percentage');
expect(prometheusMetrics).toContain('smartmetrics_memory_usage_bytes');
expect(prometheusMetrics).toContain('# HELP');
expect(prometheusMetrics).toContain('# TYPE');
});
tap.test('should enable Prometheus endpoint', async (tools) => {
const testPort = 19090;
testSmartMetrics.enablePrometheusEndpoint(testPort);
// Give the server time to start
await tools.delayFor(1000);
// Test the endpoint
const response = await fetch(`http://localhost:${testPort}/metrics`);
expect(response.status).toEqual(200);
expect(response.headers.get('content-type')).toEqual('text/plain; version=0.0.4');
const metricsText = await response.text();
expect(metricsText).toContain('smartmetrics_cpu_percentage');
expect(metricsText).toContain('smartmetrics_memory_percentage');
expect(metricsText).toContain('smartmetrics_memory_usage_bytes');
});
tap.test('should handle 404 for non-metrics endpoints', async () => {
const response = await fetch('http://localhost:19090/notfound');
expect(response.status).toEqual(404);
const text = await response.text();
expect(text).toEqual('Not Found');
});
tap.test('should disable Prometheus endpoint', async () => {
testSmartMetrics.disablePrometheusEndpoint();
// Give the server time to shut down
await new Promise(resolve => setTimeout(resolve, 1000));
// Verify the endpoint is no longer accessible
try {
await fetch('http://localhost:19090/metrics');
throw new Error('Should have failed to connect');
} catch (error) {
// Expected to fail
expect(error.message).toContain('fetch failed');
}
});
export default tap.start();

8
ts/00_commitinfo_data.ts Normal file
View File

@@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @push.rocks/commitinfo
*/
export const commitinfo = {
name: '@push.rocks/smartmetrics',
version: '3.0.0',
description: 'A package for easy collection and reporting of system and process metrics.'
}

View File

@@ -1,2 +1,2 @@
export * from './smartmetrics.interfaces';
export * from './smartmetrics.classes.smartmetrics';
export * from './smartmetrics.interfaces.js';
export * from './smartmetrics.classes.smartmetrics.js';

View File

@@ -1,23 +1,122 @@
import * as plugins from './smartmetrics.plugins';
import * as interfaces from './smartmetrics.interfaces';
import { Cipher } from 'crypto';
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 registry: plugins.prom.Registry;
public maxMemoryMB: number;
public async setup() {
const collectDefaultMetrics = plugins.promClient.collectDefaultMetrics;
this.registry = new plugins.promClient.Registry();
collectDefaultMetrics({ register: this.registry });
// Prometheus gauges for custom metrics
private cpuPercentageGauge: plugins.prom.Gauge;
private memoryPercentageGauge: plugins.prom.Gauge;
private memoryUsageBytesGauge: plugins.prom.Gauge;
private systemCpuPercentGauge: plugins.prom.Gauge;
private systemMemUsedPercentGauge: plugins.prom.Gauge;
private systemMemUsedBytesGauge: plugins.prom.Gauge;
private systemLoadAvg1Gauge: plugins.prom.Gauge;
private systemLoadAvg5Gauge: plugins.prom.Gauge;
private systemLoadAvg15Gauge: plugins.prom.Gauge;
// HTTP server for Prometheus endpoint
private prometheusServer?: plugins.http.Server;
private prometheusPort?: number;
public setup() {
this.registry = new plugins.prom.Registry();
plugins.prom.collectDefaultMetrics(this.registry);
// Initialize custom gauges
this.cpuPercentageGauge = new plugins.prom.Gauge({
name: 'smartmetrics_cpu_percentage',
help: 'Current CPU usage percentage',
registers: [this.registry]
});
this.memoryPercentageGauge = new plugins.prom.Gauge({
name: 'smartmetrics_memory_percentage',
help: 'Current memory usage percentage',
registers: [this.registry]
});
this.memoryUsageBytesGauge = new plugins.prom.Gauge({
name: 'smartmetrics_memory_usage_bytes',
help: 'Current memory usage in bytes',
registers: [this.registry]
});
this.systemCpuPercentGauge = new plugins.prom.Gauge({
name: 'smartmetrics_system_cpu_percent',
help: 'System-wide CPU usage percentage',
registers: [this.registry]
});
this.systemMemUsedPercentGauge = new plugins.prom.Gauge({
name: 'smartmetrics_system_memory_used_percent',
help: 'System-wide memory usage percentage',
registers: [this.registry]
});
this.systemMemUsedBytesGauge = new plugins.prom.Gauge({
name: 'smartmetrics_system_memory_used_bytes',
help: 'System-wide memory used in bytes',
registers: [this.registry]
});
this.systemLoadAvg1Gauge = new plugins.prom.Gauge({
name: 'smartmetrics_system_load_avg_1',
help: 'System 1-minute load average',
registers: [this.registry]
});
this.systemLoadAvg5Gauge = new plugins.prom.Gauge({
name: 'smartmetrics_system_load_avg_5',
help: 'System 5-minute load average',
registers: [this.registry]
});
this.systemLoadAvg15Gauge = new plugins.prom.Gauge({
name: 'smartmetrics_system_load_avg_15',
help: 'System 15-minute load average',
registers: [this.registry]
});
}
constructor(loggerArg: plugins.smartlog.Smartlog, sourceNameArg: string) {
this.logger = loggerArg;
this.sourceNameArg = sourceNameArg;
this.setup();
this.checkMemoryLimits();
}
private checkMemoryLimits() {
const heapStats = plugins.v8.getHeapStatistics();
const maxHeapSizeMB = heapStats.heap_size_limit / 1024 / 1024;
const totalSystemMemoryMB = plugins.os.totalmem() / 1024 / 1024;
let dockerMemoryLimitMB = totalSystemMemoryMB;
// Try cgroup v2 first, then fall back to cgroup v1
try {
const cgroupV2 = plugins.fs.readFileSync('/sys/fs/cgroup/memory.max', 'utf8').trim();
if (cgroupV2 !== 'max') {
dockerMemoryLimitMB = parseInt(cgroupV2, 10) / 1024 / 1024;
}
} catch {
try {
const cgroupV1 = plugins.fs.readFileSync(
'/sys/fs/cgroup/memory/memory.limit_in_bytes',
'utf8'
).trim();
dockerMemoryLimitMB = parseInt(cgroupV1, 10) / 1024 / 1024;
} catch {
// Not running in a container — use system memory
}
}
// Pick the most restrictive limit
this.maxMemoryMB = Math.min(totalSystemMemoryMB, dockerMemoryLimitMB, maxHeapSizeMB);
}
public start() {
@@ -31,7 +130,7 @@ export class SmartMetrics {
eventType: 'heartbeat',
metrics: await this.getMetrics(),
});
await plugins.smartdelay.delayFor(10000, null, true);
await plugins.smartdelay.delayFor(20000, null, true);
}
};
unattendedStart();
@@ -50,56 +149,151 @@ 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]);
let pids: number[] = [];
try {
pids = await plugins.pidtree.getChildPids(process.pid);
} catch {
// pidtree can fail if process tree cannot be read
}
const stats = await plugins.pidusage.getPidUsage([process.pid, ...pids]);
// lets compute cpu usage
// Aggregate normalized CPU (0-100% of total machine) across process tree
let cpuPercentage = 0;
for (const stat of Object.keys(stats)) {
if (!stats[stat]) continue;
cpuPercentage += stats[stat].cpu;
for (const stat of Object.values(stats)) {
if (!stat) continue;
cpuPercentage += stat.cpuNormalizedPercent;
}
let cpuUsageText = `${Math.round(cpuPercentage * 100) / 100} %`;
// lets compute memory usage
let memoryUsageBytes = 0;
for (const stat of Object.keys(stats)) {
if (!stats[stat]) continue;
memoryUsageBytes += stats[stat].memory;
for (const stat of Object.values(stats)) {
if (!stat) continue;
memoryUsageBytes += stat.memory;
}
let memoryPercentage = Math.round((memoryUsageBytes / 1000000000) * 100 * 100) / 100;
let memoryUsageText = `${memoryPercentage}% | ${this.formatBytes(memoryUsageBytes)} / ${this.formatBytes(1000000000)}`;
console.log(`${cpuUsageText} ||| ${memoryUsageText} `);
let memoryPercentage =
Math.round((memoryUsageBytes / (this.maxMemoryMB * 1024 * 1024)) * 100 * 100) / 100;
let memoryUsageText = `${memoryPercentage}% | ${this.formatBytes(
memoryUsageBytes
)} / ${this.formatBytes(this.maxMemoryMB * 1024 * 1024)}`;
// Get system-wide metrics
const systemUsage = await plugins.sysusage.getSystemUsage();
// 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);
}
if (this.systemCpuPercentGauge) {
this.systemCpuPercentGauge.set(systemUsage.cpuPercent);
}
if (this.systemMemUsedPercentGauge) {
this.systemMemUsedPercentGauge.set(systemUsage.memUsedPercent);
}
if (this.systemMemUsedBytesGauge) {
this.systemMemUsedBytesGauge.set(systemUsage.memUsedBytes);
}
if (this.systemLoadAvg1Gauge) {
this.systemLoadAvg1Gauge.set(systemUsage.loadAvg1);
}
if (this.systemLoadAvg5Gauge) {
this.systemLoadAvg5Gauge.set(systemUsage.loadAvg5);
}
if (this.systemLoadAvg15Gauge) {
this.systemLoadAvg15Gauge.set(systemUsage.loadAvg15);
}
// Calculate Node.js metrics directly
const cpuUsage = process.cpuUsage();
const process_cpu_seconds_total = (cpuUsage.user + cpuUsage.system) / 1000000;
const heapStats = plugins.v8.getHeapStatistics();
const nodejs_heap_size_total_bytes = heapStats.total_heap_size;
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,
memoryUsageBytes,
memoryUsageText,
systemCpuPercent: systemUsage.cpuPercent,
systemMemTotalBytes: systemUsage.memTotalBytes,
systemMemAvailableBytes: systemUsage.memAvailableBytes,
systemMemUsedBytes: systemUsage.memUsedBytes,
systemMemUsedPercent: systemUsage.memUsedPercent,
systemLoadAvg1: systemUsage.loadAvg1,
systemLoadAvg5: systemUsage.loadAvg5,
systemLoadAvg15: systemUsage.loadAvg15,
};
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();
}
}

View File

@@ -1,12 +1,21 @@
// this might be extracted into a package @pushrocks/smartmetrics-interfaces in the future
export interface IMetricsSnapshot {
// existing process/node fields
process_cpu_seconds_total: number;
nodejs_active_handles_total: number;
nodejs_active_requests_total: number;
nodejs_heap_size_total_bytes: number;
cpuPercentage: number;
cpuPercentage: number; // normalized to 0-100% of total machine
cpuUsageText: string;
memoryPercentage: number;
memoryUsageBytes: number;
memoryUsageText: string;
}
// system-wide fields
systemCpuPercent: number;
systemMemTotalBytes: number;
systemMemAvailableBytes: number;
systemMemUsedBytes: number;
systemMemUsedPercent: number;
systemLoadAvg1: number;
systemLoadAvg5: number;
systemLoadAvg15: number;
}

View File

@@ -0,0 +1,55 @@
import * as fs from 'fs';
// Get all descendant PIDs of the given root PID by reading /proc/<pid>/stat.
// Returns an array of descendant PIDs (excludes the root itself).
export async function getChildPids(rootPid: number): Promise<number[]> {
const parentMap = new Map<number, number[]>(); // parent → children
let entries: string[];
try {
entries = fs.readdirSync('/proc');
} catch {
return [];
}
for (const entry of entries) {
const pid = parseInt(entry, 10);
if (isNaN(pid)) continue;
try {
const stat = fs.readFileSync(`/proc/${pid}/stat`, 'utf8');
// Format: pid (comm) state ppid ...
// comm can contain spaces and parentheses, so find the last ')' first
const closeParenIdx = stat.lastIndexOf(')');
if (closeParenIdx === -1) continue;
const afterComm = stat.slice(closeParenIdx + 2); // skip ') '
const fields = afterComm.split(' ');
const ppid = parseInt(fields[1], 10); // field index 1 after state is ppid
if (!parentMap.has(ppid)) {
parentMap.set(ppid, []);
}
parentMap.get(ppid)!.push(pid);
} catch {
// Process may have exited between readdir and readFile
continue;
}
}
// BFS from rootPid to collect all descendants
const result: number[] = [];
const queue: number[] = [rootPid];
while (queue.length > 0) {
const current = queue.shift()!;
const children = parentMap.get(current);
if (children) {
for (const child of children) {
result.push(child);
queue.push(child);
}
}
}
return result;
}

129
ts/smartmetrics.pidusage.ts Normal file
View File

@@ -0,0 +1,129 @@
import * as fs from 'fs';
import * as os from 'os';
import { execSync } from 'child_process';
// CPU core count (cached at module load)
const cpuCoreCount = typeof os.availableParallelism === 'function'
? os.availableParallelism()
: os.cpus().length;
// Cached system constants
let clkTck: number | null = null;
let pageSize: number | null = null;
function getClkTck(): number {
if (clkTck === null) {
try {
clkTck = parseInt(execSync('getconf CLK_TCK', { encoding: 'utf8' }).trim(), 10);
} catch {
clkTck = 100; // standard Linux default
}
}
return clkTck;
}
function getPageSize(): number {
if (pageSize === null) {
try {
pageSize = parseInt(execSync('getconf PAGESIZE', { encoding: 'utf8' }).trim(), 10);
} catch {
pageSize = 4096; // standard Linux default
}
}
return pageSize;
}
// History for CPU delta tracking
interface ISnapshot {
utime: number;
stime: number;
timestamp: number; // hrtime in seconds
}
const history = new Map<number, ISnapshot>();
function readProcStat(pid: number): { utime: number; stime: number; rss: number } | null {
try {
const stat = fs.readFileSync(`/proc/${pid}/stat`, 'utf8');
// Format: pid (comm) state ppid ... fields
// utime is field 14, stime is field 15, rss is field 24 (1-indexed)
const closeParenIdx = stat.lastIndexOf(')');
if (closeParenIdx === -1) return null;
const afterComm = stat.slice(closeParenIdx + 2);
const fields = afterComm.split(' ');
// fields[0] = state (field 3), so utime = fields[11] (field 14), stime = fields[12] (field 15), rss = fields[21] (field 24)
const utime = parseInt(fields[11], 10);
const stime = parseInt(fields[12], 10);
const rss = parseInt(fields[21], 10);
return { utime, stime, rss };
} catch {
return null;
}
}
function hrtimeSeconds(): number {
const [sec, nsec] = process.hrtime();
return sec + nsec / 1e9;
}
export interface IPidUsageResult {
cpu: number; // raw per-core CPU% (can exceed 100%)
cpuCoreCount: number; // number of CPU cores on the machine
cpuNormalizedPercent: number; // cpu / coreCount — 0-100% of total machine
memory: number; // RSS in bytes
}
/**
* Get CPU percentage and memory usage for the given PIDs.
* CPU% is calculated as a delta between successive calls.
*/
export async function getPidUsage(
pids: number[]
): Promise<Record<number, IPidUsageResult>> {
const tck = getClkTck();
const ps = getPageSize();
const result: Record<number, IPidUsageResult> = {};
for (const pid of pids) {
const stat = readProcStat(pid);
if (!stat) {
continue;
}
const now = hrtimeSeconds();
const totalTicks = stat.utime + stat.stime;
const memoryBytes = stat.rss * ps;
const prev = history.get(pid);
if (prev) {
const elapsedSeconds = now - prev.timestamp;
const ticksDelta = totalTicks - (prev.utime + prev.stime);
const cpuSeconds = ticksDelta / tck;
const cpuPercent = elapsedSeconds > 0 ? (cpuSeconds / elapsedSeconds) * 100 : 0;
result[pid] = {
cpu: cpuPercent,
cpuCoreCount,
cpuNormalizedPercent: cpuPercent / cpuCoreCount,
memory: memoryBytes,
};
} else {
// First call for this PID — no delta available, report 0% cpu
result[pid] = {
cpu: 0,
cpuCoreCount,
cpuNormalizedPercent: 0,
memory: memoryBytes,
};
}
// Update history
history.set(pid, {
utime: stat.utime,
stime: stat.stime,
timestamp: now,
});
}
return result;
}

View File

@@ -1,26 +1,21 @@
// node native
import * as v8 from 'v8';
import * as os from 'os';
import * as fs from 'fs';
import * as http from 'http';
export {
os
}
export { v8, os, fs, http };
// pushrocks scope
import * as smartdelay from '@pushrocks/smartdelay';
import * as smartlog from '@pushrocks/smartlog';
import * as smartdelay from '@push.rocks/smartdelay';
import * as smartlog from '@push.rocks/smartlog';
export {
smartdelay,
smartlog
}
export { smartdelay, smartlog };
// third party scope
import pidusage from 'pidusage';
import pidtree from 'pidtree';
import * as promClient from 'prom-client';
// own implementations (replacing pidtree, pidusage, prom-client)
import * as pidtree from './smartmetrics.pidtree.js';
import * as pidusage from './smartmetrics.pidusage.js';
import * as prom from './smartmetrics.prom.js';
import * as sysusage from './smartmetrics.sysusage.js';
export {
pidusage,
pidtree,
promClient
}
export { pidtree, pidusage, prom, sysusage };

671
ts/smartmetrics.prom.ts Normal file
View File

@@ -0,0 +1,671 @@
import * as v8 from 'v8';
import * as fs from 'fs';
import { PerformanceObserver, monitorEventLoopDelay } from 'perf_hooks';
// ── Metric types ────────────────────────────────────────────────────────────
export interface IGaugeConfig {
name: string;
help: string;
registers?: Registry[];
labelNames?: string[];
collect?: () => void | Promise<void>;
}
export interface ICounterConfig {
name: string;
help: string;
registers?: Registry[];
labelNames?: string[];
collect?: () => void | Promise<void>;
}
export interface IHistogramConfig {
name: string;
help: string;
registers?: Registry[];
labelNames?: string[];
buckets?: number[];
collect?: () => void | Promise<void>;
}
interface IMetric {
name: string;
help: string;
type: string;
collect?: () => void | Promise<void>;
getLines(): Promise<string[]>;
}
// ── Registry ────────────────────────────────────────────────────────────────
export class Registry {
private metricsList: IMetric[] = [];
registerMetric(metric: IMetric): void {
this.metricsList.push(metric);
}
async metrics(): Promise<string> {
const lines: string[] = [];
for (const m of this.metricsList) {
if (m.collect) {
await m.collect();
}
lines.push(`# HELP ${m.name} ${m.help}`);
lines.push(`# TYPE ${m.name} ${m.type}`);
lines.push(...(await m.getLines()));
}
return lines.join('\n') + '\n';
}
}
// ── Gauge ───────────────────────────────────────────────────────────────────
export class Gauge implements IMetric {
public name: string;
public help: string;
public type = 'gauge';
public collect?: () => void | Promise<void>;
private value = 0;
private labelledValues = new Map<string, number>();
constructor(config: IGaugeConfig) {
this.name = config.name;
this.help = config.help;
this.collect = config.collect;
if (config.registers) {
for (const r of config.registers) {
r.registerMetric(this);
}
}
}
set(labelsOrValue: Record<string, string> | number, value?: number): void {
if (typeof labelsOrValue === 'number') {
this.value = labelsOrValue;
} else {
const key = this.labelsToKey(labelsOrValue);
this.labelledValues.set(key, value!);
}
}
inc(labelsOrAmount?: Record<string, string> | number, amount?: number): void {
if (labelsOrAmount === undefined) {
this.value += 1;
} else if (typeof labelsOrAmount === 'number') {
this.value += labelsOrAmount;
} else {
const key = this.labelsToKey(labelsOrAmount);
const cur = this.labelledValues.get(key) || 0;
this.labelledValues.set(key, cur + (amount ?? 1));
}
}
async getLines(): Promise<string[]> {
const lines: string[] = [];
if (this.labelledValues.size > 0) {
for (const [key, val] of this.labelledValues) {
lines.push(`${this.name}{${key}} ${formatValue(val)}`);
}
} else {
lines.push(`${this.name} ${formatValue(this.value)}`);
}
return lines;
}
/** Reset all values */
reset(): void {
this.value = 0;
this.labelledValues.clear();
}
private labelsToKey(labels: Record<string, string>): string {
return Object.entries(labels)
.map(([k, v]) => `${k}="${v}"`)
.join(',');
}
}
// ── Counter ─────────────────────────────────────────────────────────────────
export class Counter implements IMetric {
public name: string;
public help: string;
public type = 'counter';
public collect?: () => void | Promise<void>;
private value = 0;
private labelledValues = new Map<string, number>();
constructor(config: ICounterConfig) {
this.name = config.name;
this.help = config.help;
this.collect = config.collect;
if (config.registers) {
for (const r of config.registers) {
r.registerMetric(this);
}
}
}
inc(labelsOrAmount?: Record<string, string> | number, amount?: number): void {
if (labelsOrAmount === undefined) {
this.value += 1;
} else if (typeof labelsOrAmount === 'number') {
this.value += labelsOrAmount;
} else {
const key = this.labelsToKey(labelsOrAmount);
const cur = this.labelledValues.get(key) || 0;
this.labelledValues.set(key, cur + (amount ?? 1));
}
}
async getLines(): Promise<string[]> {
const lines: string[] = [];
if (this.labelledValues.size > 0) {
for (const [key, val] of this.labelledValues) {
lines.push(`${this.name}{${key}} ${formatValue(val)}`);
}
} else {
lines.push(`${this.name} ${formatValue(this.value)}`);
}
return lines;
}
reset(): void {
this.value = 0;
this.labelledValues.clear();
}
private labelsToKey(labels: Record<string, string>): string {
return Object.entries(labels)
.map(([k, v]) => `${k}="${v}"`)
.join(',');
}
}
// ── Histogram ───────────────────────────────────────────────────────────────
export class Histogram implements IMetric {
public name: string;
public help: string;
public type = 'histogram';
public collect?: () => void | Promise<void>;
private bucketBounds: number[];
private bucketCounts: number[];
private sum = 0;
private count = 0;
private labelledData = new Map<
string,
{ bucketCounts: number[]; sum: number; count: number }
>();
constructor(config: IHistogramConfig) {
this.name = config.name;
this.help = config.help;
this.bucketBounds = config.buckets || [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10];
this.bucketCounts = new Array(this.bucketBounds.length).fill(0);
this.collect = config.collect;
if (config.registers) {
for (const r of config.registers) {
r.registerMetric(this);
}
}
}
observe(labelsOrValue: Record<string, string> | number, value?: number): void {
if (typeof labelsOrValue === 'number') {
this.observeUnlabelled(labelsOrValue);
} else {
const key = this.labelsToKey(labelsOrValue);
let data = this.labelledData.get(key);
if (!data) {
data = {
bucketCounts: new Array(this.bucketBounds.length).fill(0),
sum: 0,
count: 0,
};
this.labelledData.set(key, data);
}
data.sum += value!;
data.count += 1;
for (let i = 0; i < this.bucketBounds.length; i++) {
if (value! <= this.bucketBounds[i]) {
data.bucketCounts[i]++;
}
}
}
}
private observeUnlabelled(val: number): void {
this.sum += val;
this.count += 1;
for (let i = 0; i < this.bucketBounds.length; i++) {
if (val <= this.bucketBounds[i]) {
this.bucketCounts[i]++;
}
}
}
async getLines(): Promise<string[]> {
const lines: string[] = [];
if (this.labelledData.size > 0) {
for (const [key, data] of this.labelledData) {
for (let i = 0; i < this.bucketBounds.length; i++) {
lines.push(
`${this.name}_bucket{${key},le="${this.bucketBounds[i]}"} ${data.bucketCounts[i]}`
);
}
lines.push(`${this.name}_bucket{${key},le="+Inf"} ${data.count}`);
lines.push(`${this.name}_sum{${key}} ${formatValue(data.sum)}`);
lines.push(`${this.name}_count{${key}} ${data.count}`);
}
} else {
for (let i = 0; i < this.bucketBounds.length; i++) {
lines.push(
`${this.name}_bucket{le="${this.bucketBounds[i]}"} ${this.bucketCounts[i]}`
);
}
lines.push(`${this.name}_bucket{le="+Inf"} ${this.count}`);
lines.push(`${this.name}_sum ${formatValue(this.sum)}`);
lines.push(`${this.name}_count ${this.count}`);
}
return lines;
}
reset(): void {
this.sum = 0;
this.count = 0;
this.bucketCounts.fill(0);
this.labelledData.clear();
}
private labelsToKey(labels: Record<string, string>): string {
return Object.entries(labels)
.map(([k, v]) => `${k}="${v}"`)
.join(',');
}
}
// ── Default Metrics Collectors ──────────────────────────────────────────────
export function collectDefaultMetrics(registry: Registry): void {
registerProcessCpuTotal(registry);
registerProcessStartTime(registry);
registerProcessMemory(registry);
registerProcessOpenFds(registry);
registerProcessMaxFds(registry);
registerEventLoopLag(registry);
registerProcessHandles(registry);
registerProcessRequests(registry);
registerProcessResources(registry);
registerHeapSizeAndUsed(registry);
registerHeapSpaces(registry);
registerVersion(registry);
registerGc(registry);
}
function registerProcessCpuTotal(registry: Registry): void {
const userGauge = new Gauge({
name: 'process_cpu_user_seconds_total',
help: 'Total user CPU time spent in seconds.',
registers: [registry],
collect() {
const now = process.cpuUsage();
userGauge.set(now.user / 1e6);
},
});
const systemGauge = new Gauge({
name: 'process_cpu_system_seconds_total',
help: 'Total system CPU time spent in seconds.',
registers: [registry],
collect() {
const now = process.cpuUsage();
systemGauge.set(now.system / 1e6);
},
});
new Gauge({
name: 'process_cpu_seconds_total',
help: 'Total user and system CPU time spent in seconds.',
registers: [registry],
collect() {
const now = process.cpuUsage();
this.set((now.user + now.system) / 1e6);
},
});
}
function registerProcessStartTime(registry: Registry): void {
const startTimeSeconds = Math.floor(Date.now() / 1000 - process.uptime());
new Gauge({
name: 'process_start_time_seconds',
help: 'Start time of the process since unix epoch in seconds.',
registers: [registry],
collect() {
this.set(startTimeSeconds);
},
});
}
function registerProcessMemory(registry: Registry): void {
new Gauge({
name: 'process_resident_memory_bytes',
help: 'Resident memory size in bytes.',
registers: [registry],
collect() {
this.set(process.memoryUsage.rss());
},
});
new Gauge({
name: 'process_virtual_memory_bytes',
help: 'Virtual memory size in bytes.',
registers: [registry],
collect() {
try {
const status = fs.readFileSync('/proc/self/status', 'utf8');
const match = status.match(/VmSize:\s+(\d+)\s+kB/);
if (match) {
this.set(parseInt(match[1], 10) * 1024);
}
} catch {
// not on Linux — skip
}
},
});
new Gauge({
name: 'process_heap_bytes',
help: 'Process heap size in bytes.',
registers: [registry],
collect() {
this.set(process.memoryUsage().heapUsed);
},
});
}
function registerProcessOpenFds(registry: Registry): void {
new Gauge({
name: 'process_open_fds',
help: 'Number of open file descriptors.',
registers: [registry],
collect() {
try {
const fds = fs.readdirSync('/proc/self/fd');
this.set(fds.length);
} catch {
this.set(0);
}
},
});
}
function registerProcessMaxFds(registry: Registry): void {
new Gauge({
name: 'process_max_fds',
help: 'Maximum number of open file descriptors.',
registers: [registry],
collect() {
try {
const limits = fs.readFileSync('/proc/self/limits', 'utf8');
const match = limits.match(/Max open files\s+(\d+)/);
if (match) {
this.set(parseInt(match[1], 10));
}
} catch {
this.set(0);
}
},
});
}
function registerEventLoopLag(registry: Registry): void {
let histogram: ReturnType<typeof monitorEventLoopDelay> | null = null;
try {
histogram = monitorEventLoopDelay({ resolution: 10 });
histogram.enable();
} catch {
// Not available in this runtime
}
new Gauge({
name: 'nodejs_eventloop_lag_seconds',
help: 'Lag of event loop in seconds.',
registers: [registry],
collect() {
if (histogram) {
this.set(histogram.mean / 1e9);
}
},
});
new Gauge({
name: 'nodejs_eventloop_lag_min_seconds',
help: 'The minimum recorded event loop delay.',
registers: [registry],
collect() {
if (histogram) {
this.set(histogram.min / 1e9);
}
},
});
new Gauge({
name: 'nodejs_eventloop_lag_max_seconds',
help: 'The maximum recorded event loop delay.',
registers: [registry],
collect() {
if (histogram) {
this.set(histogram.max / 1e9);
}
},
});
new Gauge({
name: 'nodejs_eventloop_lag_mean_seconds',
help: 'The mean of the recorded event loop delays.',
registers: [registry],
collect() {
if (histogram) {
this.set(histogram.mean / 1e9);
}
},
});
new Gauge({
name: 'nodejs_eventloop_lag_stddev_seconds',
help: 'The standard deviation of the recorded event loop delays.',
registers: [registry],
collect() {
if (histogram) {
this.set(histogram.stddev / 1e9);
}
},
});
for (const p of [50, 90, 99]) {
new Gauge({
name: `nodejs_eventloop_lag_p${p}_seconds`,
help: `The ${p}th percentile of the recorded event loop delays.`,
registers: [registry],
collect() {
if (histogram) {
this.set(histogram.percentile(p) / 1e9);
}
},
});
}
}
function registerProcessHandles(registry: Registry): void {
new Gauge({
name: 'nodejs_active_handles_total',
help: 'Number of active libuv handles grouped by handle type.',
registers: [registry],
collect() {
const handles = (process as any)._getActiveHandles?.();
this.set(handles ? handles.length : 0);
},
});
}
function registerProcessRequests(registry: Registry): void {
new Gauge({
name: 'nodejs_active_requests_total',
help: 'Number of active libuv requests grouped by request type.',
registers: [registry],
collect() {
const requests = (process as any)._getActiveRequests?.();
this.set(requests ? requests.length : 0);
},
});
}
function registerProcessResources(registry: Registry): void {
new Gauge({
name: 'nodejs_active_resources_total',
help: 'Number of active resources that are currently keeping the event loop alive.',
registers: [registry],
collect() {
try {
const resources = (process as any).getActiveResourcesInfo?.();
this.set(resources ? resources.length : 0);
} catch {
this.set(0);
}
},
});
}
function registerHeapSizeAndUsed(registry: Registry): void {
new Gauge({
name: 'nodejs_heap_size_total_bytes',
help: 'Process heap size from Node.js in bytes.',
registers: [registry],
collect() {
this.set(process.memoryUsage().heapTotal);
},
});
new Gauge({
name: 'nodejs_heap_size_used_bytes',
help: 'Process heap size used from Node.js in bytes.',
registers: [registry],
collect() {
this.set(process.memoryUsage().heapUsed);
},
});
new Gauge({
name: 'nodejs_external_memory_bytes',
help: 'Node.js external memory size in bytes.',
registers: [registry],
collect() {
this.set(process.memoryUsage().external);
},
});
}
function registerHeapSpaces(registry: Registry): void {
const spaceGauge = new Gauge({
name: 'nodejs_heap_space_size_total_bytes',
help: 'Process heap space size total from Node.js in bytes.',
labelNames: ['space'],
registers: [registry],
collect() {
spaceGauge.reset();
const spaces = v8.getHeapSpaceStatistics();
for (const space of spaces) {
spaceGauge.set({ space: space.space_name }, space.space_size);
}
},
});
const usedGauge = new Gauge({
name: 'nodejs_heap_space_size_used_bytes',
help: 'Process heap space size used from Node.js in bytes.',
labelNames: ['space'],
registers: [registry],
collect() {
usedGauge.reset();
const spaces = v8.getHeapSpaceStatistics();
for (const space of spaces) {
usedGauge.set({ space: space.space_name }, space.space_used_size);
}
},
});
const availableGauge = new Gauge({
name: 'nodejs_heap_space_size_available_bytes',
help: 'Process heap space size available from Node.js in bytes.',
labelNames: ['space'],
registers: [registry],
collect() {
availableGauge.reset();
const spaces = v8.getHeapSpaceStatistics();
for (const space of spaces) {
availableGauge.set({ space: space.space_name }, space.space_available_size);
}
},
});
}
function registerVersion(registry: Registry): void {
const versionParts = process.version.slice(1).split('.').map(Number);
const gauge = new Gauge({
name: 'nodejs_version_info',
help: 'Node.js version info.',
labelNames: ['version', 'major', 'minor', 'patch'],
registers: [registry],
collect() {
gauge.set(
{
version: process.version,
major: String(versionParts[0]),
minor: String(versionParts[1]),
patch: String(versionParts[2]),
},
1
);
},
});
}
function registerGc(registry: Registry): void {
const gcHistogram = new Histogram({
name: 'nodejs_gc_duration_seconds',
help: 'Garbage collection duration by kind, in seconds.',
labelNames: ['kind'],
buckets: [0.001, 0.01, 0.1, 1, 2, 5],
registers: [registry],
});
const kindLabels: Record<number, string> = {
1: 'Scavenge',
2: 'Mark/Sweep/Compact',
4: 'IncrementalMarking',
8: 'ProcessWeakCallbacks',
15: 'All',
};
try {
const obs = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
const gcEntry = entry as any;
const kind = kindLabels[gcEntry.detail?.kind ?? gcEntry.kind] || 'Unknown';
gcHistogram.observe({ kind }, entry.duration / 1000);
}
});
obs.observe({ entryTypes: ['gc'] });
} catch {
// GC observation not available
}
}
// ── Helpers ─────────────────────────────────────────────────────────────────
function formatValue(v: number): string {
if (Number.isInteger(v)) return String(v);
return v.toString();
}

View File

@@ -0,0 +1,94 @@
import * as fs from 'fs';
import * as os from 'os';
export interface ISystemUsageResult {
cpuPercent: number; // 0-100% system-wide CPU utilization
memTotalBytes: number; // total physical RAM
memAvailableBytes: number; // available memory
memUsedBytes: number; // memTotal - memAvailable
memUsedPercent: number; // 0-100%
loadAvg1: number; // 1-min load average
loadAvg5: number; // 5-min load average
loadAvg15: number; // 15-min load average
}
// History for system CPU delta tracking
interface ICpuSnapshot {
idle: number;
total: number;
}
let prevCpuSnapshot: ICpuSnapshot | null = null;
function readProcStat(): ICpuSnapshot | null {
try {
const content = fs.readFileSync('/proc/stat', 'utf8');
const firstLine = content.split('\n')[0]; // "cpu user nice system idle iowait irq softirq steal ..."
const parts = firstLine.split(/\s+/).slice(1).map(Number);
// parts: [user, nice, system, idle, iowait, irq, softirq, steal, ...]
const idle = parts[3] + (parts[4] || 0); // idle + iowait
const total = parts.reduce((sum, v) => sum + v, 0);
return { idle, total };
} catch {
return null;
}
}
function getMemoryInfo(): { totalBytes: number; availableBytes: number } {
try {
const content = fs.readFileSync('/proc/meminfo', 'utf8');
let memTotal = 0;
let memAvailable = 0;
for (const line of content.split('\n')) {
if (line.startsWith('MemTotal:')) {
memTotal = parseInt(line.split(/\s+/)[1], 10) * 1024; // kB to bytes
} else if (line.startsWith('MemAvailable:')) {
memAvailable = parseInt(line.split(/\s+/)[1], 10) * 1024;
}
}
if (memTotal > 0 && memAvailable > 0) {
return { totalBytes: memTotal, availableBytes: memAvailable };
}
} catch {
// fall through to os fallback
}
// Fallback using os module
const totalBytes = os.totalmem();
const availableBytes = os.freemem();
return { totalBytes, availableBytes };
}
export async function getSystemUsage(): Promise<ISystemUsageResult> {
// CPU
let cpuPercent = 0;
const currentSnapshot = readProcStat();
if (currentSnapshot && prevCpuSnapshot) {
const totalDelta = currentSnapshot.total - prevCpuSnapshot.total;
const idleDelta = currentSnapshot.idle - prevCpuSnapshot.idle;
if (totalDelta > 0) {
cpuPercent = ((totalDelta - idleDelta) / totalDelta) * 100;
}
}
if (currentSnapshot) {
prevCpuSnapshot = currentSnapshot;
}
// Memory
const mem = getMemoryInfo();
const memUsedBytes = mem.totalBytes - mem.availableBytes;
const memUsedPercent = mem.totalBytes > 0 ? (memUsedBytes / mem.totalBytes) * 100 : 0;
// Load averages
const [loadAvg1, loadAvg5, loadAvg15] = os.loadavg();
return {
cpuPercent,
memTotalBytes: mem.totalBytes,
memAvailableBytes: mem.availableBytes,
memUsedBytes,
memUsedPercent,
loadAvg1,
loadAvg5,
loadAvg15,
};
}

14
tsconfig.json Normal file
View File

@@ -0,0 +1,14 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"verbatimModuleSyntax": true
},
"exclude": [
"dist_*/**/*.d.ts"
]
}

View File

@@ -1,17 +0,0 @@
{
"extends": ["tslint:latest", "tslint-config-prettier"],
"rules": {
"semicolon": [true, "always"],
"no-console": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"member-ordering": {
"options":{
"order": [
"static-method"
]
}
}
},
"defaultSeverity": "warning"
}