Class: Cabin::Metrics::Timer

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

Defined Under Namespace

Classes: TimerContext

Instance Method Summary collapse

Constructor Details

#initializeTimer

Returns a new instance of Timer.



11
12
13
14
# File 'lib/cabin/metrics/timer.rb', line 11

def initialize
  @invocations = 0
  @lock = Mutex.new
end

Instance Method Details

#countObject



46
47
48
# File 'lib/cabin/metrics/timer.rb', line 46

def count
  return @lock.synchronize { @invocations }
end

#record(duration) ⇒ Object



37
38
39
40
41
42
# File 'lib/cabin/metrics/timer.rb', line 37

def record(duration)
  @lock.synchronize do
    @invocations += 1
    # TODO(sissel): histogram the duration
  end
end

#time(&block) ⇒ Object



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

def time(&block)
  return time_block(&block) if block_given?

  # Return an object we can .stop
  return TimerContext.new(method(:record))
end

#valueObject



52
53
54
# File 'lib/cabin/metrics/timer.rb', line 52

def value
  return count
end