# Metrics

A small express middleware to get base metrics for any node.js app.


## Installation

- `npm install --save @plastdev/metrics`

## Usage

`import { Metrics } from '@plastdev/metrics'`  
`let metrics = new Metrics(options)`

Note: The options Part may be omitted, as all parts are optional.  

Before your Routes:  
`router.use(metrics.collect)`  

And to enable the *_metrics*-Endpoint:  
`router.get('/_metrics', metrics.endpoint)`  

To Add your own custom Metrics:
`metrics.addCustomMetric({
      name: 'test',
      help: 'Some Test Metric',
      labelNames: ['tester']
    })`

then you may increase or decrease the value at any given point:
`this.metrics.incCustomMetric('test', 'tester', 'called_bar')`
`this.metrics.decCustomMetric('test', 'tester', 'called_bar')`

### Options

The Following Options may be used to configure the behaviour.

- ignore: A String Array with routes to ignore, e.g. ['/foo'] . Default: []. Important: */_metrics* and */favicon.ico* are always ignored
- disableErrorCounter: Disable the Error Counter. Default: false
- disableRouteCounter: Disable the Route Counter. Default: false
- disableDurationCounter: Disable the Duration Counter. Default: false
- disableDefaultMetrics: Disable the Collection of default metrics. Default: false

#### Default Metrics

- process_cpu_user_seconds_total Total user CPU time spent in seconds.
- process_cpu_system_seconds_total Total system CPU time spent in seconds.
- process_cpu_seconds_total Total user and system CPU time spent in seconds.
- process_start_time_seconds Start time of the process since unix epoch in seconds.
- process_resident_memory_bytes Resident memory size in bytes.
- nodejs_eventloop_lag_seconds Lag of event loop in seconds.
- nodejs_eventloop_lag_min_seconds The minimum recorded event loop delay.
- nodejs_eventloop_lag_max_seconds The maximum recorded event loop delay.
- nodejs_eventloop_lag_mean_seconds The mean of the recorded event loop delays.
- nodejs_eventloop_lag_stddev_seconds The standard deviation of the recorded event loop delays.
- nodejs_eventloop_lag_p50_seconds The 50th percentile of the recorded event loop delays.
- nodejs_eventloop_lag_p90_seconds The 90th percentile of the recorded event loop delays.
- nodejs_eventloop_lag_p99_seconds The 99th percentile of the recorded event loop delays.
- nodejs_active_handles Number of active libuv handles grouped by handle type. Every handle type is C++ class name.
- nodejs_active_handles_total Total number of active handles.
- nodejs_active_requests Number of active libuv requests grouped by request type. Every request type is C++ class name.
- nodejs_active_requests_total Total number of active requests.
- nodejs_heap_size_total_bytes Process heap size from Node.js in bytes.
- nodejs_heap_size_used_bytes Process heap size used from Node.js in bytes.
- nodejs_external_memory_bytes Node.js external memory size in bytes.
- nodejs_heap_space_size_total_bytes Process heap space size total from Node.js in bytes.
- nodejs_heap_space_size_used_bytes Process heap space size used from Node.js in bytes.
- nodejs_heap_space_size_available_bytes Process heap space size available from Node.js in bytes.
- nodejs_version_info Node.js version info.

## Examples

You can find prebuilt examples in the fitting folder.

Just call `node examples/dist/server.js 3000` to start a Test-Server on Port 3000.

Then use your Browser to call the Endpoints:  

- /foo
- /bar
- /404
- /401

And check the results on the Endpoint */_metrics*