Class: ActiveSupport::Notifications::Event

Inherits:
Object
  • Object
show all
Defined in:
activesupport/lib/active_support/notifications/instrumenter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, start, ending, transaction_id, payload) ⇒ Event

Returns a new instance of Event.


62
63
64
65
66
67
68
69
70
71
72
# File 'activesupport/lib/active_support/notifications/instrumenter.rb', line 62

def initialize(name, start, ending, transaction_id, payload)
  @name           = name
  @payload        = payload.dup
  @time           = start ? start.to_f * 1_000.0 : start
  @transaction_id = transaction_id
  @end            = ending ? ending.to_f * 1_000.0 : ending
  @cpu_time_start = 0.0
  @cpu_time_finish = 0.0
  @allocation_count_start = 0
  @allocation_count_finish = 0
end

Instance Attribute Details

#endObject (readonly)

Returns the value of attribute end.


59
60
61
# File 'activesupport/lib/active_support/notifications/instrumenter.rb', line 59

def end
  @end
end

#nameObject (readonly)

Returns the value of attribute name.


59
60
61
# File 'activesupport/lib/active_support/notifications/instrumenter.rb', line 59

def name
  @name
end

#payloadObject

Returns the value of attribute payload.


60
61
62
# File 'activesupport/lib/active_support/notifications/instrumenter.rb', line 60

def payload
  @payload
end

#timeObject (readonly)

Returns the value of attribute time.


59
60
61
# File 'activesupport/lib/active_support/notifications/instrumenter.rb', line 59

def time
  @time
end

#transaction_idObject (readonly)

Returns the value of attribute transaction_id.


59
60
61
# File 'activesupport/lib/active_support/notifications/instrumenter.rb', line 59

def transaction_id
  @transaction_id
end

Instance Method Details

#allocationsObject

Returns the number of allocations made since the call to start! and the call to finish!


115
116
117
# File 'activesupport/lib/active_support/notifications/instrumenter.rb', line 115

def allocations
  @allocation_count_finish - @allocation_count_start
end

#childrenObject

:nodoc:


119
120
121
122
123
124
125
# File 'activesupport/lib/active_support/notifications/instrumenter.rb', line 119

def children # :nodoc:
  ActiveSupport::Deprecation.warn <<~EOM
    ActiveSupport::Notifications::Event#children is deprecated and will
    be removed in Rails 7.2.
  EOM
  []
end

#cpu_timeObject

Returns the CPU time (in milliseconds) passed since the call to start! and the call to finish!


103
104
105
# File 'activesupport/lib/active_support/notifications/instrumenter.rb', line 103

def cpu_time
  @cpu_time_finish - @cpu_time_start
end

#durationObject

Returns the difference in milliseconds between when the execution of the event started and when it ended.

ActiveSupport::Notifications.subscribe('wait') do |*args|
  @event = ActiveSupport::Notifications::Event.new(*args)
end

ActiveSupport::Notifications.instrument('wait') do
  sleep 1
end

@event.duration # => 1000.138

148
149
150
# File 'activesupport/lib/active_support/notifications/instrumenter.rb', line 148

def duration
  self.end - time
end

#finish!Object

Record information at the time this event finishes


95
96
97
98
99
# File 'activesupport/lib/active_support/notifications/instrumenter.rb', line 95

def finish!
  @cpu_time_finish = now_cpu
  @end = now
  @allocation_count_finish = now_allocations
end

#idle_timeObject

Returns the idle time time (in milliseconds) passed since the call to start! and the call to finish!


109
110
111
# File 'activesupport/lib/active_support/notifications/instrumenter.rb', line 109

def idle_time
  duration - cpu_time
end

#parent_of?(event) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)

127
128
129
130
131
132
133
134
# File 'activesupport/lib/active_support/notifications/instrumenter.rb', line 127

def parent_of?(event) # :nodoc:
  ActiveSupport::Deprecation.warn <<~EOM
    ActiveSupport::Notifications::Event#parent_of? is deprecated and will
    be removed in Rails 7.2.
  EOM
  start = (time - event.time) * 1000
  start <= 0 && (start + duration >= event.duration)
end

#recordObject


74
75
76
77
78
79
80
81
82
83
84
85
# File 'activesupport/lib/active_support/notifications/instrumenter.rb', line 74

def record
  start!
  begin
    yield payload if block_given?
  rescue Exception => e
    payload[:exception] = [e.class.name, e.message]
    payload[:exception_object] = e
    raise e
  ensure
    finish!
  end
end

#start!Object

Record information at the time this event starts


88
89
90
91
92
# File 'activesupport/lib/active_support/notifications/instrumenter.rb', line 88

def start!
  @time = now
  @cpu_time_start = now_cpu
  @allocation_count_start = now_allocations
end