Skip to content

Commit 93b652a

Browse files
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
1 parent 669b52d commit 93b652a

4 files changed

Lines changed: 131 additions & 9 deletions

File tree

activesupport/lib/active_support/notifications.rb

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ module ActiveSupport
3838
# payload # => Hash, the payload
3939
# end
4040
#
41+
# Here, the +start+ and +finish+ values represent wall-clock time. If you are
42+
# concerned about accuracy, you can register a monotonic subscriber.
43+
#
44+
# ActiveSupport::Notifications.monotonic_subscribe('render') do |name, start, finish, id, payload|
45+
# name # => String, name of the event (such as 'render' from above)
46+
# start # => Monotonic time, when the instrumented block started execution
47+
# finish # => Monotonic time, when the instrumented block ended execution
48+
# id # => String, unique ID for the instrumenter that fired the event
49+
# payload # => Hash, the payload
50+
# end
51+
#
52+
# The +start+ and +finish+ values above represent monotonic time.
53+
#
4154
# For instance, let's store all "render" events in an array:
4255
#
4356
# events = []
@@ -135,6 +148,16 @@ module ActiveSupport
135148
# during the execution of the block. The callback is unsubscribed automatically
136149
# after that.
137150
#
151+
# To record +started+ and +finished+ values with monotonic time,
152+
# specify the optional <tt>:monotonic</tt> option to the
153+
# <tt>subscribed</tt> method. The <tt>:monotonic</tt> option is set
154+
# to +false+ by default.
155+
#
156+
# callback = lambda {|name, started, finished, unique_id, payload| ... }
157+
# ActiveSupport::Notifications.subscribed(callback, "sql.active_record", monotonic: true) do
158+
# ...
159+
# end
160+
#
138161
# === Manual Unsubscription
139162
#
140163
# The +subscribe+ method returns a subscriber object:
@@ -209,11 +232,17 @@ def instrument(name, payload = {})
209232
# @event = event
210233
# end
211234
def subscribe(*args, &block)
212-
notifier.subscribe(*args, &block)
235+
pattern, callback = *args
236+
notifier.subscribe(pattern, callback, false, &block)
237+
end
238+
239+
def monotonic_subscribe(*args, &block)
240+
pattern, callback = *args
241+
notifier.subscribe(pattern, callback, true, &block)
213242
end
214243

215-
def subscribed(callback, *args, &block)
216-
subscriber = subscribe(*args, &callback)
244+
def subscribed(callback, pattern, monotonic: false, &block)
245+
subscriber = notifier.subscribe(pattern, callback, monotonic)
217246
yield
218247
ensure
219248
unsubscribe(subscriber)

activesupport/lib/active_support/notifications/fanout.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def initialize
2020
super
2121
end
2222

23-
def subscribe(pattern = nil, callable = nil, &block)
24-
subscriber = Subscribers.new(pattern, callable || block)
23+
def subscribe(pattern = nil, callable = nil, monotonic = false, &block)
24+
subscriber = Subscribers.new(monotonic, pattern, callable || block)
2525
synchronize do
2626
if String === pattern
2727
@string_subscribers[pattern] << subscriber
@@ -84,8 +84,8 @@ def wait
8484
end
8585

8686
module Subscribers # :nodoc:
87-
def self.new(pattern, listener)
88-
subscriber_class = Timed
87+
def self.new(monotonic, pattern, listener)
88+
subscriber_class = monotonic ? MonotonicTimed : Timed
8989

9090
if listener.respond_to?(:start) && listener.respond_to?(:finish)
9191
subscriber_class = Evented
@@ -190,6 +190,23 @@ def finish(name, id, payload)
190190
end
191191
end
192192

193+
class MonotonicTimed < Evented # :nodoc:
194+
def publish(name, *args)
195+
@delegate.call name, *args
196+
end
197+
198+
def start(name, id, payload)
199+
timestack = Thread.current[:_timestack_monotonic] ||= []
200+
timestack.push Concurrent.monotonic_time
201+
end
202+
203+
def finish(name, id, payload)
204+
timestack = Thread.current[:_timestack_monotonic]
205+
started = timestack.pop
206+
@delegate.call(name, started, Concurrent.monotonic_time, id, payload)
207+
end
208+
end
209+
193210
class EventObject < Evented
194211
def start(name, id, payload)
195212
stack = Thread.current[:_event_stack] ||= []

activesupport/test/notifications_test.rb

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,38 @@ def test_subscribe_via_top_level_api
6262
end
6363
end
6464

65+
class TimedAndMonotonicTimedSubscriberTest < TestCase
66+
def test_subscribe
67+
event_name = "foo"
68+
class_of_started = nil
69+
class_of_finished = nil
70+
71+
ActiveSupport::Notifications.subscribe(event_name) do |name, started, finished, unique_id, data|
72+
class_of_started = started.class
73+
class_of_finished = finished.class
74+
end
75+
76+
ActiveSupport::Notifications.instrument(event_name)
77+
78+
assert_equal [Time, Time], [class_of_started, class_of_finished]
79+
end
80+
81+
def test_monotonic_subscribe
82+
event_name = "foo"
83+
class_of_started = nil
84+
class_of_finished = nil
85+
86+
ActiveSupport::Notifications.monotonic_subscribe(event_name) do |name, started, finished, unique_id, data|
87+
class_of_started = started.class
88+
class_of_finished = finished.class
89+
end
90+
91+
ActiveSupport::Notifications.instrument(event_name)
92+
93+
assert_equal [Float, Float], [class_of_started, class_of_finished]
94+
end
95+
end
96+
6597
class SubscribedTest < TestCase
6698
def test_subscribed
6799
name = "foo"
@@ -95,6 +127,42 @@ def test_subscribing_to_instrumentation_while_inside_it
95127
ensure
96128
ActiveSupport::Notifications.notifier = old_notifier
97129
end
130+
131+
def test_timed_subscribed
132+
event_name = "foo"
133+
class_of_started = nil
134+
class_of_finished = nil
135+
callback = lambda do |name, started, finished, unique_id, data|
136+
class_of_started = started.class
137+
class_of_finished = finished.class
138+
end
139+
140+
ActiveSupport::Notifications.subscribed(callback, event_name) do
141+
ActiveSupport::Notifications.instrument(event_name)
142+
end
143+
144+
ActiveSupport::Notifications.instrument(event_name)
145+
146+
assert_equal [Time, Time], [class_of_started, class_of_finished]
147+
end
148+
149+
def test_monotonic_timed_subscribed
150+
event_name = "foo"
151+
class_of_started = nil
152+
class_of_finished = nil
153+
callback = lambda do |name, started, finished, unique_id, data|
154+
class_of_started = started.class
155+
class_of_finished = finished.class
156+
end
157+
158+
ActiveSupport::Notifications.subscribed(callback, event_name, monotonic: true) do
159+
ActiveSupport::Notifications.instrument(event_name)
160+
end
161+
162+
ActiveSupport::Notifications.instrument(event_name)
163+
164+
assert_equal [Float, Float], [class_of_started, class_of_finished]
165+
end
98166
end
99167

100168
class UnsubscribeTest < TestCase
@@ -317,7 +385,6 @@ def test_event_cpu_time_does_not_raise_error_when_start_or_finished_not_called
317385
assert_equal 0, event.cpu_time
318386
end
319387

320-
321388
def test_events_consumes_information_given_as_payload
322389
event = event(:foo, Concurrent.monotonic_time, Concurrent.monotonic_time + 1, random_id, payload: :bar)
323390
assert_equal Hash[payload: :bar], event.payload

guides/source/active_support_instrumentation.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,16 @@ The block receives the following arguments:
643643
```ruby
644644
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, started, finished, unique_id, data|
645645
# your own custom stuff
646-
Rails.logger.info "#{name} Received!"
646+
Rails.logger.info "#{name} Received! (started: #{started}, finished: #{finished})" # process_action.action_controller Received (started: 2019-05-05 13:43:57 -0800, finished: 2019-05-05 13:43:58 -0800)
647+
end
648+
```
649+
650+
If you are concerned about the accuracy of `started` and `finished` to compute a precise elapsed time then use `ActiveSupport::Notifications.monotonic_subscribe`. The given block would receive the same arguments as above but the `started` and `finished` will have values with an accurate monotonic time instead of wall-clock time.
651+
652+
```ruby
653+
ActiveSupport::Notifications.monotonic_subscribe "process_action.action_controller" do |name, started, finished, unique_id, data|
654+
# your own custom stuff
655+
Rails.logger.info "#{name} Received! (started: #{started}, finished: #{finished})" # process_action.action_controller Received (started: 1560978.425334, finished: 1560979.429234)
647656
end
648657
```
649658

0 commit comments

Comments
 (0)