Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

start / finish events are sent by the instrumenter

  • Loading branch information...
commit f08f8750a512f741acb004d0cebe210c5f949f28 1 parent 60736fe
@tenderlove tenderlove authored
View
42 activesupport/lib/active_support/notifications/fanout.rb
@@ -20,6 +20,14 @@ def unsubscribe(subscriber)
@listeners_for.clear
end
+ def start(name, id, payload)
+ listeners_for(name).each { |s| s.start(name, id, payload) }
+ end
+
+ def finish(name, id, payload)
+ listeners_for(name).each { |s| s.finish(name, id, payload) }
+ end
+
def publish(name, *args)
listeners_for(name).each { |s| s.publish(name, *args) }
end
@@ -39,7 +47,7 @@ def wait
module Subscribers # :nodoc:
def self.new(pattern, block)
if pattern
- Subscriber.new pattern, block
+ TimedSubscriber.new pattern, block
else
AllMessages.new pattern, block
end
@@ -51,8 +59,12 @@ def initialize(pattern, delegate)
@delegate = delegate
end
- def publish(message, *args)
- @delegate.call(message, *args)
+ def start(name, id, payload)
+ raise NotImplementedError
+ end
+
+ def finish(name, id, payload)
+ raise NotImplementedError
end
def subscribed_to?(name)
@@ -65,7 +77,29 @@ def matches?(subscriber_or_name)
end
end
- class AllMessages < Subscriber # :nodoc:
+ class TimedSubscriber < Subscriber
+ def initialize(pattern, delegate)
+ @timestack = Hash.new { |h,id|
+ h[id] = Hash.new { |ids,name| ids[name] = [] }
+ }
+ super
+ end
+
+ def publish(name, *args)
+ @delegate.call name, *args
+ end
+
+ def start(name, id, payload)
+ @timestack[id][name].push Time.now
+ end
+
+ def finish(name, id, payload)
+ started = @timestack[id][name].pop
+ @delegate.call(name, started, Time.now, id, payload)
+ end
+ end
+
+ class AllMessages < TimedSubscriber # :nodoc:
def subscribed_to?(name)
true
end
View
6 activesupport/lib/active_support/notifications/instrumenter.rb
@@ -1,5 +1,6 @@
module ActiveSupport
module Notifications
+ # Instrumentors are stored in a thread local.
class Instrumenter
attr_reader :id
@@ -12,15 +13,14 @@ def initialize(notifier)
# 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
-
+ @notifier.start(name, @id, payload)
begin
yield
rescue Exception => e
payload[:exception] = [e.class.name, e.message]
raise e
ensure
- @notifier.publish(name, started, Time.now, @id, payload)
+ @notifier.finish(name, @id, payload)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.