Class: Hitimes::Metric
- Inherits:
-
Object
- Object
- Hitimes::Metric
- Defined in:
- lib/hitimes/metric.rb
Overview
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.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#additional_data ⇒ Object
readonly
An additional hash of data to associate with the metric.
-
#name ⇒ Object
readonly
The ‘name’ to associate with the metric.
-
#sampling_delta ⇒ Object
readonly
the number of seconds as a float since the sampling_start_time.
Instance Method Summary collapse
-
#initialize(name, additional_data = {}) ⇒ Metric
constructor
:call-seq: Metric.new( ‘my_metric’ ) -> Metric Metric.new( ‘my_metric’, ‘foo’ => ‘bar’, ‘this’ => 42 ) -> Metric.
-
#sampling_start_time ⇒ Object
:call-seq: metric.sampling_start_time -> Float or nil.
-
#sampling_stop_time ⇒ Object
:call-seq: metric.sampling_stop_time -> Float or nil.
-
#to_hash ⇒ Object
:call-seq: metric.to_hash -> Hash metric.to_hash.
-
#utc_microseconds ⇒ Object
:call-seq: metric.utc_microseconds -> Float.
Constructor Details
#initialize(name, additional_data = {}) ⇒ Metric
:call-seq:
Metric.new( 'my_metric' ) -> Metric
Metric.new( 'my_metric', 'foo' => 'bar', 'this' => 42 ) -> Metric
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.
44 45 46 47 48 49 50 51 |
# 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 |
Instance Attribute Details
#additional_data ⇒ Object (readonly)
An additional hash of data to associate with the metric
29 30 31 |
# File 'lib/hitimes/metric.rb', line 29 def additional_data @additional_data end |
#name ⇒ Object (readonly)
The ‘name’ to associate with the metric
32 33 34 |
# File 'lib/hitimes/metric.rb', line 32 def name @name end |
#sampling_delta ⇒ Object (readonly)
the number of seconds as a float since the sampling_start_time
26 27 28 |
# File 'lib/hitimes/metric.rb', line 26 def sampling_delta @sampling_delta end |
Instance Method Details
#sampling_start_time ⇒ Object
:call-seq:
metric.sampling_start_time -> Float or nil
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.
62 63 64 65 66 |
# File 'lib/hitimes/metric.rb', line 62 def sampling_start_time return unless @sampling_start_interval @sampling_start_time ||= utc_microseconds end |
#sampling_stop_time ⇒ Object
:call-seq:
metric.sampling_stop_time -> Float or nil
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.
84 85 86 87 88 |
# File 'lib/hitimes/metric.rb', line 84 def sampling_stop_time return unless @sampling_delta.positive? (sampling_start_time + (@sampling_delta * 1_000_000)) end |
#to_hash ⇒ Object
:call-seq:
metric.to_hash -> Hash
metric.to_hash
Convert the metric to a Hash.
97 98 99 100 101 102 |
# File 'lib/hitimes/metric.rb', line 97 def to_hash { "sampling_start_time" => sampling_start_time, "sampling_stop_time" => sampling_stop_time, "additional_data" => additional_data, "name" => name, } end |
#utc_microseconds ⇒ Object
:call-seq:
metric.utc_microseconds -> Float
The current time in microseconds from the UNIX Epoch in the UTC
110 111 112 |
# File 'lib/hitimes/metric.rb', line 110 def utc_microseconds Time.now.gmtime.to_f * 1_000_000 end |