Skip to content

Commit

Permalink
Count the number of requests to mongo, as well as the total request r…
Browse files Browse the repository at this point in the history
…untime
  • Loading branch information
tomafro committed Feb 21, 2011
1 parent cdf414d commit 772eb00
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/mongo/rails/instrumentation/controller_runtime.rb
Expand Up @@ -9,9 +9,9 @@ module ControllerRuntime
attr_internal :mongo_runtime

def cleanup_view_runtime
mongo_rt_before_render = LogSubscriber.reset_runtime
mongo_rt_before_render = LogSubscriber.reset
runtime = super
mongo_rt_after_render = LogSubscriber.reset_runtime
mongo_rt_after_render = LogSubscriber.reset
self.mongo_runtime = mongo_rt_before_render + mongo_rt_after_render
runtime - mongo_rt_after_render
end
Expand Down
21 changes: 17 additions & 4 deletions lib/mongo/rails/instrumentation/log_subscriber.rb
@@ -1,20 +1,33 @@
require 'mongo/rails/instrumentation'

class Mongo::Rails::Instrumentation::LogSubscriber < ActiveSupport::LogSubscriber
PREFIX = self.class.name.freeze
COUNT_KEY = PREFIX + "#count".freeze
RUNTIME_KEY = PREFIX + "#runtime".freeze

def self.runtime=(value)
Thread.current["mongo_mongo_runtime"] = value
Thread.current[RUNTIME_KEY] = value
end

def self.runtime
Thread.current["mongo_mongo_runtime"] ||= 0
Thread.current[RUNTIME_KEY] ||= 0
end

def self.count=(value)
Thread.current[COUNT_KEY] = value
end

def self.count
Thread.current[COUNT_KEY] ||= 0
end

def self.reset_runtime
rt, self.runtime = runtime, 0
def self.reset
rt, self.runtime, self.count = runtime, 0, 0
rt
end

def mongo(event)
self.class.runtime += event.duration
self.class.count += 1
end
end
2 changes: 1 addition & 1 deletion lib/mongo/rails/instrumentation/version.rb
@@ -1,7 +1,7 @@
module Mongo
module Rails
module Instrumentation
VERSION = "0.1.0"
VERSION = "0.2.0"
end
end
end
4 changes: 2 additions & 2 deletions mongo-rails-instrumentation.gemspec
Expand Up @@ -9,8 +9,8 @@ Gem::Specification.new do |s|
s.authors = ["Tom Ward"]
s.email = ["tom@popdog.net"]
s.homepage = "http://tomafro.net"
s.summary = %q{Records time spent in mongo and adds to request logs}
s.description = %q{Records time spent in mongo and adds to request logs}
s.summary = %q{Record time spent in Mongo and add to request logs}
s.description = %q{Record time spent in Mongo and add to request logs}

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down

0 comments on commit 772eb00

Please sign in to comment.