Class: Librato::Collector::CounterCache
- Inherits:
-
Object
- Object
- Librato::Collector::CounterCache
- 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
-
#default_tags ⇒ Object
readonly
Returns the value of attribute default_tags.
Instance Method Summary collapse
-
#[](key) ⇒ Integer|Float
Retrieve the current value for a given metric.
-
#delete_all ⇒ Object
removes all tracked metrics.
- #fetch(key, options = {}) ⇒ Object
-
#flush_to(queue, opts = {}) ⇒ Object
transfer all measurements to queue and reset internal status.
-
#increment(counter, options = {}) ⇒ Object
Increment a given metric.
-
#initialize(options = {}) ⇒ CounterCache
constructor
A new instance of CounterCache.
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(={}) @cache = {} @lock = Mutex.new @sporadics = Set.new = .fetch(:default_tags, {}) end |
Instance Attribute Details
#default_tags ⇒ Object (readonly)
Returns the value of attribute default_tags.
16 17 18 |
# File 'lib/librato/collector/counter_cache.rb', line 16 def 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.
31 32 33 |
# File 'lib/librato/collector/counter_cache.rb', line 31 def [](key) fetch(key) end |
#delete_all ⇒ Object
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, ={}) key = key.to_s key = if [:tags] Librato::Metrics::Util.build_key_for(key, [:tags]) elsif Librato::Metrics::Util.build_key_for(key, ) 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
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, ={}) metric = counter.to_s if .is_a?(INTEGER_CLASS) # suppport legacy style = {by: } end by = [:by] || 1 source = [:source] = [:tags] = { source: source } if source && ! = if && [:inherit_tags] .merge() elsif else end metric = Librato::Metrics::Util.build_key_for(metric, ) if if [: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] = if end end |