Permalink
Browse files

Added mongo mapper support

  • Loading branch information...
1 parent d11bc54 commit 6f9d0e38790abb307f4f2b955c70b2e76e7dd4d8 @derailed committed Nov 19, 2009
View
@@ -22,8 +22,11 @@ PROJ.version = Rackamole::VERSION
PROJ.spec.opts << '--color'
PROJ.ruby_opts = %w[-W0]
PROJ.readme = 'README.rdoc'
+PROJ.rcov.opts = ["--sort", "coverage", "-T", '-x mongo']
# Dependencies
depend_on "logging" , ">= 1.2.2"
depend_on "hitimes" , ">= 1.0.3"
-depend_on "mongodb-mongo", ">= 0.14.1"
+depend_on "mongo" , ">= 0.17.1"
+depend_on "mongo_mapper" , ">= 0.6.1"
+depend_on "darkfish-rdoc", ">= 1.1.5"
View
@@ -1,6 +1,6 @@
require 'hitimes'
-require 'mongo/util/ordered_hash'
require 'json'
+require 'mongo'
module Rack
class Mole
View
@@ -0,0 +1,4 @@
+require 'mongo_mapper'
+require 'activesupport'
+
+Rackamole.require_all_libs_relative_to(__FILE__)
@@ -0,0 +1,13 @@
+module Rackamole::Mongo
+ class Feature
+ include MongoMapper::Document
+
+ key :controller, String, :required => false
+ key :action , String, :required => false
+ key :context , String, :required => false
+ key :app_name , String, :required => true
+ timestamps!
+
+ many :logs, :class_name => 'Rackamole::Mongo::Log'
+ end
+end
View
@@ -0,0 +1,22 @@
+module Rackamole::Mongo
+ class Log
+ include MongoMapper::Document
+
+ key :feature_id , String, :required => true
+ key :perf_issue , Boolean, :required => true, :default => false
+ key :type , String , :required => true, :default => 'Feature'
+ key :ip , String , :required => true
+ key :browser , String , :required => true
+ key :method , String , :required => true
+ key :path , String , :required => true
+ key :url , String , :required => true
+
+ key :user_id , Integer, :required => false, :default => -1
+ key :user_name , String , :required => false, :default => "Unknown"
+ key :session , Hash , :required => false
+ key :router_info, Hash , :required => false
+ timestamps!
+
+ belongs_to :feature, :class_name => 'Rackamole::Mongo::Feature'
+ end
+end
View
@@ -0,0 +1 @@
+Rackamole.require_all_libs_relative_to(__FILE__)
@@ -1,121 +0,0 @@
-require 'mongo'
-
-include Mongo
-
-# TODO !! Need to deal with auth
-module Rackamole
- module Store
- # Mongo adapter. Stores mole info in a mongo database.
- # Two collections are available namely features and logs. Logs references
- # the features collection.
- class Mongo
-
- attr_reader :connection
-
- def initialize( options={} )
- opts = default_options.merge( options )
- @connection = Connection.new( opts[:host], opts[:port] ).db( opts[:database] )
- end
-
- # clear out db content
- def reset!
- features.clear
- logs.clear
- end
-
- # Dump mole info to logger
- def mole( args )
- return if args.empty?
-
- feature = find_or_create_feature( args )
- log_feature( feature, args )
- rescue => mole_boom
- $stderr.puts "MOLE STORE CRAPPED OUT -- #{mole_boom}"
- $stderr.puts mole_boom.backtrace.join( "\n " )
- end
-
- # Convenience to access mole features cltn
- def features
- @features ||= @connection['features']
- end
-
- # Convenience to access mole log cltn
- def logs
- @logs ||= @connection['logs']
- end
-
- # =======================================================================
- private
-
- # Set up mongo default options ie localhost host, default mongo port and
- # the database being mole_mdb
- def default_options
- {
- :host => 'localhost',
- :port => 27017,
- :database => 'mole_mdb'
- }
- end
-
- # retrieves a feature if exists or create a new one otherwise
- def find_or_create_feature( args )
- if args[:route_info]
- controller = args[:route_info][:controller]
- action = args[:route_info][:action]
- end
-
- feature = find_feature( args[:app_name], args[:path], controller, action )
-
- # Got one
- return feature if feature
-
- # If not create a new feature
- row = { :app_name => args[:app_name] }
- if controller and action
- row['controller'] = controller
- row['action'] = action
- else
- row['context'] = args[:path]
- end
- row['created_at'] = Time.now
- row['updated_at'] = Time.now
- features.insert( row )
- end
-
- # Attempt to find a mole feature
- def find_feature( app_name, path, controller, action )
- conds = { 'app_name' => app_name }
-
- # For rails use controller/action
- if controller and action
- conds['controller'] = controller
- conds['action'] = action
- # otherwise use path...
- else
- conds['context'] = path
- end
- features.find_one( conds )
- end
-
- # Insert a new feature in the db
- def log_feature( feature, args )
- type = 'Feature'
- type = 'Exception' if args[:stack]
- type = 'Performance' if args[:performance]
-
- row = {
- :type => type,
- :feature => ::DBRef.new( 'features', feature.instance_of?( ObjectID ) ? feature : feature['_id'] ),
- :created_at => Time.now,
- :updated_at => Time.now
- }
-
- skip_cols = [:app_name]
- args.each do |k,v|
- row[k] = v unless skip_cols.include?( k )
- end
- logs.insert( row )
- end
- end
- end
-end
@@ -0,0 +1,77 @@
+require 'mongo'
+
+include Mongo
+
+# TODO !! Need to deal with auth
+module Rackamole
+ module Store
+ # Mongo adapter. Stores mole info in a mongo database.
+ class MongoDb
+
+ def initialize( options={} )
+ opts = default_options.merge( options )
+ MongoMapper.connection = Connection.new( opts[:host], opts[:port], :logger => opts[:logger] )
+ MongoMapper.database = opts[:database]
+ end
+
+ # clear out db content ( used in testing... )
+ def reset!
+ Rackamole::Mongo::Feature.delete_all
+ Rackamole::Mongo::Log.delete_all
+ end
+
+ # Dump mole info to logger
+ def mole( arguments )
+ return if arguments.empty?
+ args = arguments.clone
+
+ app_name = args.delete( :app_name )
+
+ if args[:route_info]
+ controller = args[:route_info][:controller]
+ action = args[:route_info][:action]
+ feature = Rackamole::Mongo::Feature.find_or_create_by_app_name_and_controller_and_action( app_name, controller, action )
+ else
+ context = args[:path]
+ feature = Rackamole::Mongo::Feature.find_or_create_by_app_name_and_context( app_name, context )
+ end
+ log_feature( feature, args )
+ rescue => mole_boom
+ $stderr.puts "MOLE STORE CRAPPED OUT -- #{mole_boom}"
+ $stderr.puts mole_boom.backtrace.join( "\n " )
+ end
+
+ # =======================================================================
+ private
+
+ # Set up mongo default options ie localhost host, default mongo port and
+ # the database being mole_mdb
+ def default_options
+ {
+ :host => 'localhost',
+ :port => 27017,
+ :database => 'mole_mdb'
+ }
+ end
+
+ # Insert a new feature in the db
+ def log_feature( feature, args )
+ type = 'Feature'
+ type = 'Exception' if args[:stack]
+ type = 'Performance' if args[:performance]
+
+ attrs = {
+ :type => type,
+ :feature => feature,
+ :created_at => Time.now,
+ :updated_at => Time.now
+ }
+
+ args.each do |k,v|
+ attrs[k] = v
+ end
+ Rackamole::Mongo::Log.create!( attrs )
+ end
+ end
+ end
+end
@@ -1,16 +1,16 @@
require File.join(File.dirname(__FILE__), %w[.. spec_helper])
-require 'actionpack'
+# require 'actionpack'
describe Rack::Mole do
include Rack::Test::Methods
before :each do
@response = [ 200, {"Content-Type" => "text/plain"}, ["success"] ]
+ @test_env = { 'rack.session' => { :user_id => 100 }, 'HTTP_X_FORWARDED_FOR' => '1.1.1.1', 'HTTP_USER_AGENT' => "IBrowse" }
end
class TestStore
- attr_accessor :mole_result
-
+ attr_accessor :mole_result
def mole( args )
@mole_result = args
end
@@ -24,11 +24,35 @@ def app( opts={} )
run lambda { |env| response }
end
end
-
+
+ def error_app( opts={} )
+ @app ||= Rack::Builder.new do
+ use Rack::Lint
+ use Rack::Mole, opts
+ run lambda { |env| raise "Oh Snap!" }
+ end
+ end
+
+ it "should mole a framwework exception correctly" do
+ @test_store = TestStore.new
+ @test_env = { 'rack.session' => { :user_id => 100 }, 'HTTP_X_FORWARDED_FOR' => '1.1.1.1', 'HTTP_USER_AGENT' => "IBrowse" }
+ error_app(
+ :app_name => "Test App",
+ :environment => :test,
+ :perf_threshold => 0.1,
+ :user_key => { :session_key => :user_id, :extractor => lambda{ |k| "Test user #{k}"} },
+ :store => @test_store )
+
+ begin
+ get "/", nil, @test_env
+ rescue
+ @test_store.mole_result[:stack].should have(4).items
+ end
+ end
+
describe 'moling a request' do
before :each do
@test_store = TestStore.new
- @test_env = { 'rack.session' => { :user_id => 100 }, 'HTTP_X_FORWARDED_FOR' => '1.1.1.1', 'HTTP_USER_AGENT' => "IBrowse" }
app(
:app_name => "Test App",
:environment => :test,
@@ -62,7 +86,7 @@ def app( opts={} )
@test_store.mole_result[:stack].should have(4).items
end
end
-
+
it "should capture request parameters correctly" do
get "/", { :blee => 'duh' }, @test_env
@test_store.mole_result[:params].should == { :blee => "duh".to_json }
@@ -1,5 +1,4 @@
require File.join(File.dirname(__FILE__), %w[.. .. spec_helper])
-require 'mongo/util/ordered_hash'
describe Rackamole::Store::Log do
describe "#mole" do
Oops, something went wrong.

0 comments on commit 6f9d0e3

Please sign in to comment.