Librato is a great platform to track metrics and has a really simple gem to track your rack based apps, but what if yours is not one? And if you're on Heroku you can't use StatsD... So what do you do?

It turns out that Librato can get metrics from the application's log so by adding a drain to your Heroku app you'll have the simplest metrics tracking library ever:

class Metrics
  def self.increment(metric, amount = 1)
    logger.info "count##{metric}=#{amount}"
  end

  def self.time(metric, **options, &block)
    if block_given?
      start_time = Time.now
      block.call
      total = (Time.now - start_time) * 1000
    else
      total = options[:time]
    end
    logger.info "measure##{metric}=#{total.round}ms" if total
  end

  def self.gauge(metric, amount)
    logger.info "sample##{metric}=#{amount}"
  end

  def self.logger
    @logger ||= Logger.new(STDOUT)
  end
  private_class_method :logger
end

And now getting metrics from your application is really, really simple:

Metrics.increment('shares')
Metrics.gauge('queue_size', 1)
Metrics.time('expensive_operation') { # do some work }

You can check out all the things you can do with Librato and log metrics at the Heroku DevCenter.

Metrics in your logs

Probably the thing I like the most about this approach is having your metrics in your application log waiting for them to be can be consumed and analyzed from any service... Your application is a producer, Librato is just a consumer.

You could add Kibana with its powerful queries to get stuff out from the metrics (and other data), or have multiple teams consume the metrics and build different applications.

Setting up alerts based on metrics becomes trivial with services like Papertrail

Bottom line, logs are awesome, and you should take advantage of that!