davetapley
davetapley

Reputation: 17968

Log changes to a model with Librato

I have a Devise model:

class Account < ActiveRecord::Base
  devise ::trackable

I'd like to see the number of times an Account is created and signed in to, in Librato.

Upvotes: 0

Views: 61

Answers (1)

davetapley
davetapley

Reputation: 17968

Using Librato's log stream parsing you can record the events with a $stdout.puts.

I recommend extracting this log to a concern, and using model callbacks to monitor changes.

We can create a file in lib/librato/account.rb:

module Librato
  module Account
    extend ActiveSupport::Concern

    included do
      after_create do
        $stdout.puts 'count#account.create=1'
      end

      after_save if: :current_sign_in_at_changed? do
        $stdout.puts 'count#account.sign_in=1'
      end
    end
  end
end

And then include it in your model, thus:

class Account < ActiveRecord::Base
  include Librato::Account

Upvotes: 0

Related Questions