Class: Librato::Metrics::Aggregator
- Inherits:
-
Object
- Object
- Librato::Metrics::Aggregator
- Includes:
- Processor
- Defined in:
- lib/librato/metrics/aggregator.rb
Overview
If you are measuring something very frequently you can sample into an aggregator and it will track and submit a single aggregate measurement
Constant Summary collapse
- SEPARATOR =
must not be in valid tags and/or source criteria
'%%'
Constants included from Processor
Processor::MEASUREMENTS_PER_REQUEST
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Attributes included from Processor
#last_submit_time, #per_request, #prefix, #tags
Instance Method Summary collapse
-
#add(measurements) ⇒ Aggregator
Add a metric entry to the metric set:.
-
#clear ⇒ Object
(also: #flush)
Remove all queued metrics.
-
#empty? ⇒ Boolean
Returns true if aggregate contains no measurements.
-
#initialize(opts = {}) ⇒ Aggregator
constructor
A new instance of Aggregator.
-
#queued ⇒ Object
Returns currently queued data.
Methods included from Processor
#client, #has_tags?, #persister, #submit, #time
Constructor Details
#initialize(opts = {}) ⇒ Aggregator
Returns a new instance of Aggregator.
38 39 40 41 |
# File 'lib/librato/metrics/aggregator.rb', line 38 def initialize(opts={}) @aggregated = {} (opts) end |
Instance Attribute Details
#source ⇒ Object (readonly)
Returns the value of attribute source
30 31 32 |
# File 'lib/librato/metrics/aggregator.rb', line 30 def source @source end |
Instance Method Details
#add(measurements) ⇒ Aggregator
Add a metric entry to the metric set:
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/librato/metrics/aggregator.rb', line 53 def add(measurements) measurements.each do |metric, data| entry = {} if @prefix metric = "#{@prefix}.#{metric}" end entry[:name] = metric.to_s if data.respond_to?(:each) # hash form validate_parameters(data) value = data[:value] if data[:source] metric = "#{metric}#{SEPARATOR}#{data[:source]}" entry[:source] = data[:source].to_s elsif data[:tags] && data[:tags].respond_to?(:each) metric = Librato::Metrics::Util.build_key_for(metric.to_s, data[:tags]) entry[:tags] = data[:tags] end else value = data end @aggregated[metric] = {} unless @aggregated[metric] @aggregated[metric][:aggregate] ||= Aggregate.new @aggregated[metric][:aggregate] << value @aggregated[metric].merge!(entry) end autosubmit_check self end |
#clear ⇒ Object Also known as: flush
Remove all queued metrics
92 93 94 |
# File 'lib/librato/metrics/aggregator.rb', line 92 def clear @aggregated = {} end |
#empty? ⇒ Boolean
Returns true if aggregate contains no measurements
86 87 88 |
# File 'lib/librato/metrics/aggregator.rb', line 86 def empty? @aggregated.empty? end |
#queued ⇒ Object
Returns currently queued data
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/librato/metrics/aggregator.rb', line 99 def queued entries = [] multidimensional = @aggregated.each_value do |data| entry = { name: data[:name], count: data[:aggregate].count, sum: data[:aggregate].sum, # TODO: make float/non-float consistent in the gem min: data[:aggregate].min.to_f, max: data[:aggregate].max.to_f # TODO: expose v.sum2 and include } if data[:source] entry[:source] = data[:source] elsif data[:tags] multidimensional = true entry[:tags] = data[:tags] end multidimensional = true if data[:time] entries << entry end time = multidimensional ? :time : :measure_time req = if multidimensional { measurements: entries } else { gauges: entries } end req[:source] = @source if @source req[:tags] = @tags if req[time] = @time if @time req end |