==================
Abstraction library for time-series metrics in Node.js JS applications.
This is a collection of redis patterns I've been using in prod. I'm moving them into their own module for community sharing and collaboration.
Initially the feature set is geared towards my production use cases. I'm happy to accept issues/PRs for changes that allow for a broader range of use cases.
If you'd like to improve this module, please see the contributing section.
npm install redis-metrics
Each abstraction accepts a configuration object at instantiation.
Options
- name - Base namespace
- client - a redis client instance
Default Values
{
name : "metric",
client: null
}
Example:
Multiple instances sharing one redis client
redis = require 'redis'
client = redis.createClient()
{Leaderboard,Counter} = require 'redis-metrics'
postRankings = new Leaderboard
name: "post_rankings"
client: client
pageVisits = new Counter
name: "visits"
client: client
Metric base class. Provides sugar for date and key namespace
Methods
- key([namespace...]) - returns current namespace. Accepts an optional array of namespace tags to update the current namespace.
- hour() - returns hour
- day() - returns day
- month() - returns month
- year() - returns year
Extends Metric. Provides bitmap metrics, where each item represents a unique offset.
Methods
- set(offset,[callback]) - Set bit at offset
- unset(offset,[callback]) - Unset bit at offset
- count(key) - return population count for key
- size(key) - return size for key
- key([namespace...]) - return namespace based off
this.name
Example:
foo = new BitMap "page_visits"
app.get '/post/:id', (req,res) ->
getPostFromDB (err,record) ->
foo.set record.id
...
res.render 'post', record
Result (assuming record has an ID of '3'):
page_visits:2013:10:01:18 001000
page_visits:2013:10:01 001000
page_visits:2013:10: 001000
page_visits:2013 001000
Extends Metric. Provides a simple counter
Extends Metric. Provides a named leaderboard
- Each metric abstraction should live in its own file in
src/lib/
- The file name should match the class name (ex 'BitMap.coffee')
- New abstractions should extend the 'Metric' base class.
Pull requests are checked by TravisCI, with a 100% code coverage requirement.
make test