Class: Metrics::Instrumenter

Inherits:
Object
  • Object
show all
Defined in:
lib/metrics/instrumenter.rb

Overview

Public: Responsible for sampling a measurement of something.

metric - The name of the metric to measure (e.g. rack.request.time) options - Hash of options (default: {}):

unit: Unit of measurement [ms, MB, GB, ...] (optional)
type: Method type of the measurement [measure, sample, count]
      (optional, default: measure)

Returns a new Metrics::Instrumenter.

Constant Summary collapse

TIME_UNITS =
'ms'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metric, *args, &block) ⇒ Instrumenter

Returns a new instance of Instrumenter.



22
23
24
25
26
27
# File 'lib/metrics/instrumenter.rb', line 22

def initialize(metric, *args, &block)
  @metric  = metric
  @options = extract_options!(args)
  @block   = block
  @value   = args.first if args.length > 0
end

Instance Attribute Details

#metricObject (readonly)

Returns the value of attribute metric.



14
15
16
# File 'lib/metrics/instrumenter.rb', line 14

def metric
  @metric
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/metrics/instrumenter.rb', line 14

def options
  @options
end

Class Method Details

.instrument(*args, &block) ⇒ Object



16
17
18
19
20
# File 'lib/metrics/instrumenter.rb', line 16

def self.instrument(*args, &block)
  instrument = new(*args, &block)
  instrument.value
  instrument
end

Instance Method Details

#resultObject



53
54
55
56
57
# File 'lib/metrics/instrumenter.rb', line 53

def result
  return nil unless block
  return @result if defined?(@result)
  @result = block.call
end

#sourceObject



45
46
47
# File 'lib/metrics/instrumenter.rb', line 45

def source
  options[:source]
end

#tagsObject



49
50
51
# File 'lib/metrics/instrumenter.rb', line 49

def tags
  options[:tags] || {}
end

#typeObject



41
42
43
# File 'lib/metrics/instrumenter.rb', line 41

def type
  options[:type] || 'measure'
end

#unitsObject



37
38
39
# File 'lib/metrics/instrumenter.rb', line 37

def units
  timing? ? TIME_UNITS : options[:units]
end

#valueObject

Public: Runs the instrumenter.

Returns the run time if a block was supplied. Returns the value if the



33
34
35
# File 'lib/metrics/instrumenter.rb', line 33

def value
  timing? ? time : @value
end