Module: FFWD::Plugin::GoogleCloud::Utils

Defined in:
lib/ffwd/plugin/google_cloud/utils.rb

Class Method Summary collapse

Class Method Details

.extract_labels(source) ⇒ Object



80
81
82
# File 'lib/ffwd/plugin/google_cloud/utils.rb', line 80

def self.extract_labels source
  source.map{|k, v| {:key => k, :description => k}}
end

.make_desc(m) ⇒ Object



48
49
50
# File 'lib/ffwd/plugin/google_cloud/utils.rb', line 48

def self.make_desc m
  {:metric => make_key(m), :labels => make_labels(m)}
end

.make_key(m) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ffwd/plugin/google_cloud/utils.rb', line 52

def self.make_key m
  attributes = Hash[m.attributes]

  entries = []

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

  entries << what unless what.nil?
  entries += attributes.keys.sort.map(&:to_s)
  entries = entries.join('.')

  unless entries.empty?
    "#{CUSTOM_PREFIX}/#{m.key}/#{entries}"
  else
    "#{CUSTOM_PREFIX}/#{m.key}"
  end
end

.make_labels(m) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/ffwd/plugin/google_cloud/utils.rb', line 71

def self.make_labels m
  labels = Hash[m.attributes.select{|k, v| k.to_s != "what"}.map{|k, v|
    ["#{CUSTOM_PREFIX}/#{k}", v]
  }]

  #labels["#{CUSTOM_PREFIX}/host"] = m.host
  labels
end

.make_point(m) ⇒ Object



43
44
45
46
# File 'lib/ffwd/plugin/google_cloud/utils.rb', line 43

def self.make_point m
  time = m.time.utc.strftime('%FT%TZ')
  {:start => time, :end => time, :doubleValue => m.value}
end

.make_timeseries(buffer) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ffwd/plugin/google_cloud/utils.rb', line 21

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)

    if seen.member?(d[:metric])
      dropped += 1
      next
    end

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

  [dropped, result]
end