Compare commits
53 Commits
Author | SHA1 | Date | |
---|---|---|---|
77675c6367 | |||
c47732c5fc | |||
f587884e86 | |||
30992ea44b | |||
34b09ed7a7 | |||
9413e0323a | |||
e514209fb8 | |||
fc4bc118d6 | |||
19ce45deaa | |||
1e582023f7 | |||
d6877f9adf | |||
861e9ab0be | |||
1460fedbcb | |||
537df1d53e | |||
352d0f8a1e | |||
8454244f3b | |||
4b6d6feea1 | |||
01306ed3f9 | |||
2a2ca66708 | |||
299e50cbf7 | |||
1e6af24df0 | |||
66f0dc6815 | |||
7d703fe57e | |||
2ddccdaab1 | |||
173b3e2b19 | |||
df7a499c1d | |||
173df91528 | |||
057a0a329e | |||
e53bebf7a1 | |||
c4f5e9338a | |||
dad2bc26fc | |||
29609eb5ba | |||
0292d9bc13 | |||
69af53a30a | |||
121bfdad43 | |||
f96ed80be0 | |||
ce74c3da40 | |||
c108c00963 | |||
25de5d5874 | |||
9822d470e3 | |||
a425ed64c3 | |||
2eb4bc5697 | |||
dd46f0d90b | |||
0972f1c64d | |||
22b181a7a0 | |||
e43b89f51d | |||
3185c7dab1 | |||
ef6d625ac9 | |||
f5e8af9a06 | |||
cd2cc0685e | |||
1b8f4f2219 | |||
533639b66d | |||
8f0ebccb58 |
66
.gitea/workflows/default_nottags.yaml
Normal file
66
.gitea/workflows/default_nottags.yaml
Normal 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
|
124
.gitea/workflows/default_tags.yaml
Normal file
124
.gitea/workflows/default_tags.yaml
Normal 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
|
137
.gitlab-ci.yml
137
.gitlab-ci.yml
@ -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
24
.vscode/launch.json
vendored
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
40
changelog.md
Normal file
40
changelog.md
Normal file
@ -0,0 +1,40 @@
|
||||
# Changelog
|
||||
|
||||
## 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.
|
@ -2,17 +2,36 @@
|
||||
"gitzone": {
|
||||
"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"
|
||||
]
|
||||
}
|
||||
},
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"tsdoc": {
|
||||
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||
}
|
||||
}
|
25791
package-lock.json
generated
25791
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
57
package.json
57
package.json
@ -1,24 +1,23 @@
|
||||
{
|
||||
"name": "@pushrocks/smartmetrics",
|
||||
"version": "1.0.4",
|
||||
"name": "@push.rocks/smartmetrics",
|
||||
"version": "2.0.9",
|
||||
"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 --web)",
|
||||
"buildDocs": "tsdoc"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tsbundle": "^1.0.78",
|
||||
"@gitzone/tstest": "^1.0.44",
|
||||
"@pushrocks/tapbundle": "^3.2.9",
|
||||
"@types/node": "^16.6.0",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
"@git.zone/tsbuild": "^2.6.4",
|
||||
"@git.zone/tsbundle": "^2.0.8",
|
||||
"@git.zone/tsrun": "^1.2.44",
|
||||
"@git.zone/tstest": "^2.3.1",
|
||||
"@types/node": "^22.15.30"
|
||||
},
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
@ -36,8 +35,34 @@
|
||||
"readme.md"
|
||||
],
|
||||
"dependencies": {
|
||||
"@pushrocks/smartdelay": "^2.0.13",
|
||||
"@pushrocks/smartlog": "^2.0.44",
|
||||
"prom-client": "^13.2.0"
|
||||
}
|
||||
"@push.rocks/smartdelay": "^3.0.1",
|
||||
"@push.rocks/smartlog": "^3.0.2",
|
||||
"@types/pidusage": "^2.0.2",
|
||||
"pidtree": "^0.6.0",
|
||||
"pidusage": "^4.0.1",
|
||||
"prom-client": "^15.1.3"
|
||||
},
|
||||
"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"
|
||||
}
|
||||
|
8842
pnpm-lock.yaml
generated
Normal file
8842
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
30
readme.hints.md
Normal file
30
readme.hints.md
Normal 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.
|
290
readme.md
290
readme.md
@ -1,39 +1,271 @@
|
||||
# @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
|
||||
## What is SmartMetrics?
|
||||
|
||||
Status Category | Status Badge
|
||||
-- | --
|
||||
GitLab Pipelines | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
||||
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.
|
||||
|
||||
## Usage
|
||||
## Key Features
|
||||
|
||||
Use TypeScript for best in class intellisense
|
||||
- 📊 **Real-time Metrics Collection** - Monitor CPU and memory usage across all processes
|
||||
- 🔄 **Automatic Child Process Tracking** - Aggregates metrics from main and child processes
|
||||
- 🐳 **Docker-Aware** - Detects container memory limits automatically
|
||||
- 📈 **Prometheus Integration** - Built-in HTTP endpoint for Prometheus scraping
|
||||
- 🔧 **Flexible Output Formats** - Get metrics as JSON objects or Prometheus text
|
||||
- 📝 **Automatic Heartbeat Logging** - Optional periodic metrics logging
|
||||
- 🚀 **Zero Configuration** - Works out of the box with sensible defaults
|
||||
|
||||
## Contribution
|
||||
## Installation
|
||||
|
||||
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). :)
|
||||
```bash
|
||||
npm install @push.rocks/smartmetrics
|
||||
```
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
## Quick Start
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
```typescript
|
||||
import { SmartMetrics } from '@push.rocks/smartmetrics';
|
||||
import { Smartlog } from '@push.rocks/smartlog';
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
||||
// Create a logger instance
|
||||
const logger = new Smartlog({
|
||||
logContext: 'my-app',
|
||||
minimumLogLevel: 'info'
|
||||
});
|
||||
|
||||
// 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 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. This gives you a complete picture of your application's resource footprint.
|
||||
|
||||
### 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.
|
||||
|
||||
### Dual Output Formats
|
||||
- **JSON Format**: Ideal for application monitoring, custom dashboards, and programmatic access
|
||||
- **Prometheus Format**: Perfect for integration with Prometheus/Grafana monitoring stacks
|
||||
|
||||
## API Reference
|
||||
|
||||
### Constructor
|
||||
|
||||
```typescript
|
||||
new SmartMetrics(logger: Smartlog, sourceName: string)
|
||||
```
|
||||
- `logger`: Smartlog instance for output
|
||||
- `sourceName`: Identifier for your service/application
|
||||
|
||||
### Methods
|
||||
|
||||
#### `async getMetrics(): Promise<IMetricsSnapshot>`
|
||||
Retrieves current system metrics as a JSON object.
|
||||
|
||||
**Returns:**
|
||||
```typescript
|
||||
{
|
||||
process_cpu_seconds_total: number; // Total CPU time in seconds
|
||||
nodejs_active_handles_total: number; // Active handles count
|
||||
nodejs_active_requests_total: number; // Active requests count
|
||||
nodejs_heap_size_total_bytes: number; // Heap size in bytes
|
||||
cpuPercentage: number; // Current CPU usage (0-100)
|
||||
cpuUsageText: string; // Human-readable CPU usage
|
||||
memoryPercentage: number; // Memory usage percentage
|
||||
memoryUsageBytes: number; // Memory usage in bytes
|
||||
memoryUsageText: string; // Human-readable memory usage
|
||||
}
|
||||
```
|
||||
|
||||
**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. Logs metrics every 20 seconds.
|
||||
|
||||
**Example:**
|
||||
```typescript
|
||||
smartMetrics.start();
|
||||
// Logs: "sending heartbeat for my-service with metrics" every 20 seconds
|
||||
```
|
||||
|
||||
#### `stop(): void`
|
||||
Stops automatic metrics collection and closes any open endpoints.
|
||||
|
||||
#### `async getPrometheusFormattedMetrics(): Promise<string>`
|
||||
Returns metrics in Prometheus text exposition format.
|
||||
|
||||
**Example:**
|
||||
```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
|
||||
// ...
|
||||
```
|
||||
|
||||
#### `enablePrometheusEndpoint(port?: number): void`
|
||||
Starts an HTTP server that exposes metrics for Prometheus scraping.
|
||||
|
||||
**Parameters:**
|
||||
- `port`: Port number (default: 9090)
|
||||
|
||||
**Example:**
|
||||
```typescript
|
||||
smartMetrics.enablePrometheusEndpoint(3000);
|
||||
// Metrics now available at http://localhost:3000/metrics
|
||||
```
|
||||
|
||||
#### `disablePrometheusEndpoint(): void`
|
||||
Stops the Prometheus HTTP endpoint.
|
||||
|
||||
## Use Cases
|
||||
|
||||
### 1. Application Performance Monitoring
|
||||
```typescript
|
||||
// Monitor performance during critical operations
|
||||
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`);
|
||||
```
|
||||
|
||||
### 2. Resource Limit Enforcement
|
||||
```typescript
|
||||
// Prevent operations when resources are constrained
|
||||
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
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Prometheus + Grafana Monitoring
|
||||
```typescript
|
||||
// Expose metrics for Prometheus
|
||||
smartMetrics.enablePrometheusEndpoint();
|
||||
|
||||
// In your Prometheus config:
|
||||
// scrape_configs:
|
||||
// - job_name: 'my-app'
|
||||
// static_configs:
|
||||
// - targets: ['localhost:9090']
|
||||
```
|
||||
|
||||
### 4. Development and Debugging
|
||||
```typescript
|
||||
// Track memory leaks during development
|
||||
setInterval(async () => {
|
||||
const metrics = await smartMetrics.getMetrics();
|
||||
console.log(`Heap: ${metrics.nodejs_heap_size_total_bytes / 1024 / 1024}MB`);
|
||||
}, 5000);
|
||||
```
|
||||
|
||||
### 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
|
||||
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',
|
||||
metrics: {
|
||||
cpu: metrics.cpuUsageText,
|
||||
memory: metrics.memoryUsageText
|
||||
}
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
### With PM2
|
||||
```typescript
|
||||
// Graceful shutdown on high memory
|
||||
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);
|
||||
```
|
||||
|
||||
### 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
|
||||
|
||||
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.
|
||||
|
||||
### Trademarks
|
||||
|
||||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||||
|
||||
### Company Information
|
||||
|
||||
Task Venture Capital GmbH
|
||||
Registered at District court Bremen HRB 35230 HB, Germany
|
||||
|
||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||
|
||||
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
81
readme.plan.md
Normal 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`
|
@ -1,5 +1,3 @@
|
||||
import * as smartlog from '@pushrocks/smartlog';
|
||||
import * as smartlog from '@push.rocks/smartlog';
|
||||
|
||||
export {
|
||||
smartlog
|
||||
}
|
||||
export { smartlog };
|
||||
|
66
test/test.ts
66
test/test.ts
@ -1,26 +1,78 @@
|
||||
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 () => {
|
||||
testSmartMetrics.start();
|
||||
});
|
||||
|
||||
tap.test('should produce valid metrics', async () => {
|
||||
tap.test('should produce valid metrics', async (tools) => {
|
||||
console.log('calling .getMetrics from Testfile:');
|
||||
console.log(await testSmartMetrics.getMetrics());
|
||||
})
|
||||
});
|
||||
|
||||
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');
|
||||
}
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @push.rocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartmetrics',
|
||||
version: '2.0.9',
|
||||
description: 'A package for easy collection and reporting of system and process metrics.'
|
||||
}
|
@ -1,2 +1,2 @@
|
||||
export * from './smartmetrics.interfaces';
|
||||
export * from './smartmetrics.classes.smartmetrics';
|
||||
export * from './smartmetrics.interfaces.js';
|
||||
export * from './smartmetrics.classes.smartmetrics.js';
|
||||
|
@ -1,64 +1,94 @@
|
||||
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 maxMemoryMB: number;
|
||||
|
||||
// Prometheus gauges for custom metrics
|
||||
private cpuPercentageGauge: plugins.promClient.Gauge<string>;
|
||||
private memoryPercentageGauge: plugins.promClient.Gauge<string>;
|
||||
private memoryUsageBytesGauge: plugins.promClient.Gauge<string>;
|
||||
|
||||
// HTTP server for Prometheus endpoint
|
||||
private prometheusServer?: plugins.http.Server;
|
||||
private prometheusPort?: number;
|
||||
|
||||
public async setup() {
|
||||
const collectDefaultMetrics = plugins.promClient.collectDefaultMetrics;
|
||||
this.registry = new plugins.promClient.Registry();
|
||||
collectDefaultMetrics({ register: this.registry });
|
||||
|
||||
// Initialize custom gauges
|
||||
this.cpuPercentageGauge = new plugins.promClient.Gauge({
|
||||
name: 'smartmetrics_cpu_percentage',
|
||||
help: 'Current CPU usage percentage',
|
||||
registers: [this.registry]
|
||||
});
|
||||
|
||||
this.memoryPercentageGauge = new plugins.promClient.Gauge({
|
||||
name: 'smartmetrics_memory_percentage',
|
||||
help: 'Current memory usage percentage',
|
||||
registers: [this.registry]
|
||||
});
|
||||
|
||||
this.memoryUsageBytesGauge = new plugins.promClient.Gauge({
|
||||
name: 'smartmetrics_memory_usage_bytes',
|
||||
help: 'Current memory usage in bytes',
|
||||
registers: [this.registry]
|
||||
});
|
||||
}
|
||||
|
||||
constructor(loggerArg: plugins.smartlog.Smartlog, sourceNameArg: string) {
|
||||
this.logger = loggerArg;
|
||||
this.sourceNameArg = sourceNameArg;
|
||||
this.setup();
|
||||
this.checkMemoryLimits();
|
||||
}
|
||||
|
||||
public async start() {
|
||||
if (this.started) {
|
||||
return;
|
||||
private checkMemoryLimits() {
|
||||
let heapStats = plugins.v8.getHeapStatistics();
|
||||
let maxHeapSizeMB = heapStats.heap_size_limit / 1024 / 1024;
|
||||
let totalSystemMemoryMB = plugins.os.totalmem() / 1024 / 1024;
|
||||
|
||||
let dockerMemoryLimitMB = totalSystemMemoryMB;
|
||||
try {
|
||||
let dockerMemoryLimitBytes = plugins.fs.readFileSync(
|
||||
'/sys/fs/cgroup/memory/memory.limit_in_bytes',
|
||||
'utf8'
|
||||
);
|
||||
dockerMemoryLimitMB = parseInt(dockerMemoryLimitBytes, 10) / 1024 / 1024;
|
||||
} catch (error) {
|
||||
// Ignore - this will fail if not running in a Docker container
|
||||
}
|
||||
this.started = true;
|
||||
while (this.started) {
|
||||
this.logger.log('info', `sending heartbeat for ${this.sourceNameArg} with metrics`, {
|
||||
eventType: 'heartbeat',
|
||||
metrics: await this.getMetrics(),
|
||||
});
|
||||
await plugins.smartdelay.delayFor(60000, null, true);
|
||||
|
||||
// Set the maximum memory to the lower value between the Docker limit and the total system memory
|
||||
this.maxMemoryMB = Math.min(totalSystemMemoryMB, dockerMemoryLimitMB, maxHeapSizeMB);
|
||||
|
||||
// If the maximum old space size limit is greater than the maximum available memory, throw an error
|
||||
if (maxHeapSizeMB > this.maxMemoryMB) {
|
||||
throw new Error('Node.js process can use more memory than is available');
|
||||
}
|
||||
}
|
||||
|
||||
public getCpuUsagePercentage() {
|
||||
// Take the first CPU, considering every CPUs have the same specs
|
||||
// and every NodeJS process only uses one at a time.
|
||||
const cpus = plugins.os.cpus();
|
||||
let total: number = 0;
|
||||
|
||||
for (const cpu of cpus) {
|
||||
total +=
|
||||
cpu.times.user +
|
||||
cpu.times.sys +
|
||||
cpu.times.nice +
|
||||
cpu.times.nice +
|
||||
cpu.times.irq +
|
||||
cpu.times.idle;
|
||||
}
|
||||
|
||||
// Normalize the one returned by process.cpuUsage()
|
||||
// (microseconds VS miliseconds)
|
||||
const usage = process.cpuUsage();
|
||||
const currentCPUUsage = (usage.user + usage.system) * 1000;
|
||||
|
||||
// Find out the percentage used for this specific CPU
|
||||
const perc = (currentCPUUsage / total) * 100;
|
||||
|
||||
return perc;
|
||||
public start() {
|
||||
const unattendedStart = async () => {
|
||||
if (this.started) {
|
||||
return;
|
||||
}
|
||||
this.started = true;
|
||||
while (this.started) {
|
||||
this.logger.log('info', `sending heartbeat for ${this.sourceNameArg} with metrics`, {
|
||||
eventType: 'heartbeat',
|
||||
metrics: await this.getMetrics(),
|
||||
});
|
||||
await plugins.smartdelay.delayFor(20000, null, true);
|
||||
}
|
||||
};
|
||||
unattendedStart();
|
||||
}
|
||||
|
||||
public formatBytes(bytes: number, decimals = 2) {
|
||||
@ -74,44 +104,120 @@ 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 cpuPercentage = 0;
|
||||
for (const stat of Object.keys(stats)) {
|
||||
if (!stats[stat]) continue;
|
||||
cpuPercentage += stats[stat].cpu;
|
||||
}
|
||||
let cpuUsageText = `${Math.round(cpuPercentage * 100) / 100} %`;
|
||||
|
||||
let memoryUsageBytes = 0;
|
||||
for (const stat of Object.keys(stats)) {
|
||||
if (!stats[stat]) continue;
|
||||
memoryUsageBytes += stats[stat].memory;
|
||||
}
|
||||
|
||||
let memoryPercentage =
|
||||
Math.round((memoryUsageBytes / (this.maxMemoryMB * 1024 * 1024)) * 100 * 100) / 100;
|
||||
let memoryUsageText = `${memoryPercentage}% | ${this.formatBytes(
|
||||
memoryUsageBytes
|
||||
)} / ${this.formatBytes(this.maxMemoryMB * 1024 * 1024)}`;
|
||||
|
||||
console.log(`${cpuUsageText} ||| ${memoryUsageText} `);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// Calculate Node.js metrics directly
|
||||
const cpuUsage = process.cpuUsage();
|
||||
const process_cpu_seconds_total = (cpuUsage.user + cpuUsage.system) / 1000000; // Convert from microseconds to seconds
|
||||
|
||||
const heapStats = plugins.v8.getHeapStatistics();
|
||||
const nodejs_heap_size_total_bytes = heapStats.total_heap_size;
|
||||
|
||||
// Note: Active handles and requests are internal Node.js metrics that require deprecated APIs
|
||||
// We return 0 here, but the Prometheus default collectors will track the real values
|
||||
const nodejs_active_handles_total = 0;
|
||||
const nodejs_active_requests_total = 0;
|
||||
|
||||
const returnMetrics: interfaces.IMetricsSnapshot = {
|
||||
originalMetrics,
|
||||
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,
|
||||
cpuPercentage: Math.round(this.getCpuUsagePercentage() * 100) / 100,
|
||||
memoryUsageText: this.formatBytes(
|
||||
(
|
||||
originalMetrics.find(
|
||||
(metricSet) => metricSet.name === 'nodejs_heap_size_total_bytes'
|
||||
) as any
|
||||
).values[0].value
|
||||
),
|
||||
memoryPercentage:
|
||||
Math.round(((
|
||||
originalMetrics.find(
|
||||
(metricSet) => metricSet.name === 'nodejs_heap_size_total_bytes'
|
||||
) as any
|
||||
).values[0].value /
|
||||
plugins.os.totalmem()) *
|
||||
100 * 100) / 100,
|
||||
process_cpu_seconds_total,
|
||||
nodejs_active_handles_total,
|
||||
nodejs_active_requests_total,
|
||||
nodejs_heap_size_total_bytes,
|
||||
cpuPercentage,
|
||||
cpuUsageText,
|
||||
memoryPercentage,
|
||||
memoryUsageBytes,
|
||||
memoryUsageText,
|
||||
};
|
||||
return returnMetrics;
|
||||
}
|
||||
|
||||
public async stop() {}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
// this might be extracted into a package @pushrocks/smartmetrics-interfaces in the future
|
||||
export interface IMetricsSnapshot {
|
||||
originalMetrics: any[];
|
||||
process_cpu_seconds_total: number;
|
||||
nodejs_active_handles_total: number;
|
||||
nodejs_active_requests_total: number;
|
||||
nodejs_heap_size_total_bytes: number;
|
||||
cpuPercentage: number;
|
||||
cpuUsageText: string;
|
||||
memoryPercentage: number;
|
||||
memoryUsageBytes: number;
|
||||
memoryUsageText: string;
|
||||
}
|
||||
}
|
||||
|
@ -1,22 +1,20 @@
|
||||
// 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';
|
||||
|
||||
export {
|
||||
promClient
|
||||
}
|
||||
export { pidusage, pidtree, promClient };
|
||||
|
14
tsconfig.json
Normal file
14
tsconfig.json
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"esModuleInterop": true,
|
||||
"verbatimModuleSyntax": true
|
||||
},
|
||||
"exclude": [
|
||||
"dist_*/**/*.d.ts"
|
||||
]
|
||||
}
|
17
tslint.json
17
tslint.json
@ -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"
|
||||
}
|
Reference in New Issue
Block a user