Class: Metriks::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/metriks/registry.rb

Overview

Public: A collection of metrics

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Public: Initializes a new Registry.



17
18
19
20
# File 'lib/metriks/registry.rb', line 17

def initialize
  @mutex = Mutex.new
  @metrics = {}
end

Class Method Details

.defaultObject

Public: The default registry for the process.

Returns the default Registry for the process.



12
13
14
# File 'lib/metriks/registry.rb', line 12

def self.default
  @default ||= new
end

Instance Method Details

#add(name, metric) ⇒ Object

Public: Add a new metric.

name - The String name of the metric to add metric - The metric instance to add

Examples

registry.add('method.calls', Metriks::Counter.new)

Returns nothing. Raises RuntimeError if the metric name is already defined



149
150
151
152
153
154
155
156
157
# File 'lib/metriks/registry.rb', line 149

def add(name, metric)
  @mutex.synchronize do
    if @metrics[name]
      raise "Metric '#{name}' already defined"
    else
      @metrics[name] = metric
    end
  end
end

#clearObject

Public: Clear all of the metrics in the Registry. This ensures all metrics that have been added are stopped.

Returns nothing.



26
27
28
29
30
31
32
33
34
# File 'lib/metriks/registry.rb', line 26

def clear
  @mutex.synchronize do
    @metrics.each do |key, metric|
      metric.stop if metric.respond_to?(:stop)
    end

    @metrics = {}
  end
end

#counter(name) ⇒ Object

Public: Fetch or create a new counter metric. Counters are one of the simplest metrics whose only operations are increment and decrement.

name - The String name of the metric to define or fetch

Examples

registry.counter('method.calls')

Returns the Metricks::Counter identified by the name.



71
72
73
# File 'lib/metriks/registry.rb', line 71

def counter(name)
  add_or_get(name, Metriks::Counter)
end

#each(&block) ⇒ Object

Public: Iterate over all of the counters.

Examples

registry.each do |name, metric|
  puts name
end

Returns nothing.



53
54
55
56
57
58
59
# File 'lib/metriks/registry.rb', line 53

def each(&block)
  metrics = @mutex.synchronize do
    @metrics.dup
  end

  metrics.each(&block)
end

#get(name) ⇒ Object

Public: Fetch an existing metric.

name - The String name of the metric to fetch

Examples

registry.get('rack.utilization')

Returns the metric or nil.



132
133
134
135
136
# File 'lib/metriks/registry.rb', line 132

def get(name)
  @mutex.synchronize do
    @metrics[name]
  end
end

#meter(name) ⇒ Object

Public: Fetch or create a new meter metric. Meters are a counter that tracks throughput along with the count.

name - The String name of the metric to define or fetch

Examples

registry.meter('resque.calls')

Returns the Metricks::Meter identified by the name.



85
86
87
# File 'lib/metriks/registry.rb', line 85

def meter(name)
  add_or_get(name, Metriks::Meter)
end

#stopObject

Public: Clear all of the metrics in the Registry. This has the same effect as calling #clear.

Returns nothing.



40
41
42
# File 'lib/metriks/registry.rb', line 40

def stop
  clear
end

#timer(name) ⇒ Object

Public: Fetch or create a new timer metric. Timers provide the means to time the execution of a method including statistics on the number of invocations, average length of time, throughput.

name - The String name of the metric to define or fetch

Examples

registry.timer('resque.worker')

Returns the Metricks::Timer identified by the name.



100
101
102
# File 'lib/metriks/registry.rb', line 100

def timer(name)
  add_or_get(name, Metriks::Timer)
end

#utilization_timer(name) ⇒ Object

Public: Fetch or create a new utilization timer metric.

Utilization timers are a specialized version of a timer that calculate the percentage of wall-clock time (between 0 and 1) that was spent in the method. This metric is most valuable in a single-threaded environment where a processes is waiting on an external resource like a message queue or HTTP server.

name - The String name of the metric to define or fetch

Examples

registry.utilization_timer('rack.utilization')

Returns the Metricks::UtilizationTimer identified by the name.



119
120
121
# File 'lib/metriks/registry.rb', line 119

def utilization_timer(name)
  add_or_get(name, Metriks::UtilizationTimer)
end