Class: Camayoc::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/camayoc/stats.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent = nil) ⇒ Stats

Constructor

  • name

    Name of stat

  • parent

    Optional parent stat (default: nil)



9
10
11
12
13
# File 'lib/camayoc/stats.rb', line 9

def initialize(name,parent=nil)
  self.name = name
  self.parent = parent
  self.handlers = []
end

Instance Attribute Details

#handlersObject

Returns the value of attribute handlers.



4
5
6
# File 'lib/camayoc/stats.rb', line 4

def handlers
  @handlers
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/camayoc/stats.rb', line 4

def name
  @name
end

#parentObject

Returns the value of attribute parent.



4
5
6
# File 'lib/camayoc/stats.rb', line 4

def parent
  @parent
end

Instance Method Details

#<<(handler) ⇒ Object

Quick alias for adding a handler



32
33
34
# File 'lib/camayoc/stats.rb', line 32

def <<(handler)
  self.add(handler)
end

#[](descendant_name) ⇒ Object



15
16
17
# File 'lib/camayoc/stats.rb', line 15

def [](descendant_name)
  Camayoc[Camayoc.join(name,descendant_name)]
end

#add(handler, filter_opts = {}) ⇒ Object

Add handler with filter options

  • handler

    Handler instance

  • filter_opts

    Options for a new Handlers::Filter instance (optional)



23
24
25
26
27
28
29
# File 'lib/camayoc/stats.rb', line 23

def add(handler,filter_opts={})
  if !filter_opts.empty?
    handler = Handlers::Filter.new(handler,filter_opts)
  end
  self.handlers << handler
  self
end

#benchmark(stat, options = {}) ⇒ Object

Executes a block, stores the timing information in the specified stat and returns the value of the block. Use this to wrap existing code with timing information without worrying about return values.



62
63
64
65
66
67
68
# File 'lib/camayoc/stats.rb', line 62

def benchmark(stat,options={})
  result = nil
  realtime(stat,options) do
    result = yield
  end
  result
end

#count(stat, value, options = {}) ⇒ Object

Count and incr/decr convenience methods



47
48
49
# File 'lib/camayoc/stats.rb', line 47

def count(stat,value,options={})
  propagate(:count,stat,value,options)
end

#decrement(stat, options = {}) ⇒ Object



55
56
57
# File 'lib/camayoc/stats.rb', line 55

def decrement(stat,options={})
  count(stat,-1,options)
end

#event(stat, value, options = {}) ⇒ Object

Generic event



42
43
44
# File 'lib/camayoc/stats.rb', line 42

def event(stat,value,options={})
  propagate(:generic,stat,value,options)
end

#increment(stat, options = {}) ⇒ Object



51
52
53
# File 'lib/camayoc/stats.rb', line 51

def increment(stat,options={})
  count(stat,1,options)
end

#realtime(stat, options = {}) ⇒ Object

Executes a block, stores the timing information in the specified stat and returns the time **in seconds** that the execution took. This is basically a drop-in replacement for calls to Benchmark.realtime



73
74
75
76
77
78
79
80
# File 'lib/camayoc/stats.rb', line 73

def realtime(stat,options={})
  start_time = Time.now
  yield
  duration = (Time.now.to_f - start_time.to_f)
  # Convert to ms for timing call
  timing(stat,(duration*1000).round,options)
  duration
end

#timing(stat, value, options = {}) ⇒ Object

Timing stat



37
38
39
# File 'lib/camayoc/stats.rb', line 37

def timing(stat,value,options={})
  propagate(:timing,stat,value,options)
end