Skip to content

Commit 3e2f9a6

Browse files
authored
Merge pull request rails#43390 from jhawthorn/remove_notification_event_children
Remove child event tracking from ActiveSupport::Subscriber
2 parents 139ef8a + 9c58a54 commit 3e2f9a6

File tree

7 files changed

+71
-85
lines changed

7 files changed

+71
-85
lines changed

actionview/lib/action_view/log_subscriber.rb

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,58 @@ def render_collection(event)
5050
end
5151
end
5252

53-
def start(name, id, payload)
54-
log_rendering_start(payload, name)
53+
module Utils # :nodoc:
54+
def logger
55+
ActionView::Base.logger
56+
end
5557

56-
super
57-
end
58+
private
59+
def from_rails_root(string)
60+
string = string.sub(rails_root, "")
61+
string.sub!(VIEWS_PATTERN, "")
62+
string
63+
end
5864

59-
def logger
60-
ActionView::Base.logger
65+
def rails_root # :doc:
66+
@root ||= "#{Rails.root}/"
67+
end
6168
end
6269

63-
private
64-
EMPTY = ""
65-
def from_rails_root(string) # :doc:
66-
string = string.sub(rails_root, EMPTY)
67-
string.sub!(VIEWS_PATTERN, EMPTY)
68-
string
70+
include Utils
71+
72+
class Start # :nodoc:
73+
include Utils
74+
75+
def start(name, id, payload)
76+
return unless logger
77+
logger.debug do
78+
qualifier =
79+
if name == "render_template.action_view"
80+
""
81+
elsif name == "render_layout.action_view"
82+
"layout "
83+
end
84+
85+
return unless qualifier
86+
87+
message = +" Rendering #{qualifier}#{from_rails_root(payload[:identifier])}"
88+
message << " within #{from_rails_root(payload[:layout])}" if payload[:layout]
89+
message
90+
end
91+
end
92+
93+
def finish(name, id, payload)
94+
end
6995
end
7096

71-
def rails_root # :doc:
72-
@root ||= "#{Rails.root}/"
97+
def self.attach_to(*)
98+
ActiveSupport::Notifications.subscribe("render_template.action_view", ActionView::LogSubscriber::Start.new)
99+
ActiveSupport::Notifications.subscribe("render_layout.action_view", ActionView::LogSubscriber::Start.new)
100+
101+
super
73102
end
74103

104+
private
75105
def render_count(payload) # :doc:
76106
if payload[:cache_hits]
77107
"[#{payload[:cache_hits]} / #{payload[:count]} cache hits]"
@@ -88,23 +118,6 @@ def cache_message(payload) # :doc:
88118
"[cache miss]"
89119
end
90120
end
91-
92-
def log_rendering_start(payload, name)
93-
debug do
94-
qualifier =
95-
if name == "render_template.action_view"
96-
""
97-
elsif name == "render_layout.action_view"
98-
"layout "
99-
end
100-
101-
return unless qualifier
102-
103-
message = +" Rendering #{qualifier}#{from_rails_root(payload[:identifier])}"
104-
message << " within #{from_rails_root(payload[:layout])}" if payload[:layout]
105-
message
106-
end
107-
end
108121
end
109122
end
110123

activesupport/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Deprecate `Notification::Event`'s `#children` and `#parent_of?`
2+
13
* Change default serialization format of `MessageEncryptor` from `Marshal` to `JSON` for Rails 7.1.
24

35
Existing apps are provided with an upgrade path to migrate to `JSON` as described in `guides/source/upgrading_ruby_on_rails.md`

activesupport/lib/active_support/log_subscriber.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,10 @@ def logger
107107
LogSubscriber.logger
108108
end
109109

110-
def start(name, id, payload)
111-
super if logger
112-
end
113-
114-
def finish(name, id, payload)
110+
def call(event)
115111
super if logger
116112
rescue => e
117-
log_exception(name, e)
113+
log_exception(event.name, e)
118114
end
119115

120116
def publish_event(event)

activesupport/lib/active_support/notifications/instrumenter.rb

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def unique_id
5656
end
5757

5858
class Event
59-
attr_reader :name, :time, :end, :transaction_id, :children
59+
attr_reader :name, :time, :end, :transaction_id
6060
attr_accessor :payload
6161

6262
def initialize(name, start, ending, transaction_id, payload)
@@ -65,7 +65,6 @@ def initialize(name, start, ending, transaction_id, payload)
6565
@time = start ? start.to_f * 1_000.0 : start
6666
@transaction_id = transaction_id
6767
@end = ending ? ending.to_f * 1_000.0 : ending
68-
@children = []
6968
@cpu_time_start = 0.0
7069
@cpu_time_finish = 0.0
7170
@allocation_count_start = 0
@@ -117,6 +116,23 @@ def allocations
117116
@allocation_count_finish - @allocation_count_start
118117
end
119118

119+
def children # :nodoc:
120+
ActiveSupport::Deprecation.warn <<~EOM
121+
ActiveSupport::Notifications::Event#children is deprecated and will
122+
be removed in Rails 7.2.
123+
EOM
124+
[]
125+
end
126+
127+
def parent_of?(event) # :nodoc:
128+
ActiveSupport::Deprecation.warn <<~EOM
129+
ActiveSupport::Notifications::Event#parent_of? is deprecated and will
130+
be removed in Rails 7.2.
131+
EOM
132+
start = (time - event.time) * 1000
133+
start <= 0 && (start + duration >= event.duration)
134+
end
135+
120136
# Returns the difference in milliseconds between when the execution of the
121137
# event started and when it ended.
122138
#
@@ -133,14 +149,6 @@ def duration
133149
self.end - time
134150
end
135151

136-
def <<(event)
137-
@children << event
138-
end
139-
140-
def parent_of?(event)
141-
@children.include? event
142-
end
143-
144152
private
145153
def now
146154
Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)

activesupport/lib/active_support/subscriber.rb

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -126,38 +126,18 @@ def fetch_public_methods(subscriber, inherit_all)
126126
attr_reader :patterns # :nodoc:
127127

128128
def initialize
129-
@queue_key = [self.class.name, object_id].join "-"
130129
@patterns = {}
131130
super
132131
end
133132

134-
def start(name, id, payload)
135-
event = ActiveSupport::Notifications::Event.new(name, nil, nil, id, payload)
136-
event.start!
137-
parent = event_stack.last
138-
parent << event if parent
139-
140-
event_stack.push event
141-
end
142-
143-
def finish(name, id, payload)
144-
event = event_stack.pop
145-
event.finish!
146-
event.payload.merge!(payload)
147-
148-
method = name.split(".").first
133+
def call(event)
134+
method = event.name.split(".").first
149135
send(method, event)
150136
end
151137

152138
def publish_event(event) # :nodoc:
153139
method = event.name.split(".").first
154140
send(method, event)
155141
end
156-
157-
private
158-
def event_stack
159-
registry = ActiveSupport::IsolatedExecutionState[:active_support_subscriber_queue_registry] ||= {}
160-
registry[@queue_key] ||= []
161-
end
162142
end
163143
end

activesupport/test/log_subscriber_test.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ def test_event_is_an_active_support_notifications_event
7777

7878
def test_event_attributes
7979
ActiveSupport::LogSubscriber.attach_to :my_log_subscriber, @log_subscriber
80-
instrument "some_event.my_log_subscriber"
80+
instrument "some_event.my_log_subscriber" do
81+
[] # Make an allocation
82+
end
8183
wait
8284
event = @log_subscriber.event
8385
if defined?(JRUBY_VERSION)

activesupport/test/notifications_test.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -462,21 +462,6 @@ def test_events_consumes_information_given_as_payload
462462
assert_equal Hash[payload: :bar], event.payload
463463
end
464464

465-
def test_event_is_parent_based_on_children
466-
time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
467-
468-
parent = event(:foo, Process.clock_gettime(Process::CLOCK_MONOTONIC), Process.clock_gettime(Process::CLOCK_MONOTONIC) + 100, random_id, {})
469-
child = event(:foo, time, time + 10, random_id, {})
470-
not_child = event(:foo, time, time + 100, random_id, {})
471-
472-
parent.children << child
473-
474-
assert parent.parent_of?(child)
475-
assert_not child.parent_of?(parent)
476-
assert_not parent.parent_of?(not_child)
477-
assert_not not_child.parent_of?(parent)
478-
end
479-
480465
def test_subscribe_raises_error_on_non_supported_arguments
481466
notifier = ActiveSupport::Notifications::Fanout.new
482467

0 commit comments

Comments
 (0)