Class: Hitimes::ValueMetric

Inherits:
Metric
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hitimes/value_metric.rb

Overview

A ValueMetric holds the data from measuring a single value over a period of time. In most cases this may be a single measurement at a single point in time.

A good example of a ValueMetric is measuring the number of items in a queue.

A ValueMetric contains a Stats object, therefore ValueMetric has count, max, mean, min, stddev, sum, sumsq methods that delegate to that Stats object for convenience.

Instance Attribute Summary (collapse)

Attributes inherited from Metric

#additional_data, #name, #sampling_delta

Instance Method Summary (collapse)

Methods inherited from Metric

#sampling_start_time, #sampling_stop_time, #utc_microseconds

Constructor Details

- (ValueMetric) initialize(name, additional_data = {})

:call-seq:

ValueMetric.new( 'my_metric' ) -> ValueMetric
ValueMetric.new( 'my_metric', 'foo' => 'bar', 'this' => 42 ) -> ValueMetric

Create a new ValueMetric giving it a name and additional data. additional_data may be anything that follows the to_hash protocol.



32
33
34
35
# File 'lib/hitimes/value_metric.rb', line 32

def initialize( name, additional_data = {} )
  super( name, additional_data )
  @stats = Stats.new
end

Instance Attribute Details

- (Object) stats (readonly)

holds all the statistics



22
23
24
# File 'lib/hitimes/value_metric.rb', line 22

def stats
  @stats
end

Instance Method Details

- (Object) measure(value)

:call-seq:

metric.measure( value ) -> Float

Give the value as the measurement to the metric. The value is returned



43
44
45
46
47
48
49
50
51
# File 'lib/hitimes/value_metric.rb', line 43

def measure( value )
  @sampling_start_time ||= self.utc_microseconds()
  @sampling_start_interval ||= Interval.now

  @stats.update( value )
  
  # update the length of time we have been sampling
  @sampling_delta = @sampling_start_interval.duration_so_far
end

- (Object) to_hash

:call-seq:

metric.to_hash -> Hash

Convert the metric to a hash



59
60
61
62
63
64
65
# File 'lib/hitimes/value_metric.rb', line 59

def to_hash
  h = super
  (Stats::STATS - %w[ rate ]).each do |s|
    h[s] = self.send( s ) 
  end
  return h
end