Class: Librato::Collector::CounterCache

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/librato/collector/counter_cache.rb

Overview

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

Constant Summary collapse

SEPARATOR =
'%%'
INTEGER_CLASS =
1.class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CounterCache

Returns a new instance of CounterCache.



18
19
20
21
22
23
# 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

Instance Attribute Details

#default_tagsObject (readonly)

Returns the value of attribute default_tags.



16
17
18
# File 'lib/librato/collector/counter_cache.rb', line 16

def default_tags
  @default_tags
end

Instance Method Details

#[](key) ⇒ Integer|Float

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.

Parameters:

  • key (String|Symbol)

    metric name

Returns:

  • (Integer|Float)

    current value



31
32
33
# File 'lib/librato/collector/counter_cache.rb', line 31

def [](key)
  fetch(key)
end

#delete_allObject

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



38
39
40
# File 'lib/librato/collector/counter_cache.rb', line 38

def delete_all
  @lock.synchronize { @cache.clear }
end

#fetch(key, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# 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 = {}) ⇒ Object

transfer all measurements to queue and reset internal status



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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 = {}) ⇒ Object

Increment a given metric

Examples:

Increment metric ‘foo’ by 1

increment :foo

Increment metric ‘bar’ by 2

increment :bar, :by => 2

Increment metric ‘foo’ by 1 with a custom source

increment :foo, :source => user.id


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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