Commit cd8cde49668933eda93ce127093316f3bc924930

Authored by Marcin Ciunelis
1 parent bdbf3530
Exists in master and in 1 other branch production

Err#fingerprint calculated from many attributes

app/models/app.rb
@@ -74,7 +74,7 @@ class App @@ -74,7 +74,7 @@ class App
74 end 74 end
75 75
76 def find_or_create_err!(attrs) 76 def find_or_create_err!(attrs)
77 - Err.where(attrs).first || problems.create!.errs.create!(attrs) 77 + Err.where(:fingerprint => attrs[:fingerprint]).first || problems.create!.errs.create!(attrs)
78 end 78 end
79 79
80 # Mongoid Bug: find(id) on association proxies returns an Enumerator 80 # Mongoid Bug: find(id) on association proxies returns an Enumerator
app/models/err.rb
@@ -15,6 +15,7 @@ class Err @@ -15,6 +15,7 @@ class Err
15 belongs_to :problem 15 belongs_to :problem
16 index :problem_id 16 index :problem_id
17 index :error_class 17 index :error_class
  18 + index :fingerprint
18 19
19 has_many :notices, :inverse_of => :err, :dependent => :destroy 20 has_many :notices, :inverse_of => :err, :dependent => :destroy
20 21
app/models/error_report.rb
1 -require 'digest/md5' 1 +require 'digest/sha1'
2 require 'hoptoad_notifier' 2 require 'hoptoad_notifier'
3 3
4 class ErrorReport 4 class ErrorReport
@@ -10,11 +10,7 @@ class ErrorReport @@ -10,11 +10,7 @@ class ErrorReport
10 end 10 end
11 11
12 def fingerprint 12 def fingerprint
13 - normalized_backtrace = backtrace[0...3].map do |trace|  
14 - trace.merge 'method' => trace['method'].gsub(/[0-9_]{10,}+/, "__FRAGMENT__")  
15 - end  
16 -  
17 - @fingerprint ||= Digest::MD5.hexdigest(normalized_backtrace.to_s) 13 + @fingerprint ||= Digest::SHA1.hexdigest(fingerprint_source.to_s)
18 end 14 end
19 15
20 def rails_env 16 def rails_env
@@ -53,5 +49,24 @@ class ErrorReport @@ -53,5 +49,24 @@ class ErrorReport
53 err.notices << notice 49 err.notices << notice
54 notice 50 notice
55 end 51 end
  52 +
  53 + private
  54 + def fingerprint_source
  55 + {
  56 + :backtrace => normalized_backtrace.to_s,
  57 + :error_class => error_class,
  58 + :component => component,
  59 + :action => action,
  60 + :environment => rails_env,
  61 + :api_key => api_key
  62 + }
  63 + end
  64 +
  65 + def normalized_backtrace
  66 + backtrace[0...3].map do |trace|
  67 + trace.merge 'method' => trace['method'].gsub(/[0-9_]{10,}+/, "__FRAGMENT__")
  68 + end
  69 + end
  70 +
56 end 71 end
57 72
lib/tasks/errbit/database.rake
  1 +require 'digest/sha1'
  2 +
1 namespace :errbit do 3 namespace :errbit do
2 namespace :db do 4 namespace :db do
3 5
@@ -21,5 +23,35 @@ namespace :errbit do @@ -21,5 +23,35 @@ namespace :errbit do
21 Problem.resolved.each {|problem| problem.destroy } 23 Problem.resolved.each {|problem| problem.destroy }
22 puts "=== Cleared #{count} resolved errors from the database." if count > 0 24 puts "=== Cleared #{count} resolved errors from the database." if count > 0
23 end 25 end
  26 +
  27 + desc "Regenerate fingerprints"
  28 + task :regenerate_fingerprints => :environment do
  29 +
  30 + def normalize_backtrace(backtrace)
  31 + backtrace[0...3].map do |trace|
  32 + trace.merge 'method' => trace['method'].gsub(/[0-9_]{10,}+/, "__FRAGMENT__")
  33 + end
  34 + end
  35 +
  36 + def fingerprint(source)
  37 + Digest::SHA1.hexdigest(source.to_s)
  38 + end
  39 +
  40 + puts "Regenerating Err fingerprints"
  41 + Err.create_indexes
  42 + Err.all.limit(10).each do |err|
  43 + next if err.notices.count == 0
  44 + source = {
  45 + :backtrace => normalize_backtrace(err.notices.first.backtrace).to_s,
  46 + :error_class => err.error_class,
  47 + :component => err.component,
  48 + :action => err.action,
  49 + :environment => err.environment,
  50 + :api_key => err.app.api_key
  51 + }
  52 + err.update_attributes(:fingerprint => fingerprint(source))
  53 + end
  54 + end
  55 +
24 end 56 end
25 end 57 end