module FFWD::Plugin::GoogleCloud::Utils

Constants

M64

Public Class Methods

extract_labels(source) click to toggle source
# File lib/ffwd/plugin/google_cloud/utils.rb, line 100
def self.extract_labels source
  source.map{|k, v| {:key => k, :description => ""}}
end
hash_labels(attributes) click to toggle source
# File lib/ffwd/plugin/google_cloud/utils.rb, line 88
def self.hash_labels attributes
  attributes.keys.sort.map{|v| Zlib::crc32(v.to_s)}.reduce(33){|k, v|
    (63 * k + v).modulo(M64)
  }
end
make_common_labels(buffer) click to toggle source
# File lib/ffwd/plugin/google_cloud/utils.rb, line 24
def self.make_common_labels buffer
  return nil if buffer.empty?
  make_labels buffer.first.fixed_attr
end
make_desc(m) click to toggle source
# File lib/ffwd/plugin/google_cloud/utils.rb, line 62
def self.make_desc m
  {:metric => make_key(m), :labels => make_labels(m.external_attr)}
end
make_key(m) click to toggle source
# File lib/ffwd/plugin/google_cloud/utils.rb, line 66
def self.make_key m
  attributes = Hash[m.external_attr]

  entries = []

  what ||= attributes.delete(:what)
  what ||= attributes.delete("what")

  entries << what unless what.nil?
  entries = entries.join('.')

  # ruby Object#hash is inconsistent across runs, so we will instead
  # perform a custom hashing.
  hash = hash_labels(attributes).to_s(32)

  unless entries.empty?
    "#{CUSTOM_PREFIX}/#{m.key}/#{entries}-#{hash}"
  else
    "#{CUSTOM_PREFIX}/#{m.key}-#{other_keys}"
  end
end
make_labels(attr) click to toggle source
# File lib/ffwd/plugin/google_cloud/utils.rb, line 94
def self.make_labels attr
  Hash[attr.select{|k, v| k.to_s != "what"}.map{|k, v|
    ["#{CUSTOM_PREFIX}/#{k}", v]
  }]
end
make_point(m) click to toggle source
# File lib/ffwd/plugin/google_cloud/utils.rb, line 57
def self.make_point m
  time = m.time.utc.strftime('%FT%TZ')
  {:start => time, :end => time, :doubleValue => m.value}
end
make_timeseries(buffer) click to toggle source
# File lib/ffwd/plugin/google_cloud/utils.rb, line 29
def self.make_timeseries buffer
  # we are not allowed to send duplicate data.
  seen = Set.new

  result = []
  dropped = 0

  buffer.each do |m|
    d = make_desc(m)

    # Deduplicate batches.
    #
    # Asserts that each batch only contains one point for each label
    # configuration.
    seen_key = [d[:metric], d[:labels].map.sort].hash

    if seen.member?(seen_key)
      dropped += 1
      next
    end

    seen.add(seen_key)
    result << {:timeseriesDesc => d, :point => make_point(m)}
  end

  [dropped, result]
end