Class: Fluent::StackdriverV1MetricsOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_stackdriver-v1_metrics.rb

Instance Method Summary collapse

Constructor Details

#initializeStackdriverV1MetricsOutput

Returns a new instance of StackdriverV1MetricsOutput.



6
7
8
9
# File 'lib/fluent/plugin/out_stackdriver-v1_metrics.rb', line 6

def initialize
  super
  require 'stackdriver'
end

Instance Method Details

#configure(conf) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fluent/plugin/out_stackdriver-v1_metrics.rb', line 18

def configure(conf)
  super(conf) {
    @api_key = conf.delete('api_key')
    @instance_id = conf.delete('instance_id')
    @counter_maps = conf.delete('counter_maps')
    @counter_defaults = conf.delete('counter_defaults')
    @metric_maps = conf.delete('metric_maps')
    @metric_defaults = conf.delete('metric_defaults')
  }

  @base_entry = {}
  @base_entry['instance'] = @instance_id if @instance_id

end

#format(tag, time, record) ⇒ Object



33
34
35
36
# File 'lib/fluent/plugin/out_stackdriver-v1_metrics.rb', line 33

def format(tag, time, record)
  # Everything goes into the buffer in a JSON format.
  { 'tag' => tag, 'time' => time, 'record' => record }.to_json + "\n"
end

#write(chunk) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fluent/plugin/out_stackdriver-v1_metrics.rb', line 38

def write(chunk)

  timestamp = Time.now.to_i
  data = []

  count_data = {}
  metric_data = {}

  chunk.read.chomp.split("\n").each do |line|
    event = JSON.parse(line)

    @counter_maps.each do |k,v|
      if eval(k)
        name = eval(v) 
        count_data[name] ||= 0
        count_data[name] += 1
      end
    end

    @metric_maps.each do |k,v|
      if eval(k)
        if eval(v)
          data << @base_entry.merge({ 'collected_at' => event['time'].to_i }).merge(eval(v))
        end
      end
    end

  end

  count_data.each do |name,value|
    data << @base_entry.merge({
      'name' => name,
      'value' => value,
      'collected_at' => timestamp
    })
  end

  @counter_defaults.each do |e|
    if not count_data.key?(e['name'])
      data << @base_entry.merge({'collected_at' => timestamp}).merge(e)
    end
  end

  @metric_defaults.each do |e|
    if not metric_data.key?(e['name'])
      data << @base_entry.merge({'collected_at' => timestamp}).merge(e)
    end
  end

  if data
    StackDriver.init @api_key
    StackDriver.send_multi_metrics data
  end

end