lib/rackamole/mole.rb in rackamole-0.0.4 vs lib/rackamole/mole.rb in rackamole-0.0.6

- old
+ new

@@ -1,8 +1,8 @@ require 'hitimes' -require 'mongo/util/ordered_hash' require 'json' +require 'mongo' module Rack class Mole # Initialize The Mole with the possible options @@ -79,37 +79,37 @@ return info unless mole_request?( request ) session = env['rack.session'] route = get_route( request ) - ip, browser = identify( env ) - user_id = nil - user_name = nil + ip, user_agent = identify( env ) + user_id = nil + user_name = nil # BOZO !! This could be slow if have to query db to get user name... # Preferred store username in session and give at key if session and @user_key - if @user_key.instance_of? Symbol - user_name = session[@user_key] - elsif @user_key.instance_of? Hash + if @user_key.instance_of? Hash user_id = session[ @user_key[:session_key] ] if @user_key[:extractor] user_name = @user_key[:extractor].call( user_id ) end + else + user_name = session[@user_key] end end info[:app_name] = @app_name - info[:environment] = @environment if @environment + info[:environment] = @environment || "Unknown" info[:user_id] = user_id if user_id info[:user_name] = user_name || "Unknown" info[:ip] = ip - info[:browser] = browser + info[:browser] = id_browser( user_agent ) info[:host] = env['SERVER_NAME'] info[:software] = env['SERVER_SOFTWARE'] info[:request_time] = elapsed if elapsed - info[:perf_issue] = (elapsed and elapsed > @perf_threshold) + info[:performance] = (elapsed and elapsed > @perf_threshold) info[:url] = request.url info[:method] = env['REQUEST_METHOD'] info[:path] = request.path info[:route_info] = route if route @@ -136,10 +136,22 @@ rescue => boom $stderr.puts "!! MOLE RECORDING CRAPPED OUT !! -- #{boom}" boom.backtrace.each { |l| $stderr.puts l } end + # Attempts to detect browser type from agent info. + # BOZO !! Probably more efficient way to do this... + def browser_types() @browsers ||= [ 'Firefox', 'Safari', 'MSIE 8.0', 'MSIE 7.0', 'MSIE 6.0', 'Opera', 'Chrome' ] end + + def id_browser( user_agent ) + return "N/A" if !user_agent or user_agent.empty? + browser_types.each do |b| + return b if user_agent.match( /.*?#{b.gsub(/\./,'\.')}.*?/ ) + end + "N/A" + end + # Trim stack trace def trim_stack( boom ) boom.backtrace[0...4] end @@ -153,25 +165,31 @@ @moleable end # Fetch route info if any... def get_route( request ) - return nil unless defined?( RAILS_ENV ) - ::ActionController::Routing::Routes.recognize_path( request.path, {:method => request.request_method.downcase.to_sym } ) + return nil unless defined?( RAILS_ENV ) + + # Check for invalid route exception... + begin + return ::ActionController::Routing::Routes.recognize_path( request.path, {:method => request.request_method.downcase.to_sym } ) + rescue + return nil + end end # Dump env to stdout - def dump( env, level=0 ) - env.keys.sort{ |a,b| a.to_s <=> b.to_s }.each do |k| - value = env[k] - if value.respond_to?(:each_pair) - puts "%s %-#{40-level}s" % [' '*level,k] - dump( env[k], level+1 ) - elsif value.instance_of?(::ActionController::Request) or value.instance_of?(::ActionController::Response) - puts "%s %-#{40-level}s %s" % [ ' '*level, k, value.class ] - else - puts "%s %-#{40-level}s %s" % [ ' '*level, k, value.inspect ] - end - end - end + # def dump( env, level=0 ) + # env.keys.sort{ |a,b| a.to_s <=> b.to_s }.each do |k| + # value = env[k] + # if value.respond_to?(:each_pair) + # puts "%s %-#{40-level}s" % [' '*level,k] + # dump( env[k], level+1 ) + # elsif value.instance_of?(::ActionController::Request) or value.instance_of?(::ActionController::Response) + # puts "%s %-#{40-level}s %s" % [ ' '*level, k, value.class ] + # else + # puts "%s %-#{40-level}s %s" % [ ' '*level, k, value.inspect ] + # end + # end + # end end end \ No newline at end of file