class Librato::Collector::CounterCache

maintains storage of a set of incrementable, counter-like measurements

Constants

INTEGER_CLASS
SEPARATOR

Attributes

default_tags[R]

Public Class Methods

new(options={}) click to toggle source
# File lib/librato/collector/counter_cache.rb, line 18
def initialize(options={})
  @cache = {}
  @lock = Mutex.new
  @sporadics = Set.new
  @default_tags = options.fetch(:default_tags, {})
end

Public Instance Methods

[](key) click to toggle source

Retrieve the current value for a given metric. This is a short form for convenience which only retrieves metrics with no custom source specified. For more options see fetch.

@param [String|Symbol] key metric name @return [Integer|Float] current value

# File lib/librato/collector/counter_cache.rb, line 31
def [](key)
  fetch(key)
end
delete_all() click to toggle source

removes all tracked metrics. note this removes all measurement data AND metric names any continuously tracked metrics will not report until they get another measurement

# File lib/librato/collector/counter_cache.rb, line 38
def delete_all
  @lock.synchronize { @cache.clear }
end
fetch(key, options={}) click to toggle source
# File lib/librato/collector/counter_cache.rb, line 42
def fetch(key, options={})
  key = key.to_s
  key =
    if options[:tags]
      Librato::Metrics::Util.build_key_for(key, options[:tags])
    elsif @default_tags
      Librato::Metrics::Util.build_key_for(key, @default_tags)
    end
  @lock.synchronize { @cache[key] }
end
flush_to(queue, opts={}) click to toggle source

transfer all measurements to queue and reset internal status

# File lib/librato/collector/counter_cache.rb, line 54
def flush_to(queue, opts={})
  counts = nil
  @lock.synchronize do
    # work off of a duplicate data set so we block for
    # as little time as possible
    # requires a deep copy of data set
    counts = JSON.parse(@cache.dup.to_json, symbolize_names: true)
    reset_cache unless opts[:preserve]
  end
  counts.each do |metric, payload|
    metric = metric.to_s.split(SEPARATOR).first
    queue.add metric => payload
  end
end
increment(counter, options={}) click to toggle source

Increment a given metric

@example Increment metric 'foo' by 1

increment :foo

@example Increment metric 'bar' by 2

increment :bar, :by => 2

@example Increment metric 'foo' by 1 with a custom source

increment :foo, :source => user.id
# File lib/librato/collector/counter_cache.rb, line 80
def increment(counter, options={})
  metric = counter.to_s
  if options.is_a?(INTEGER_CLASS)
    # suppport legacy style
    options = {by: options}
  end
  by = options[:by] || 1
  source = options[:source]
  tags_option = options[:tags]
  tags_option = { source: source } if source && !tags_option
  tags =
    if tags_option && options[:inherit_tags]
      @default_tags.merge(tags_option)
    elsif tags_option
      tags_option
    else
      @default_tags
    end
  metric = Librato::Metrics::Util.build_key_for(metric, tags) if tags
  if options[:sporadic]
    make_sporadic(metric)
  end
  @lock.synchronize do
    @cache[metric] = {} unless @cache[metric]
    @cache[metric][:name] ||= metric
    @cache[metric][:value] ||= 0
    @cache[metric][:value] += by
    @cache[metric][:tags] = tags if tags
  end
end

Private Instance Methods

make_sporadic(metric) click to toggle source
# File lib/librato/collector/counter_cache.rb, line 113
def make_sporadic(metric)
  @sporadics << metric
end
reset_cache() click to toggle source
# File lib/librato/collector/counter_cache.rb, line 117
def reset_cache
  # remove any source/metric pairs that aren't continuous
  @sporadics.each { |metric| @cache.delete(metric) }
  @sporadics.clear
  # reset all continuous source/metric pairs to 0
  @cache.each_key { |key| @cache[key][:value] = 0 }
end