Skip to content

Instantly share code, notes, and snippets.

@devpuppy
Created January 25, 2012 21:23
Show Gist options
  • Save devpuppy/1678802 to your computer and use it in GitHub Desktop.
Save devpuppy/1678802 to your computer and use it in GitHub Desktop.
Airbrake exporter + stats
# code at http://gist.github.com/3350
# tests at http://gist.github.com/3354
require 'rubygems'
require 'active_resource'
require 'time'
class Hoptoad < ActiveResource::Base
self.site = "https://YOUR_ACCOUNT.hoptoadapp.com"
class << self
@@auth_token = 'YOUR_API_TOKEN'
def find(*arguments)
arguments = append_auth_token_to_params(*arguments)
super(*arguments)
end
def append_auth_token_to_params(*arguments)
opts = arguments.last.is_a?(Hash) ? arguments.pop : {}
opts = opts.has_key?(:params) ? opts : opts.merge(:params => {})
opts[:params] = opts[:params].merge(:auth_token => @@auth_token)
arguments << opts
arguments
end
end
end
class Error < Hoptoad
end
# Errors are paginated. You get 30 at a time.
NUM_DAYS_AGO = 7
NUM_SECONDS_AGO = 60*60*24*NUM_DAYS_AGO
@errors = Error.find :all, :conditions => {:rails_env => 'production'}, :order => 'most_recent_notice_at DESC'
next_page = 2
puts @errors.last.most_recent_notice_at
while @errors.last.most_recent_notice_at > (Time.now - NUM_SECONDS_AGO)
puts '.'
@errors += Error.find :all, :conditions => {:rails_env => 'production'}, :order => 'most_recent_notice_at DESC', :params => { :page => next_page}
next_page += 1
end
checkpoints = [3, 6, 12, 24, 48, 72, 168]
indices = []
checkpoints.each do |checkpoint|
e = @errors.detect{|e| e.most_recent_notice_at < (Time.now - (checkpoint * 3600))} # find first older error
indices << @errors.index(e)
end
puts "Hours ago\t#{checkpoints.join("\t")}"
puts "Exception count\t#{indices.join("\t")}"
@headers = ["first seen, most recently seen, error message, count, controller, action, airbrake link\n"]
@errors.collect! do |error|
first_occurrence = error.created_at
"#{error.created_at}, #{error.most_recent_notice_at}, #{error.error_message.gsub(/,/, ';')}, #{error.notices_count}, #{error.controller}, #{error.action}, https://airbnb.airbrakeapp.com/errors/#{error.id}\n"
end
file = File.open("airbrake_errors-#{Time.now.to_i}.csv", 'w') {|f| f.write(@headers + @errors) }
puts "#{@errors.size} errors downloaded"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment