smartmetrics/readme.md
2024-04-14 17:58:26 +02:00

121 lines
5.1 KiB
Markdown

# @push.rocks/smartmetrics
easy system metrics
## Install
To include `@push.rocks/smartmetrics` in your project, you need a Node.js environment with support for ES Modules. Make sure that your `package.json` contains `"type": "module"`. You can install `@push.rocks/smartmetrics` using npm:
```bash
npm install @push.rocks/smartmetrics --save
```
Or using yarn:
```bash
yarn add @push.rocks/smartmetrics
```
## Usage
The `@push.rocks/smartmetrics` package provides an easy way to collect and monitor system metrics such as CPU and memory usage within your Node.js applications. It's built with TypeScript, offering type safety and IntelliSense in editors that support it.
### Getting Started
Before diving into the code, ensure you have `@push.rocks/smartmetrics` installed in your project and your environment is configured to use ES Modules (ESM) and TypeScript.
#### Initialization
To begin using `smartmetrics`, you need to import the module and initialize it:
```typescript
// Import dependencies
import { SmartMetrics } from '@push.rocks/smartmetrics';
import { Smartlog } from '@push.rocks/smartlog';
// Setup a logger (or use your existing logger if applicable)
const logger = new Smartlog({
logContext: 'applicationName',
minimumLogLevel: 'info',
});
// Initialize smartmetrics with the prepared logger
const smartMetrics = new SmartMetrics(logger, 'sourceName');
```
In this snippet, `Smartlog` is used for logging purposes. Replace `'applicationName'` and `'sourceName'` with appropriate values for your application.
#### Starting Metrics Monitoring
With `smartMetrics` initialized, you can start the metrics monitoring process:
```typescript
smartMetrics.start();
```
This starts the collection and monitoring of various system metrics. By default, `smartMetrics` sends heartbeat messages including these metrics at a regular interval.
#### Metrics Collection
To manually collect metrics at any point, you can call the `getMetrics` method. This could be useful for logging or sending metrics to a monitoring tool:
```typescript
async function logMetrics() {
const metrics = await smartMetrics.getMetrics();
console.log(metrics);
}
logMetrics();
```
#### Customizing Monitoring
The `start` method begins an unattended process of collecting and logging metrics. If you require more control over how and when metrics are collected or reported, you can use the `getMetrics` method in combination with your logic.
#### Stopping Metrics Monitoring
To stop the automatic metrics monitoring, simply call:
```typescript
smartMetrics.stop();
```
This stops the internal loop that periodically collects and logs metrics. It's useful for clean shutdowns of your application or when metrics monitoring is only needed during specific intervals.
### Understanding the Metrics
The `getMetrics` method returns a snapshot of various system metrics, including CPU and memory usage. Here's a brief overview of the information provided:
- `process_cpu_seconds_total`: Total CPU time spent by the process.
- `nodejs_active_handles_total`: Number of active handles.
- `nodejs_active_requests_total`: Number of active requests.
- `nodejs_heap_size_total_bytes`: Total size of the heap.
- `cpuPercentage`: Overall CPU usage percentage.
- `cpuUsageText`: Readable string representation of CPU usage.
- `memoryPercentage`: Percentage of memory used.
- `memoryUsageBytes`: Total memory used in bytes.
- `memoryUsageText`: Readable string representation of memory usage.
### Conclusion
`@push.rocks/smartmetrics` offers a straightforward and efficient way to monitor essential system metrics of your Node.js application. By integrating it, you gain valuable insights into the performance and health of your system, aiding in diagnosis and optimization efforts.
## 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.