Class: ErrorStalker::Store::Mongoid
- Defined in:
- lib/error_stalker/store/mongoid.rb
Overview
Store exceptions using MongoDB. This store provides fast storage and querying of exceptions, and long-term persistence. It also allows querying based on arbitrary data stored in the data
hash of the exception report, which allows for crazy things like searching reports by URL or IP address.
Defined Under Namespace
Classes: Application, ExceptionGroup, ExceptionReport, Machine, SchemaMigrations
Instance Method Summary collapse
-
#applications ⇒ Object
All applications that have been seen by this store.
-
#create_indexes ⇒ Object
Creates the MongoDB indexes used by this driver.
-
#empty? ⇒ Boolean
Have we logged any exceptions?.
-
#find(id) ⇒ Object
Find an exception report with the given id.
-
#group(digest) ⇒ Object
returns the ExceptionGroup object corresponding to a particular digest.
-
#initialize(config_file) ⇒ Mongoid
constructor
Configure mongoid from the mongoid config file found in
config_file
. -
#machines ⇒ Object
All machines that have been seen by this store.
-
#migrate_data ⇒ Object
Migrate the data in the mongoid database to a newer format.
-
#recent ⇒ Object
Return the last 7 days worth of unique exception reports grouped by exception group.
-
#reports_in_group(digest) ⇒ Object
Returns all the exceptions in a group, ordered by most_recent_timestamp.
-
#search(params) ⇒ Object
Searches for exception reports maching
params
. -
#store(exception_report) ⇒ Object
Store
exception_report
in the database. -
#supports_extended_searches? ⇒ Boolean
Does this store support searching through the data blob?.
- #total ⇒ Object
- #total_since(timestamp) ⇒ Object
Constructor Details
#initialize(config_file) ⇒ Mongoid
Configure mongoid from the mongoid config file found in config_file
. This mongoid config file should be similar to the one on mongoid.org/docs/installation/, and must be indexed by environment name. config_file
is relative to either wherever you start the server from or the config.ru file, unless you pass a full file path.
19 20 21 22 23 24 25 26 27 |
# File 'lib/error_stalker/store/mongoid.rb', line 19 def initialize(config_file) filename = File.(config_file) settings = YAML.load(ERB.new(File.new(filename).read).result) Mongoid.configure do |config| config.from_hash(settings[ENV['RACK_ENV']]) end Thread.new { migrate_data } end |
Instance Method Details
#applications ⇒ Object
All applications that have been seen by this store
60 61 62 |
# File 'lib/error_stalker/store/mongoid.rb', line 60 def applications Application.all.order_by(:name.asc).map(&:name) end |
#create_indexes ⇒ Object
Creates the MongoDB indexes used by this driver. Should be called at some point after deciding to use the mongoid store. Can be called either manually, or by running bin/create_indexes
127 128 129 130 |
# File 'lib/error_stalker/store/mongoid.rb', line 127 def create_indexes ErrorStalker::Store::Mongoid::ExceptionReport.create_indexes ErrorStalker::Store::Mongoid::ExceptionGroup.create_indexes end |
#empty? ⇒ Boolean
Have we logged any exceptions?
37 38 39 |
# File 'lib/error_stalker/store/mongoid.rb', line 37 def empty? ExceptionGroup.where(:most_recent_timestamp.gt => 7.days.ago).count == 0 end |
#find(id) ⇒ Object
Find an exception report with the given id.
55 56 57 |
# File 'lib/error_stalker/store/mongoid.rb', line 55 def find(id) ExceptionReport.find(id).to_exception_report end |
#group(digest) ⇒ Object
returns the ExceptionGroup object corresponding to a particular digest
77 78 79 |
# File 'lib/error_stalker/store/mongoid.rb', line 77 def group(digest) ExceptionGroup.where(:digest => digest).first end |
#machines ⇒ Object
All machines that have been seen by this store
65 66 67 |
# File 'lib/error_stalker/store/mongoid.rb', line 65 def machines Machine.all.order_by(:name.asc).map(&:name) end |
#migrate_data ⇒ Object
Migrate the data in the mongoid database to a newer format. This should eventually be more robust, like the Rails version, but for now this should be fine.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/error_stalker/store/mongoid.rb', line 135 def migrate_data if SchemaMigrations.where(:version => "1").empty? ExceptionGroup.all.order_by(:timestamp).desc.each do |group| exceptions = ExceptionReport.where(:digest => group.digest).order_by(:timestamp) unless exceptions.empty? exceptions = exceptions.to_a group.attributes[:timestamp] = nil group. = exceptions[0]. group. = exceptions[-1]. group.machines = exceptions.map(&:machine).uniq group.save end end SchemaMigrations.create(:version => "1") end end |
#recent ⇒ Object
Return the last 7 days worth of unique exception reports grouped by exception group.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/error_stalker/store/mongoid.rb', line 43 def recent # Needs to be wrapped in a PaginationHelper because we'll call # paginate on the collection returned from this method. We don't # want to use mongoid pagination here because we don't know what # parameters we want to paginate on, and we don't want to return # an array here because we don't want to load all the mongid # models in memory. This is also made trickier because of my # hacked-up :include stuff I built into ExceptionGroup. ExceptionGroup::PaginationHelper.new(ExceptionGroup.where(:most_recent_timestamp.gt => 7.days.ago).order_by(:most_recent_timestamp.desc)) end |
#reports_in_group(digest) ⇒ Object
Returns all the exceptions in a group, ordered by most_recent_timestamp
71 72 73 |
# File 'lib/error_stalker/store/mongoid.rb', line 71 def reports_in_group(digest) ExceptionReport.where(:digest => digest).order_by(:timestamp.desc) end |
#search(params) ⇒ Object
Searches for exception reports maching params
. Supports querying by arbitrary data in the data
hash associated with the exception, with the format:
REMOTE_ADDR:127.0.0.1 PATH:/test
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/error_stalker/store/mongoid.rb', line 98 def search(params) scope = ExceptionReport.all [:application, :machine].each do |param| if params[param] && !params[param].empty? puts "HERE: #{params.inspect}" scope = scope.where(param => params[param]) end end [:exception, :type].each do |param| if params[param] && !params[param].empty? scope = scope.where(param => /#{params[param]}/) end end if params[:data] && !params[:data].empty? params[:data].split.each do |keyvalue| key, value = keyvalue.split(':') scope = scope.where("data" => {"#{key}" => "#{value}"}) end end scope.order_by(:timestamp.desc) end |
#store(exception_report) ⇒ Object
Store exception_report
in the database.
30 31 32 33 34 |
# File 'lib/error_stalker/store/mongoid.rb', line 30 def store(exception_report) report = ExceptionReport.create_from_exception_report(exception_report) update_caches(report) report.id end |
#supports_extended_searches? ⇒ Boolean
Does this store support searching through the data blob?
82 83 84 |
# File 'lib/error_stalker/store/mongoid.rb', line 82 def supports_extended_searches? true end |
#total ⇒ Object
86 87 88 |
# File 'lib/error_stalker/store/mongoid.rb', line 86 def total ExceptionReport.count() end |
#total_since(timestamp) ⇒ Object
90 91 92 |
# File 'lib/error_stalker/store/mongoid.rb', line 90 def total_since() ExceptionReport.where(:timestamp.gte => ).count() end |