aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib
diff options
context:
space:
mode:
authorVishal Telangre <the@vishaltelangre.com>2019-05-05 11:25:26 +0530
committerVishal Telangre <the@vishaltelangre.com>2019-05-10 20:16:17 +0530
commit93b652affbed41bce568a0fac4ca7b56aa3b691f (patch)
treeade38e50711efc2d8476a56e99179bb13107408d /activesupport/lib
parent669b52d2965f50f1f3f99f1c54677a328191c69d (diff)
downloadrails-93b652affbed41bce568a0fac4ca7b56aa3b691f.tar.gz
rails-93b652affbed41bce568a0fac4ca7b56aa3b691f.tar.bz2
rails-93b652affbed41bce568a0fac4ca7b56aa3b691f.zip
Introduce 'ActiveSupport::Notifications::Fanout::Subscribers::MonotonicTimed' and 'ActiveSupport::Notifications::monotonic_subscribe'
Also, change the signature of ‘ActiveSupport::Notifications::Fanout#subscribe’ to accept optional ‘monotonic’ boolean argument. Then initialize either a ‘Timed’ or ‘MonotonicTimed’ subscriber based on the value of ‘monotonic’ parameter. Introduce ‘ActiveSupport::Notifications::monotonic_subscribe’ method Also, provision ‘ActiveSupport::Notifications::subscribed’ to optionally accept ‘monotonic’ boolean argument. Update documentation for ActiveSupport::Notifications Add tests Update guides documentation under the 'Active Support Instrumentation' chapter Incorporate feedback: use optional keyword argument to specify optional 'monotonic' option to 'subscribed' method Fix a typo
Diffstat (limited to 'activesupport/lib')
-rw-r--r--activesupport/lib/active_support/notifications.rb35
-rw-r--r--activesupport/lib/active_support/notifications/fanout.rb25
2 files changed, 53 insertions, 7 deletions
diff --git a/activesupport/lib/active_support/notifications.rb b/activesupport/lib/active_support/notifications.rb
index d9e93b530c..555c0ad1d3 100644
--- a/activesupport/lib/active_support/notifications.rb
+++ b/activesupport/lib/active_support/notifications.rb
@@ -38,6 +38,19 @@ module ActiveSupport
# payload # => Hash, the payload
# end
#
+ # Here, the +start+ and +finish+ values represent wall-clock time. If you are
+ # concerned about accuracy, you can register a monotonic subscriber.
+ #
+ # ActiveSupport::Notifications.monotonic_subscribe('render') do |name, start, finish, id, payload|
+ # name # => String, name of the event (such as 'render' from above)
+ # start # => Monotonic time, when the instrumented block started execution
+ # finish # => Monotonic time, when the instrumented block ended execution
+ # id # => String, unique ID for the instrumenter that fired the event
+ # payload # => Hash, the payload
+ # end
+ #
+ # The +start+ and +finish+ values above represent monotonic time.
+ #
# For instance, let's store all "render" events in an array:
#
# events = []
@@ -135,6 +148,16 @@ module ActiveSupport
# during the execution of the block. The callback is unsubscribed automatically
# after that.
#
+ # To record +started+ and +finished+ values with monotonic time,
+ # specify the optional <tt>:monotonic</tt> option to the
+ # <tt>subscribed</tt> method. The <tt>:monotonic</tt> option is set
+ # to +false+ by default.
+ #
+ # callback = lambda {|name, started, finished, unique_id, payload| ... }
+ # ActiveSupport::Notifications.subscribed(callback, "sql.active_record", monotonic: true) do
+ # ...
+ # end
+ #
# === Manual Unsubscription
#
# The +subscribe+ method returns a subscriber object:
@@ -209,11 +232,17 @@ module ActiveSupport
# @event = event
# end
def subscribe(*args, &block)
- notifier.subscribe(*args, &block)
+ pattern, callback = *args
+ notifier.subscribe(pattern, callback, false, &block)
+ end
+
+ def monotonic_subscribe(*args, &block)
+ pattern, callback = *args
+ notifier.subscribe(pattern, callback, true, &block)
end
- def subscribed(callback, *args, &block)
- subscriber = subscribe(*args, &callback)
+ def subscribed(callback, pattern, monotonic: false, &block)
+ subscriber = notifier.subscribe(pattern, callback, monotonic)
yield
ensure
unsubscribe(subscriber)
diff --git a/activesupport/lib/active_support/notifications/fanout.rb b/activesupport/lib/active_support/notifications/fanout.rb
index c506b35b1e..c37bec4ee5 100644
--- a/activesupport/lib/active_support/notifications/fanout.rb
+++ b/activesupport/lib/active_support/notifications/fanout.rb
@@ -20,8 +20,8 @@ module ActiveSupport
super
end
- def subscribe(pattern = nil, callable = nil, &block)
- subscriber = Subscribers.new(pattern, callable || block)
+ def subscribe(pattern = nil, callable = nil, monotonic = false, &block)
+ subscriber = Subscribers.new(monotonic, pattern, callable || block)
synchronize do
if String === pattern
@string_subscribers[pattern] << subscriber
@@ -84,8 +84,8 @@ module ActiveSupport
end
module Subscribers # :nodoc:
- def self.new(pattern, listener)
- subscriber_class = Timed
+ def self.new(monotonic, pattern, listener)
+ subscriber_class = monotonic ? MonotonicTimed : Timed
if listener.respond_to?(:start) && listener.respond_to?(:finish)
subscriber_class = Evented
@@ -190,6 +190,23 @@ module ActiveSupport
end
end
+ class MonotonicTimed < Evented # :nodoc:
+ def publish(name, *args)
+ @delegate.call name, *args
+ end
+
+ def start(name, id, payload)
+ timestack = Thread.current[:_timestack_monotonic] ||= []
+ timestack.push Concurrent.monotonic_time
+ end
+
+ def finish(name, id, payload)
+ timestack = Thread.current[:_timestack_monotonic]
+ started = timestack.pop
+ @delegate.call(name, started, Concurrent.monotonic_time, id, payload)
+ end
+ end
+
class EventObject < Evented
def start(name, id, payload)
stack = Thread.current[:_event_stack] ||= []