felix-metrics
This is an alternative port of Coda Hale's metrics library.
I created this despite the existing metrics port for node.js because I wanted to fully understand the underlaying Math and algorithms.
Install
npm install felix-metrics
Usage
Step 1: Add metrics to your code. For example, lets track the requests/sec of a http server:
var metrics = ;var collection = 'http';var http = ; var rps = collection;http;
Step 2: Show the collected metrics (more advanced examples follow later):
;
This will output something like this every second:
{ requestsPerSecond:
{ mean: 1710.2180279856818,
count: 10511,
'1MinuteRate': 168.08263156623656,
'5MinuteRate': 34.74630977619571,
'15MinuteRate': 11.646507524106095 } }
Step 3: Aggregate the data into your backend of choice. I recommend graphite.
Metrics
The following metrics are available (both standalone and on the Collection API):
Gauge
Values that can be read instantly. Example:
var gauge = { return processrss;});
There is currently no callback support for Gauges because otherwise it would be very difficult to report the metrics inside a collection within a regular interval.
Options:
read
A function that returns the current value of the Gauge.
Methods:
None.
Counter
Things that increment or decrement. Example:
var activeUploads = ;http;
Options:
count
An initial count for the counter. Defaults to0
.
Methods:
inc(n)
Increment the counter byn
. Defaults to1
.dec(n)
Decrement the counter byn
. Defaults to1
.
Meter
Things that are measured as events / interval. Example:
var meter = ;http;
Options:
rateUnit
The rate unit. Defaults to1000
(1 sec).tickInterval
The interval in which the averages are updated. Defaults to5000
(5 sec).
Methods:
mark(n)
Registern
events as having just occured. Defaults to `1.
Histogram
Things that are measured as distributions of scalars. Example:
var histogram = ;http;
Options:
sample
The sample resevoir to use. Defaults to anExponentiallyDecayingSample
.
Methods:
update(value, timestamp)
Pushesvalue
into the sample.timestamp
defaults toDate.now()
.
Timers
Timers are a combination of Meters and Histograms. They measure the rate as well as distribution of scalar events. Since they are frequently used for tracking how long certain things take, they expose an API for that:
var timer = ;http;
But you can also use them as generic histograms that also track the rate of events:
var timer = ;http;
Options:
meter
The internal meter to use. Defaults to a newMeter
.histogram
The internal histogram to use. Defaults to a newHistogram
.
Methods:
start()
Returns aStopwatch
.update(value)
Updates the internal histogram withvalue
and marks one event on the internal meter.
Todo
- Implement a graphite reporter and feature it in the usage section.
License
This module is licensed under the MIT license.