Module: FFWD::Plugin::KairosDB::Utils

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

Class Method Summary collapse

Class Method Details

.make_metrics(buffer) ⇒ Object

groups similar metadata and escapes them using the suite of safe_* functions available.

Should prevent unecessary invocations of safe_entry by only adding new groups of the source metric differs (||=).



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ffwd/plugin/kairosdb/utils.rb', line 23

def self.make_metrics buffer
  groups = {}

  buffer.each do |m|
    entry = {:host => m.host, :name => m.key, :attributes => m.attributes}
    group = (groups[entry] ||= safe_entry(entry).merge(:datapoints => []))
    group[:datapoints] << [(m.time.to_f * 1000).to_i, m.value]
  end

  return groups.values
end

.safe_entry(entry) ⇒ Object

make safe entry out of available information.



36
37
38
39
40
41
# File 'lib/ffwd/plugin/kairosdb/utils.rb', line 36

def self.safe_entry entry
  name = entry[:name]
  host = entry[:host]
  attributes = entry[:attributes]
  {:name => safe_string(name), :tags => safe_tags(host, attributes)}
end

.safe_string(string) ⇒ Object

Warning: These are the ‘bad’ characters I’ve been able to reverse engineer so far.



45
46
47
48
49
# File 'lib/ffwd/plugin/kairosdb/utils.rb', line 45

def self.safe_string string
  string = string.to_s
  string = string.gsub " ", "/"
  string.gsub ":", "_"
end

.safe_tags(host, attributes) ⇒ Object

Warning: KairosDB ignores complete metrics if you use tags which have no values, therefore I have not figured out a way to transport ‘tags’.



53
54
55
56
57
58
59
60
61
# File 'lib/ffwd/plugin/kairosdb/utils.rb', line 53

def self.safe_tags host, attributes
  tags = {"host" => safe_string(host)}

  attributes.each do |key, value|
    tags[safe_string(key)] = safe_string(value)
  end

  return tags
end