I've been playing around with trying to make it as simple as possible to start logging a few metrics with as little effort in setup as possible.
As I was working on Papertrail I came up with a couple interesting ideas, some of which were inspired by OpenTSDB.
In this example we are pretending we're processing emails as background jobs and would like to track how many we've sent over time.
First, we initialize a metric:
@metric = RedisTimeseriesMetric.new('emails', %w(hostname))
The counter is called "emails" and has a single tag called "hostname".
When processing the emails in our background job, we would invoke:
@metric.incr(1, Time.now, :hostname => Socket.gethostname)
to increment the counter.
To retrieve an overall count of emails that have been sent over the past day, we would use:
@metric.count(1.day.ago, Time.now)
To retrieve the count of emails that have been sent by a specific server:
@metric.count(1.day.ago, Time.now, :hostname => 'email1')
To retrieve a list of per hour email counts over the past day:
@metric.timeseries(1.day.ago, Time.now, :hostname => 'email1')
What if you wanted to graph a sparkline of the traffic? We could use jquery.sparkline.js:
<span class="sparkline" values="<%= @metric.timeseries(1.day.ago, Time.now).join(',') %>"></span>
<script type="text/javascript">$('sparkline').sparkline('html');</script>
There are a number of interesting things that could be added to this:
- Time-based roll-ups to quickly get results based on daily measurements
- Ability to store counters that are read from other sources (like interface octets)
- Limit storage to so it doesn't grow endlessly