Skip to content

Commit c6d86a5

Browse files
committed
make events not use date and time to determine parent_of. fixes rails#5932
1 parent 7fe0f27 commit c6d86a5

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

activesupport/lib/active_support/log_subscriber.rb

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def attach_to(namespace, log_subscriber=new, notifier=ActiveSupport::Notificatio
6161
@@flushable_loggers = nil
6262

6363
log_subscriber.public_methods(false).each do |event|
64-
next if :call == event
64+
next if %w{ start finish }.include?(event.to_s)
6565

6666
notifier.subscribe("#{event}.#{namespace}", log_subscriber)
6767
end
@@ -86,14 +86,35 @@ def flush_all!
8686
end
8787
end
8888

89-
def call(message, *args)
89+
def initialize
90+
@event_stack = Hash.new { |h,id|
91+
h[id] = Hash.new { |ids,name| ids[name] = [] }
92+
}
93+
super
94+
end
95+
96+
def start(name, id, payload)
97+
return unless logger
98+
99+
e = ActiveSupport::Notifications::Event.new(name, Time.now, nil, id, payload)
100+
parent = @event_stack[id][name].last
101+
parent << e if parent
102+
103+
@event_stack[id][name].push e
104+
end
105+
106+
def finish(name, id, payload)
90107
return unless logger
91108

92-
method = message.split('.').first
109+
finished = Time.now
110+
event = @event_stack[id][name].pop
111+
event.end = finished
112+
113+
method = name.split('.').first
93114
begin
94-
send(method, ActiveSupport::Notifications::Event.new(message, *args))
95-
rescue => e
96-
logger.error "Could not log #{message.inspect} event. #{e.class}: #{e.message} #{e.backtrace}"
115+
send(method, event)
116+
rescue Exception => e
117+
logger.error "Could not log #{name.inspect} event. #{e.class}: #{e.message} #{e.backtrace}"
97118
end
98119
end
99120

activesupport/lib/active_support/notifications/instrumenter.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,28 @@ def unique_id
3131
end
3232

3333
class Event
34-
attr_reader :name, :time, :end, :transaction_id, :payload, :duration
34+
attr_reader :name, :time, :transaction_id, :payload, :children
35+
attr_accessor :end
3536

3637
def initialize(name, start, ending, transaction_id, payload)
3738
@name = name
3839
@payload = payload.dup
3940
@time = start
4041
@transaction_id = transaction_id
4142
@end = ending
42-
@duration = 1000.0 * (@end - @time)
43+
@children = []
44+
end
45+
46+
def duration
47+
1000.0 * (self.end - time)
48+
end
49+
50+
def <<(event)
51+
@children << event
4352
end
4453

4554
def parent_of?(event)
46-
start = (time - event.time) * 1000
47-
start <= 0 && (start + duration >= event.duration)
55+
@children.include? event
4856
end
4957
end
5058
end

activesupport/test/notifications_test.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,15 @@ def test_events_consumes_information_given_as_payload
221221
assert_equal Hash[:payload => :bar], event.payload
222222
end
223223

224-
def test_event_is_parent_based_on_time_frame
224+
def test_event_is_parent_based_on_children
225225
time = Time.utc(2009, 01, 01, 0, 0, 1)
226226

227227
parent = event(:foo, Time.utc(2009), Time.utc(2009) + 100, random_id, {})
228228
child = event(:foo, time, time + 10, random_id, {})
229229
not_child = event(:foo, time, time + 100, random_id, {})
230230

231+
parent.children << child
232+
231233
assert parent.parent_of?(child)
232234
assert !child.parent_of?(parent)
233235
assert !parent.parent_of?(not_child)

0 commit comments

Comments
 (0)