tempo v0.1.0-pre10
Tempo
Scalable time-based counters that meant logging, graphing and providing a realtime statistics. All features in tempo are meant to have a constant size memory footprint defined by configuration.
For a quick example, please look at examples/simple-use-case.js
Features
- simple counters that sync over redis using a hash structure
- counters that keep track of historical data in buckets
- syncing with redis for aggregation on multiple servers
- sparse logging: keep a defined number of random entries in a log
Use Case
Lets say you are running a website and want to know in realtime where people are visiting.
var redis = require('redis').createClient();
var tempo = require('tempo');
// create middleware to track counts
// create time counter, increment and sync
var min = tempo.min();
app.use(function (req, res, next) {
// the '1' is unnecessary because increment defaults to '1'
min.inc('requests', 1);
next();
})
function showRequestsOverTime() {
var history = tempo.getHistory('requests');
var times = tempo.getTimes();
for (var i=0; i<history.length; i++) {
console.log("Requsts at " + (new Date(times[i])).toString() + ': ' + history[i]);
}
console.log(tempo.getCount('requests') + ' request(s) made in the last minute');
}TimedCounter
The tempo TimedCounter class allows you to create a datastore object and keep data in memory.
Instance methods
var timedCounter = new tempo.TimedCounter(options)
- options hash
- per: milliseconds per bucket
- buckets: number of buckets
- timeout (optional): ttl pass expiration time
Example for keeping data up to an hour of history:
var tempo = require('tempo');
var ds = new tempo.TimedCounter({ per: 60000, buckets: 60 });timedCounter.incr(key, n);
- key: entity name
- n (optional, defaults to 1): a number to increment by.
Keeping track of how many times a user has logged in in the past hour:
var ds = require('tempo').hour();
ds.increment(userId);timedCounter.getHistory(key, attr1, attr2, ...)
- key: entity name
Grabbing logged in counts:
var history = ds.getHistory(userId);Returns an array of counts (per bucket)
timedCounter.sync(redis, namespace)
- redis client
- prefix/namespace for the key to store in redis
- tempo's keys will look something like ":"
timedCounter.sync(redis, 'web-stats', callback);13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago