class Leafy::Core::MetricRegistry
A registry of metric instances.
Public Class Methods
Creates a new {@link MetricRegistry}.
# File lib/leafy/core/metric_registry.rb, line 15 def initialize @metrics = build_map end
Public Instance Methods
Creates a new {@link ConcurrentMap} implementation for use inside the registry. Override this to create a {@link MetricRegistry} with space- or time-bounded metric lifecycles, for example.
@return a new {@link ConcurrentMap}
# File lib/leafy/core/metric_registry.rb, line 24 def build_map Concurrent::Map.new end
Return the {@link Counter} registered under this name or create and register a new {@link Counter} if none is registered.
@param name the name of the metric @return a new or pre-existing {@link Counter}
# File lib/leafy/core/metric_registry.rb, line 58 def counter(name) getOrAdd(name, Builder.counters) end
Returns a map of all the counters in the registry and their names.
@return all the counters in the registry
# File lib/leafy/core/metric_registry.rb, line 116 def counters metrics(Counter) end
Return the {@link Gauge} registered under this name or create and register a new {@link Gauge} if none is registered.
@param name the name of the metric @param supplier a Builder
that can be used to manufacture a Gauge
@return a new or pre-existing {@link Gauge}
# File lib/leafy/core/metric_registry.rb, line 49 def gauge(name, builder) getOrAdd(name, builder) end
Returns a map of all the gauges in the registry and their names.
@return all the gauges in the registry
# File lib/leafy/core/metric_registry.rb, line 109 def gauges metrics(Gauge) end
Return the {@link Histogram} registered under this name or create and register a new {@link Histogram} if none is registered.
@param name the name of the metric @return a new or pre-existing {@link Histogram}
# File lib/leafy/core/metric_registry.rb, line 67 def histogram(name) getOrAdd(name, Builder.histograms) end
Returns a map of all the histograms in the registry and their names.
@return all the histograms in the registry
# File lib/leafy/core/metric_registry.rb, line 123 def histograms metrics(Histogram) end
Return the {@link Meter} registered under this name or create and register a new {@link Meter} if none is registered.
@param name the name of the metric @return a new or pre-existing {@link Meter}
# File lib/leafy/core/metric_registry.rb, line 76 def meter(name) getOrAdd(name, Builder.meters) end
Returns a map of all the meters in the registry and their names.
@return all the meters in the registry
# File lib/leafy/core/metric_registry.rb, line 130 def meters metrics(Meter) end
# File lib/leafy/core/metric_registry.rb, line 141 def metrics(klass = nil) if klass result = {} @metrics.each do |k,v| result[k] = v if v.is_a?(klass) end result.freeze else Hash[@metrics.keys.zip(@metrics.values)].freeze end end
Returns a set of the names of all the metrics in the registry.
@return the names of all the metrics
# File lib/leafy/core/metric_registry.rb, line 102 def names @metrics.keys.dup.freeze end
Given a {@link Metric}, registers it under the given name.
@param name the name of the metric @param metric the metric @param <T> the type of the metric @return {@code metric} @throws IllegalArgumentException if the name is already registered
# File lib/leafy/core/metric_registry.rb, line 35 def register(name, metric) existing = @metrics.put_if_absent(name, metric) if existing raise ArgumentError.new("A metric named #{name} already exists") end metric end
Removes the metric with the given name. @param name the name of the metric @return whether or not the metric was removed
/
# File lib/leafy/core/metric_registry.rb, line 94 def remove(name) metric = @metrics.delete(name) metric != nil end
Return the {@link Timer} registered under this name or create and register a new {@link Timer} if none is registered.
@param name the name of the metric @return a new or pre-existing {@link Timer}
# File lib/leafy/core/metric_registry.rb, line 85 def timer(name) getOrAdd(name, Builder.timers) end
Returns a map of all the timers in the registry and their names.
@return all the timers in the registry
# File lib/leafy/core/metric_registry.rb, line 137 def timers metrics(Timer) end
Private Instance Methods
# File lib/leafy/core/metric_registry.rb, line 153 def getOrAdd(name, builder) metric = @metrics[name] if builder.instance?(metric) return metric elsif metric.nil? begin return register(name, builder.new_metric) rescue ArgumentError added = @metrics[name] return added if builder.instance?(added) end end raise ArgumentError.new("#{name} is already used for a different type of metric") end