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:
// Create an instancevar RedisMetrics = ;var metrics = ; // If you need to catch uncaught exceptions, add an error handler to the client.metricsclient; // Create a counter for a "pageview" event and increment it three times.var myCounter = metrics;myCounter;myCounter;myCounter; // Fetch the count for myCounter, using a callback.myCounter; // Fetch the count for myCounter, using promise.myCounter;
Time-aware counter.
// Create an instancevar RedisMetrics = ;var metrics = ; // If you need to catch uncaught exceptions, add an error handler to the client.metricsclient; // Use the timeGranularity option to specify how specific the counter should be// when incrementing.var myCounter = metrics; // Fetch the count for myCounter for the current year.myCounter ; // Fetch the count for each of the last two hours.// We are using moment here for convenience.var moment = ;var now = ;var lastHour = ;myCounter ; // Fetch the count for each day in the last 30 daysvar thirtyDaysAgo = ;myCounter ; // Fetch the count for the last 60 seconds...// ... Sorry, you can't do that because the counter is only set up to track by// the hour.
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