43 Commits

Author SHA1 Message Date
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
29609eb5ba 1.0.15 2021-08-19 17:29:16 +02:00
0292d9bc13 fix(core): update 2021-08-19 17:29:15 +02:00
69af53a30a 1.0.14 2021-08-15 13:45:33 +02:00
121bfdad43 fix(core): update 2021-08-15 13:45:32 +02:00
f96ed80be0 1.0.13 2021-08-14 22:47:31 +02:00
ce74c3da40 fix(core): update 2021-08-14 22:47:31 +02:00
c108c00963 1.0.12 2021-08-14 21:42:25 +02:00
25de5d5874 fix(core): update 2021-08-14 21:42:25 +02:00
9822d470e3 1.0.11 2021-08-14 21:41:06 +02:00
a425ed64c3 fix(core): update 2021-08-14 21:41:06 +02:00
2eb4bc5697 1.0.10 2021-08-14 21:37:53 +02:00
dd46f0d90b fix(core): update 2021-08-14 21:37:52 +02:00
0972f1c64d 1.0.9 2021-08-14 21:31:50 +02:00
22b181a7a0 fix(core): update 2021-08-14 21:31:49 +02:00
e43b89f51d 1.0.8 2021-08-14 18:15:32 +02:00
3185c7dab1 fix(core): update 2021-08-14 18:15:32 +02:00
ef6d625ac9 1.0.7 2021-08-14 17:02:41 +02:00
f5e8af9a06 fix(core): update 2021-08-14 17:02:40 +02:00
cd2cc0685e 1.0.6 2021-08-14 17:01:55 +02:00
1b8f4f2219 fix(core): update 2021-08-14 17:01:54 +02:00
533639b66d 1.0.5 2021-08-13 13:13:12 +02:00
8f0ebccb58 fix(core): update 2021-08-13 13:13:11 +02:00
e98b1779e1 1.0.4 2021-08-13 11:55:38 +02:00
df5197fc8e fix(core): update 2021-08-13 11:55:38 +02:00
0e3fadca36 1.0.3 2021-08-12 23:31:11 +02:00
18 changed files with 4936 additions and 25941 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 @gitzone/tsdoc
npmci command tsdoc
continue-on-error: true

View File

@ -12,20 +12,12 @@ stages:
- release
- metadata
before_script:
- npm install -g @shipzone/npmci
# ====================
# 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
@ -36,6 +28,7 @@ auditProductionDependencies:
- npmci command npm audit --audit-level=high --only=prod --production
tags:
- docker
allow_failure: true
auditDevDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
@ -96,10 +89,9 @@ codequality:
only:
- tags
script:
- npmci command npm install -g tslint typescript
- npmci command npm install -g typescript
- npmci npm prepare
- npmci npm install
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
tags:
- lossless
- docker
@ -119,11 +111,10 @@ trigger:
pages:
stage: metadata
script:
- npmci node install lts
- npmci command npm install -g @gitzone/tsdoc
- npmci node install stable
- npmci npm prepare
- npmci npm install
- npmci command tsdoc
- npmci command npm run buildDocs
tags:
- lossless
- docker

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"
}
]
}

View File

@ -5,7 +5,7 @@
"githost": "gitlab.com",
"gitscope": "pushrocks",
"gitrepo": "smartmetrics",
"shortDescription": "easy system metrics",
"description": "easy system metrics",
"npmPackagename": "@pushrocks/smartmetrics",
"license": "MIT",
"projectDomain": "push.rocks"

25791
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartmetrics",
"version": "1.0.2",
"version": "2.0.6",
"private": false,
"description": "easy system metrics",
"main": "dist_ts/index.js",
@ -9,16 +9,16 @@
"license": "MIT",
"scripts": {
"test": "(tstest test/ --web)",
"build": "(tsbuild --web)"
"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"
"@gitzone/tsbuild": "^2.1.66",
"@gitzone/tsbundle": "^2.0.8",
"@gitzone/tsrun": "^1.2.42",
"@gitzone/tstest": "^1.0.74",
"@pushrocks/tapbundle": "^5.0.8",
"@types/node": "^20.3.3"
},
"browserslist": [
"last 1 chrome versions"
@ -36,8 +36,12 @@
"readme.md"
],
"dependencies": {
"@pushrocks/smartdelay": "^2.0.13",
"@pushrocks/smartlog": "^2.0.44",
"prom-client": "^13.2.0"
}
"@pushrocks/smartdelay": "^3.0.1",
"@pushrocks/smartlog": "^3.0.2",
"@types/pidusage": "^2.0.2",
"pidtree": "^0.6.0",
"pidusage": "^3.0.2",
"prom-client": "^14.2.0"
},
"type": "module"
}

4605
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -21,7 +21,6 @@ Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](htt
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)
## Usage
@ -33,7 +32,6 @@ We are always happy for code contributions. If you are not the code contributing
For further information read the linked docs at the top of this readme.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
## Legal
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
[![repo-footer](https://lossless.gitlab.io/publicrelations/repofooter.svg)](https://maintainedby.lossless.com)

View File

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

View File

@ -1,26 +1,27 @@
import * as plugins from './plugins';
import * as plugins from './plugins.js';
import { expect, tap } from '@pushrocks/tapbundle';
import * as smartmetrics from '../ts/index';
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.start();

8
ts/00_commitinfo_data.ts Normal file
View File

@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
*/
export const commitinfo = {
name: '@pushrocks/smartmetrics',
version: '2.0.6',
description: 'easy system 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,12 +1,12 @@
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;
public async setup() {
const collectDefaultMetrics = plugins.promClient.collectDefaultMetrics;
@ -18,46 +18,49 @@ export class SmartMetrics {
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}`, {
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,8 +77,31 @@ 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} `);
const returnMetrics: interfaces.IMetricsSnapshot = {
originalMetrics,
process_cpu_seconds_total: (
originalMetrics.find((metricSet) => metricSet.name === 'process_cpu_seconds_total') as any
).values[0].value,
@ -92,25 +118,16 @@ export class SmartMetrics {
(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,
cpuPercentage,
cpuUsageText,
memoryPercentage,
memoryUsageBytes,
memoryUsageText,
};
return returnMetrics;
}
public async stop() {}
public stop() {
this.started = false;
}
}

View File

@ -1,11 +1,12 @@
// 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;
}
}

View File

@ -1,22 +1,19 @@
// node native
import * as v8 from 'v8';
import * as os from 'os';
import * as fs from 'fs';
export {
os
}
export { v8, os, fs };
// pushrocks scope
import * as smartdelay from '@pushrocks/smartdelay';
import * as smartlog from '@pushrocks/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 };

11
tsconfig.json Normal file
View File

@ -0,0 +1,11 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "nodenext",
"esModuleInterop": true,
"verbatimModuleSyntax": true,
}
}

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"
}