class Librato::Metrics::Aggregator

If you are measuring something very frequently you can sample into an aggregator and it will track and submit a single aggregate measurement

@example

aggregator = Libato::Metrics::Aggregator.new

40.times do
  # do work...
  aggregator.add 'work.time' => work_time
end

# send directly
aggregator.submit

# or merge into a queue for submission
queue.merge!(aggregator)

Constants

SEPARATOR

Attributes

source[R]

Public Class Methods

new(opts={}) click to toggle source

@option opts [Integer] :autosubmit_interval If set the aggregator will auto-submit if the given number of seconds has passed when a new metric is added. @option opts [Boolean] :clear_failures Should the aggregator remove all stored data if it runs into problems with a request? (default: false) @option opts [Client] :client The client object to use to connect to Metrics. (default: Librato::Metrics.client) @option opts [Time|Integer] :measure_time A default measure_time to use for measurements added. @option opts [String] :prefix If set will apply the given prefix to all metric names of measurements added. @option opts [String] :source The default source to use for measurements added.

# File lib/librato/metrics/aggregator.rb, line 38
def initialize(opts={})
  @aggregated = {}
  setup_common_options(opts)
end

Public Instance Methods

add(measurements) click to toggle source

Add a metric entry to the metric set:

@example Basic use

aggregator.add 'request.time' => 30.24

@example With a custom source

aggregator.add 'request.time' => {value: 20.52, source: 'staging'}

@param [Hash] measurements measurements to add @return [Aggregator] returns self

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

Remove all queued metrics

# File lib/librato/metrics/aggregator.rb, line 92
def clear
  @aggregated = {}
end
Also aliased as: flush
empty?() click to toggle source

Returns true if aggregate contains no measurements

@return [Boolean]

# File lib/librato/metrics/aggregator.rb, line 86
def empty?
  @aggregated.empty?
end
flush()
Alias for: clear
queued() click to toggle source

Returns currently queued data

# File lib/librato/metrics/aggregator.rb, line 99
def queued
  entries = []
  multidimensional = has_tags?

  @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 has_tags?
  req[time] = @time if @time

  req
end