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.



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

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.



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

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



182
183
184
185
186
187
188
189
190
# File 'lib/metriks/registry.rb', line 182

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.



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

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 Metriks::Counter identified by the name.



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

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.



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

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

  metrics.each(&block)
end

#gauge(name, callable = nil, &block) ⇒ Object

Public: Fetch or create a new gauge metric.

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

Examples

registry.gauge('disk_space.used') { 1 }

Returns the Metriks::Gauge identified by the name.



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

def gauge(name, callable = nil, &block)
  add_or_get(name, Metriks::Gauge) do
    Metriks::Gauge.new(callable, &block)
  end
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.



165
166
167
168
169
# File 'lib/metriks/registry.rb', line 165

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

#histogram(name) ⇒ Object

Public: Fetch or create a new histogram metric. Histograms record values and expose statistics about the distribution of the data like median and 95th percentile.

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

Examples

registry.histogram('backlog.wait')

Returns the Metriks::Histogram identified by the name.



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

def histogram(name)
  add_or_get(name, Metriks::Histogram) do
    Metriks::Histogram.new_exponentially_decaying
  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 Metriks::Meter identified by the name.



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

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.



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

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 Metriks::Timer identified by the name.



116
117
118
# File 'lib/metriks/registry.rb', line 116

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 Metriks::UtilizationTimer identified by the name.



135
136
137
# File 'lib/metriks/registry.rb', line 135

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