lib/rackamole/mole.rb in rackamole-0.3.5 vs lib/rackamole/mole.rb in rackamole-0.3.6

- old
+ new

@@ -1,12 +1,12 @@ require 'hitimes' require 'json' require 'mongo' require 'yaml' -# TODO - remove default app and db name # TODO - add recording for response +# TODO - need plugable archictecture for alerts and stores module Rack class Mole # Initialize The Mole rack component. It is recommended that you specify at a minimum a user_key to track # interactions on a per user basis. If you wish to use the mole for the same application in different @@ -43,22 +43,22 @@ # :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} } # == # - # :mole_excludes :: Exclude some of the paramaters that the mole would normaly include. The list of - # customizable paramaters is: software, ip, browser type, url, path, status, headers and body. + # :mole_excludes :: Exclude some of the parameters that the mole would normally include. The list of + # customizable parameters is: software, ip, browser type, url, path, status, headers and body. # This options takes an array of symbols. Defaults to [:body]. # :perf_excludes :: Specifies a set of paths that will be excluded when a performance issue is detected. # This is useful when certain requests are known to be slow. When a match is found an # alert won't be issued but the context will still be moled. # == # Don't send an alert when perf is found on /blee/fred. Don't alert on /duh unless the request took longer than 10 secs # :perf_excludes => [ {:context => '/blee/fred}, {:context => /^\/duh.*/, :threshold => 10 } ] # == # :excluded_paths:: Exclude paths that you do not wish to mole by specifying an array of regular expresssions. - # :param_excludes:: Exempt certain sensitive request parameters from being logged to the mole. Specify an array of keys as + # :param_excludes:: Exempt certain sensitive request parameters from being logged to the mole. Specify an array of keys # as symbols ie [:password, :card_number]. # :session_excludes:: Exempt session params from being logged by the mole. Specify an array of keys as symbols # ie [:fred, :blee] to exclude session[:fred] and session[:blee] from being stored. # :twitter :: Set this option to have the mole twitt certain alerts. You must configure your twitter auth # via the :username and :password keys and :alert_on with an array of mole types you @@ -74,10 +74,16 @@ # See Pony docs for custom options for your emails # == # :email => { :from => 'fred@acme.com', :to => ['blee@acme.com', 'doh@acme.com'], :alert_on => [Rackamole.perf, Rackamole.fault] } # == # + # :growl :: You can set up rackamole to send growl notifications when certain features + # are encounters in the same way email and twitt alerts works. The :to options + # describes a collection of hash values with the ip and optional password to the recipients. + # == + # :growl => { :to => [{ :ip => '1.1.1.1' }], :alert_on => [Rackamole.perf, Rackamole.fault] } + # == def initialize( app, opts={} ) @app = app init_options( opts ) validate_options @logger = Rackamole::Logger.new( :logger_name => 'RACKAMOLE', :log_level => options[:log_level] ) @@ -116,11 +122,11 @@ def init_options( opts ) if opts[:config_file] && (env = opts[:environment] || "development") raise "Unable to find rackamole config file #{opts[:config_file]}" unless ::File.exists?( opts[:config_file] ) begin opts = YAML.load( ERB.new( IO.read( opts[:config_file] ) ).result( binding ) )[env] - opts[:environment] = env + opts[:environment] = env rescue => boom raise "Unable to parse Rackamole config file #{boom}" end end @options = default_options.merge( opts ) @@ -149,10 +155,11 @@ end # Barf early if something is wrong in the configuration configured?( :twitter, [:username, :password, :alert_on], true ) configured?( :email , [:from, :to, :alert_on], true ) + configured?( :growl , [:to, :alert_on], true ) end # Send moled info to store and potentially send out alerts... def mole_feature( env, elapsed, status, headers, body ) env['mole.stash'] = stash @@ -169,9 +176,15 @@ unless duplicated?( env, attrs ) # send email alert ? if alertable?( :email, attrs[:type] ) logger.debug ">>> Sending out email on mole type #{attrs[:type]} from #{options[:email][:from]} to #{options[:email][:to].join( ", ")}" Rackamole::Alert::Emole.deliver_alert( logger, options, attrs ) + end + + # send growl alert ? + if alertable?( :growl, attrs[:type] ) + logger.debug ">>> Sending out growl on mole type #{attrs[:type]} to @#{options[:growl][:to].inspect}" + Rackamole::Alert::Growl.deliver_alert( logger, options, attrs ) end # send twitter alert ? if alertable?( :twitter, attrs[:type] ) logger.debug ">>> Sending out twitt on mole type #{attrs[:type]} on @#{options[:twitter][:username]}" \ No newline at end of file