Class: Twingly::Metrics::Registry
- Inherits:
-
Object
- Object
- Twingly::Metrics::Registry
- Defined in:
- lib/twingly/metrics/registry.rb
Overview
Public: A collection of metrics
Class Method Summary collapse
-
.default ⇒ Object
Public: The default registry for the process.
Instance Method Summary collapse
-
#add(name, metric) ⇒ Object
Public: Add a new metric.
-
#clear ⇒ Object
Public: Clear all of the metrics in the Registry.
-
#counter(name) ⇒ Object
Public: Fetch or create a new counter metric.
-
#each(&block) ⇒ Object
Public: Iterate over all of the counters.
-
#gauge(name, callable = nil, &block) ⇒ Object
Public: Fetch or create a new gauge metric.
-
#get(name) ⇒ Object
Public: Fetch an existing metric.
-
#histogram(name) ⇒ Object
Public: Fetch or create a new histogram metric.
-
#initialize ⇒ Registry
constructor
Public: Initializes a new Registry.
-
#meter(name) ⇒ Object
Public: Fetch or create a new meter metric.
-
#stop ⇒ Object
Public: Clear all of the metrics in the Registry.
-
#timer(name) ⇒ Object
Public: Fetch or create a new timer metric.
-
#utilization_timer(name) ⇒ Object
Public: Fetch or create a new utilization timer metric.
Constructor Details
#initialize ⇒ Registry
Public: Initializes a new Registry.
21 22 23 24 |
# File 'lib/twingly/metrics/registry.rb', line 21 def initialize @mutex = Mutex.new @metrics = {} end |
Class Method Details
.default ⇒ Object
Public: The default registry for the process.
Returns the default Registry for the process.
16 17 18 |
# File 'lib/twingly/metrics/registry.rb', line 16 def self.default @default ||= new end |
Instance Method Details
#add(name, metric) ⇒ Object
185 186 187 188 189 190 191 |
# File 'lib/twingly/metrics/registry.rb', line 185 def add(name, metric) @mutex.synchronize do raise "Metric '#{name}' already defined" if @metrics[name] @metrics[name] = metric end end |
#clear ⇒ Object
Public: Clear all of the metrics in the Registry. This ensures all metrics that have been added are stopped.
Returns nothing.
30 31 32 33 34 35 36 37 38 |
# File 'lib/twingly/metrics/registry.rb', line 30 def clear @mutex.synchronize do @metrics.each_value do |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 Metrics::Counter identified by the name.
75 76 77 |
# File 'lib/twingly/metrics/registry.rb', line 75 def counter(name) add_or_get(name, Metrics::Counter) end |
#each(&block) ⇒ Object
Public: Iterate over all of the counters.
Examples
registry.each do |name, metric|
puts name
end
Returns nothing.
57 58 59 60 61 62 63 |
# File 'lib/twingly/metrics/registry.rb', line 57 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 Metrics::Gauge identified by the name.
88 89 90 91 92 |
# File 'lib/twingly/metrics/registry.rb', line 88 def gauge(name, callable = nil, &block) add_or_get(name, Metrics::Gauge) do Metrics::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.
168 169 170 171 172 |
# File 'lib/twingly/metrics/registry.rb', line 168 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 Metrics::Histogram identified by the name.
153 154 155 156 157 |
# File 'lib/twingly/metrics/registry.rb', line 153 def histogram(name) add_or_get(name, Metrics::Histogram) do Metrics::Histogram. 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 Metrics::Meter identified by the name.
104 105 106 |
# File 'lib/twingly/metrics/registry.rb', line 104 def meter(name) add_or_get(name, Metrics::Meter) end |
#stop ⇒ Object
Public: Clear all of the metrics in the Registry. This has the same effect as calling #clear.
Returns nothing.
44 45 46 |
# File 'lib/twingly/metrics/registry.rb', line 44 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 Metrics::Timer identified by the name.
119 120 121 |
# File 'lib/twingly/metrics/registry.rb', line 119 def timer(name) add_or_get(name, Metrics::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 Metrics::UtilizationTimer identified by the name.
138 139 140 |
# File 'lib/twingly/metrics/registry.rb', line 138 def utilization_timer(name) add_or_get(name, Metrics::UtilizationTimer) end |