aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-07-25 11:11:23 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2010-07-25 11:11:23 -0700
commitb7e0408ca922cf51228818edbfdcd5c63e3cb84e (patch)
tree0313ba10f8f42720b03b51fabc992d84c425d8cb /activesupport/lib/active_support
parentd02f2d2fb213584ce52a231063c948def856f657 (diff)
downloadrails-b7e0408ca922cf51228818edbfdcd5c63e3cb84e.tar.gz
rails-b7e0408ca922cf51228818edbfdcd5c63e3cb84e.tar.bz2
rails-b7e0408ca922cf51228818edbfdcd5c63e3cb84e.zip
use a hash to collect optional statistics about the instrumentation
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r--activesupport/lib/active_support/notifications.rb16
-rw-r--r--activesupport/lib/active_support/notifications/instrumenter.rb11
2 files changed, 20 insertions, 7 deletions
diff --git a/activesupport/lib/active_support/notifications.rb b/activesupport/lib/active_support/notifications.rb
index 886d7183eb..d65431a943 100644
--- a/activesupport/lib/active_support/notifications.rb
+++ b/activesupport/lib/active_support/notifications.rb
@@ -47,11 +47,21 @@ module ActiveSupport
attr_writer :notifier
delegate :publish, :unsubscribe, :to => :notifier
- def instrument(name, payload = {})
+ def instrument(name, payload = {}, info = nil)
if @instrumenters[name]
- instrumenter.instrument(name, payload) { yield payload if block_given? }
+ instrumenter.instrument(name, payload, info) {
+ yield payload if block_given?
+ }
else
- yield payload if block_given?
+ value = nil
+ if block_given?
+ if info
+ info[:elapsed] = Benchmark.ms { value = yield payload }
+ else
+ value = yield payload
+ end
+ end
+ value
end
end
diff --git a/activesupport/lib/active_support/notifications/instrumenter.rb b/activesupport/lib/active_support/notifications/instrumenter.rb
index 441fefb491..4fc446fb2b 100644
--- a/activesupport/lib/active_support/notifications/instrumenter.rb
+++ b/activesupport/lib/active_support/notifications/instrumenter.rb
@@ -14,16 +14,19 @@ module ActiveSupport
# Instrument the given block by measuring the time taken to execute it
# and publish it. Notice that events get sent even if an error occurs
# in the passed-in block
- def instrument(name, payload={})
- started = Time.now
-
+ def instrument(name, payload={}, info = nil)
begin
+ started = Time.now
yield
rescue Exception => e
payload[:exception] = [e.class.name, e.message]
raise e
ensure
- @notifier.publish(name, started, Time.now, @id, payload)
+ finished = Time.now
+ if info
+ info[:elapsed] = 1000.0 * (finished.to_f - started.to_f)
+ end
+ @notifier.publish(name, started, finished, @id, payload)
end
end