redis-metrics
Easy metric tracking and aggregation using Redis.
This module was originally created to provide an easy way of storing and
viewing aggregated counter and trend statistics.
In a sense, the libary tries to provide sugar-coated method calls for storing
and fetching Redis data to report counts and trends. The first design goal is to
make counting simpler.
Read on below or read more on the documentation site.
Install
$ npm install --save redis-metrics
Use
Basic counter:
var RedisMetrics = require('redis-metrics');
var metrics = new RedisMetrics();
metrics.client.on('error', function(err) { ... });
var myCounter = metrics.counter('pageview');
myCounter.incr();
myCounter.incr();
myCounter.incr();
myCounter.count(function(cnt) {
console.log(cnt);
});
myCounter.count().then(function(cnt) {
console.log(cnt);
});
Time-aware counter.
var RedisMetrics = require('redis-metrics');
var metrics = new RedisMetrics();
metrics.client.on('error', function(err) { ... });
var myCounter = metrics.counter('pageview', { timeGranularity: 'hour' });
myCounter.count('year')
.then(function(cnt) {
console.log(cnt);
});
var moment = require('moment');
var now = moment();
var lastHour = moment(now).subtract(1, 'hours');
myCounter.countRange('hour', lastHour, now)
.then(function(obj) {
});
var thirtyDaysAgo = moment(now).subtract(30, 'days');
myCounter.countRange('day', thirtyDaysAgo, now)
.then(function(obj) {
});
Redis Namespace
By default keys are stored in Redis as c:{name}:{period}
. If you prefer to use a different Redis namespace than c
, you can pass this in as an option:
var myCounter = metrics.counter('pageview', { timeGranularity: 'hour', namespace: 'stats' });`
Test
Run tests including code coverage:
$ npm test
Documentation
The internal module documentation is based on jsdoc and
can be generated with:
$ npm run docs