class Spectator::Registry

Registry to manage a set of meters

Attributes

clock[R]
common_tags[R]
config[R]
publisher[R]

Public Class Methods

new(config, clock = SystemClock.new) click to toggle source

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

counter(name, tags = nil) click to toggle source

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
counter_with_id(id) click to toggle source

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
distribution_summary(name, tags = nil) click to toggle source

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
distribution_summary_with_id(id) click to toggle source

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
gauge(name, tags = nil) click to toggle source

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
gauge_with_id(id) click to toggle source

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
measurements() click to toggle source

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
new_id(name, tags = nil) click to toggle source

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() click to toggle source

Start publishing measurements to the aggregator service

# File lib/spectator/registry.rb, line 83
def start
  @publisher.start
end
stop() click to toggle source

Stop publishing measurements

# File lib/spectator/registry.rb, line 88
def stop
  @publisher.stop
end
timer(name, tags = nil) click to toggle source

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
timer_with_id(id) click to toggle source

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

new_meter(meter_id) { |meter_id| ... } click to toggle source
# 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
to_symbols(tags) click to toggle source
# 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