lib/rackamole/mole.rb in rackamole-0.0.7 vs lib/rackamole/mole.rb in rackamole-0.0.8
- old
+ new
@@ -28,10 +28,11 @@
# :user_key => :user_name
# Or you can eval it on the fly - though this will be much slower and not recommended
# :user_key => { :session_key => :user_id, :extractor => lambda{ |id| User.find( id ).name} }
# ==
#
+ # :excluded_paths:: Exclude paths that you do not wish to mole by specifying an array of regular expresssions.
# :twitter_auth :: You can setup the MOle twit interesting events to a private (public if you indulge pain!) twitter account.
# Specified your twitter account information using a hash with :username and :password key
# :twitt_on :: You must configure your twitter auth and configuration using this hash. By default this option is disabled.
# ==
# :twitt_on => { :enabled => false, :features => [Rackamole.perf, Rackamole.fault] }
@@ -84,58 +85,72 @@
end
# Mole default options
def default_options
{
+ :moleable => true,
:app_name => "Moled App",
+ :environment => 'test',
:excluded_paths => [/.?\.ico/, /.?\.png/],
- :moleable => true,
- :perf_threshold => 10,
+ :perf_threshold => 10.0,
:store => Rackamole::Store::Log.new,
:twitt_on => { :enabled => false, :features => [Rackamole.perf, Rackamole.fault] },
:mail_on => { :enabled => false, :features => [Rackamole.perf, Rackamole.fault] }
}
end
# Validates all configured options... Throws error if invalid configuration
def validate_options
- %w[app_name moleable perf_threshold store].each do |k|
+ return unless options[:moleable]
+
+ %w[app_name environment perf_threshold store].each do |k|
raise "[M()le] -- Unable to locate required option key `#{k}" unless options[k.to_sym]
end
+
+ configured?( :twitter_auth, [:username, :password] )
+ configured?( :twitt_on , [:enabled, :features] )
+ configured?( :emails , [:from, :to] )
+ configured?( :mail_on , [:enabled, :features] )
end
# Send moled info to store and potentially send out alerts...
def mole_feature( env, elapsed, status, headers, body )
attrs = mole_info( env, elapsed, status, headers, body )
# send info to configured store
options[:store].mole( attrs )
# send email alert ?
- if configured?( :emails, [:from, :to] ) and alertable?( options[:mail_on], attrs[:type] )
+ if configured?( :emails, [:from, :to] ) and alertable?( :mail_on, attrs[:type] )
Rackamole::Alert::Emole.deliver_alert( options[:emails][:from], options[:emails][:to], attrs )
end
# send twitter alert ?
- if configured?( :twitter_auth, [:username, :password] ) and alertable?( options[:twitt_on], attrs[:type] )
+ if configured?( :twitter_auth, [:username, :password] ) and alertable?( :twitt_on, attrs[:type] )
twitt.send_alert( attrs )
end
rescue => boom
$stderr.puts "!! MOLE RECORDING CRAPPED OUT !! -- #{boom}"
boom.backtrace.each { |l| $stderr.puts l }
end
# Check if an options is set and configured
def configured?( key, configs )
- return false unless options[key]
- configs.each { |c| return false unless options[key][c] }
+ return false unless options.key?(key)
+ configs.each do |c|
+ raise "Invalid value for option #{key}. Expecting a hash with symbols #{configs.join(',')}." unless options[key].respond_to? :key?
+ unless options[key].key?(c)
+ raise "Option #{key} is not properly configured missing #{c.inspect}"
+ end
+ end
true
end
# Check if feature should be send to alert clients ie email or twitter
- def alertable?( filters, type )
- return false if !filters or filters.empty? or !filters[:enabled]
- filters[:features].include?( type )
+ def alertable?( filter, type )
+ return false unless configured?( filter, [:enabled,:features] )
+ return false unless options[filter][:enabled]
+ options[filter][:features].include?( type )
end
# Create or retrieve twitter client
def twitt
@twitt ||= Rackamole::Alert::Twitt.new( options[:twitter_auth][:username], options[:twitter_auth][:password] )
\ No newline at end of file