class Spectator::Registry
Registry
to manage a set of meters
Attributes
Public Class Methods
Initialize the registry using the given config, and clock The default clock is the SystemClock
The config is a Hash which should include:
:common_tags as a hash with tags that will be added to all metrics :frequency the interval at which metrics will be sent to an aggregator service, expressed in seconds :uri the endpoint for the aggregator service
# File lib/spectator/registry.rb, line 21 def initialize(config, clock = SystemClock.new) @config = config @clock = clock @meters = {} @common_tags = to_symbols(config[:common_tags]) || {} @lock = Mutex.new @publisher = Publisher.new(self) end
Public Instance Methods
Create or get a Counter
with the given name, and optional tags
# File lib/spectator/registry.rb, line 56 def counter(name, tags = nil) counter_with_id(MeterId.new(name, tags)) end
Create or get a Counter
with the given id
# File lib/spectator/registry.rb, line 36 def counter_with_id(id) new_meter(id) { |meter_id| Counter.new(meter_id) } end
Create or get a DistributionSummary
with the given name, and optional tags
# File lib/spectator/registry.rb, line 66 def distribution_summary(name, tags = nil) distribution_summary_with_id(MeterId.new(name, tags)) end
Create or get a DistributionSummary
with the given id
# File lib/spectator/registry.rb, line 46 def distribution_summary_with_id(id) new_meter(id) { |meter_id| DistributionSummary.new(meter_id) } end
Create or get a Gauge
with the given name, and optional tags
# File lib/spectator/registry.rb, line 61 def gauge(name, tags = nil) gauge_with_id(MeterId.new(name, tags)) end
Create or get a Gauge
with the given id
# File lib/spectator/registry.rb, line 41 def gauge_with_id(id) new_meter(id) { |meter_id| Gauge.new(meter_id) } end
Get the list of measurements from all registered meters
# File lib/spectator/registry.rb, line 76 def measurements @lock.synchronize do @meters.values.flat_map(&:measure) end end
Create a new MeterId
with the given name, and optional tags
# File lib/spectator/registry.rb, line 31 def new_id(name, tags = nil) MeterId.new(name, tags) end
Start publishing measurements to the aggregator service
# File lib/spectator/registry.rb, line 83 def start @publisher.start end
Stop publishing measurements
# File lib/spectator/registry.rb, line 88 def stop @publisher.stop end
Create or get a Timer
with the given name, and optional tags
# File lib/spectator/registry.rb, line 71 def timer(name, tags = nil) timer_with_id(MeterId.new(name, tags)) end
Create or get a Timer
with the given id
# File lib/spectator/registry.rb, line 51 def timer_with_id(id) new_meter(id) { |meter_id| Timer.new(meter_id) } end
Private Instance Methods
# File lib/spectator/registry.rb, line 102 def new_meter(meter_id) @lock.synchronize do meter = @meters[meter_id.key] if meter.nil? meter = yield(meter_id) @meters[meter_id.key] = meter end meter end end
# File lib/spectator/registry.rb, line 94 def to_symbols(tags) return nil if tags.nil? symbolic_tags = {} tags.each { |k, v| symbolic_tags[k.to_sym] = v.to_sym } symbolic_tags end