class Hitimes::Metric
Metric hold the common meta information for all derived metric classes
All metrics hold the meta information of:
* The name of the metric * The time of day the first measurement is taken * The time of day the last measurement is taken * additional data
Each derived class is assumed to set the #sampling_start_time and #sampling_stop_time appropriately.
Metric itself should generally not be used. Only use the derived classes.
Attributes
An additional hash of data to associate with the metric
The 'name' to associate with the metric
the number of seconds as a float since the #sampling_start_time
Public Class Methods
Create a new ValueMetric giving it a name and additional data.
additional_data
may be anything that follows the
to_hash
protocol. name
may be anything that
follows the to_s
protocol.
# File lib/hitimes/metric.rb, line 44 def initialize( name, additional_data = {} ) @sampling_start_time = nil @sampling_start_interval = nil @sampling_delta = 0 @name = name.to_s @additional_data = additional_data.to_hash end
Public Instance Methods
The time at which the first sample was taken. This is the number of microseconds since UNIX epoch UTC as a Float
If the metric has not started measuring then the start time is nil.
# File lib/hitimes/metric.rb, line 62 def sampling_start_time if @sampling_start_interval then @sampling_start_time ||= self.utc_microseconds() else nil end end
The time at which the last sample was taken This is the number of microseconds since UNIX epoch UTC as a Float
If the metric has not completely measured at least one thing then stop time is nil.
Because accessing the actual 'time of day' is an expesive operation, we only get the time of day at the beginning of the first measurement and we keep track of the offset from that point in @sampling_delta.
When #sampling_stop_time is called, the actual time of day is caculated.
# File lib/hitimes/metric.rb, line 86 def sampling_stop_time if @sampling_delta > 0 then (self.sampling_start_time + (@sampling_delta * 1_000_000)) else nil end end
Convert the metric to a Hash.
# File lib/hitimes/metric.rb, line 101 def to_hash { 'sampling_start_time' => self.sampling_start_time, 'sampling_stop_time' => self.sampling_stop_time, 'additional_data' => self.additional_data, 'name' => self.name } end
The current time in microseconds from the UNIX Epoch in the UTC
# File lib/hitimes/metric.rb, line 114 def utc_microseconds Time.now.gmtime.to_f * 1_000_000 end