Module: Simple::Metrics::Timer

Included in:
Simple::Metrics
Defined in:
lib/simple/metrics/timer.rb

Instance Method Summary collapse

Instance Method Details

#timer(name, options = {}) ⇒ Object

Create creates a new timer, registers it with the metrics registry, yields the timer to the block and stops it when the block returns.

‘{}`

Parameters:

  • name (String)

    The name of the timer

  • options (Hash) (defaults to: {})

    A hash containing keys/value pairs to override defaults. Defaults to



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/simple/metrics/timer.rb', line 10

def timer(name, options={})
  klass_name = options[:name] || self.class.name
  duration_unit = options[:duration_unit] || Simple::Metrics::DEFAULT_DURATION_UNIT
  time_unit = options[:time_unit] || Simple::Metrics::DEFAULT_RATE_UNIT
  metric_name = new_metric_name(sanitize_classname(klass_name), name, 'timer')
  new_timer = metrics_registry.newTimer(metric_name, duration_unit, time_unit)
  captured_timer = new_timer.time
  begin
    yield new_timer
  ensure
    captured_timer.stop
  end
end