@@ -9,7 +9,7 @@ A small express middleware to get base metrics for any node.js app.
## Usage
`import { Metrics } from '@plastdev/metrics'`
`import { Metrics, MetricType } from '@plastdev/metrics'`
`let metrics = new Metrics(options)`
Note: The options Part may be omitted, as all parts are optional.
...
...
@@ -20,16 +20,93 @@ Before your Routes:
And to enable the *_metrics*-Endpoint:
`router.get('/_metrics', metrics.endpoint)`
### Custom Metrics
To Add your own custom Metrics:
`metrics.addCustomMetric({
name: 'test',
help: 'Some Test Metric',
labelNames: ['tester']
})`
help: 'Some Test Metric'
}, TYPE)`
where TYPE is one of:
-`MetricType.COUNTER`
-`MetricType.GAUGE`
-`MetricType.HISTOGRAM`
-`MetricType.SUMMARY`
All metrics can take a labelNames property in the configuration object. All labelNames that the metric support needs to be declared here. There are 2 ways to add values to the labels
```js
metrics.addCustomMetric({
name:'metric_name',
help:'metric_help',
labelNames:['method','statusCode'],
},MetricType.GAUGE);
metrics.customMetrics["NAME"].set({method:'GET',statusCode:'200'},100);// 1st version, Set value 100 with method set to GET and statusCode to 200
metrics.customMetrics["NAME"].labels('GET','200').set(100);// 2nd version, Same as above
```
It is also possible to use timers with labels, both before and after the timer is created:
```js
constend=metrics.customMetrics["NAME"].startTimer({method:'GET'});// Set method to GET, we don't know statusCode yet
xhrRequest(function (err,res){
if (err){
end({statusCode:'500'});// Sets value to xhrRequest duration in seconds with statusCode 500
}else{
end({statusCode:'200'});// Sets value to xhrRequest duration in seconds with statusCode 200
}
});
```
#### Counter
Counters go up, and reset when the process restarts.
-`metrics.customMetrics["NAME"].inc(); // Inc with 1`
-`metrics.customMetrics["NAME"].inc(10); // Inc with 10`
#### Gauge
Gauges are similar to Counters but Gauges value can be decreased.
-`metrics.customMetrics["NAME"].set(10); // Set to 10`
-`metrics.customMetrics["NAME"].inc(); // Inc with 1`
-`metrics.customMetrics["NAME"].inc(10); // Inc with 10`
-`metrics.customMetrics["NAME"].dec(); // Dec with 1`
-`metrics.customMetrics["NAME"].dec(10); // Dec with 10`
#### Histograms
Histograms track sizes and frequency of events.
`metrics.customMetrics["NAME"].observe(10); // Observe value in histogram`