Class: Fluent::InfluxdbMetricsOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_influxdb_metrics.rb

Instance Method Summary collapse

Constructor Details

#initializeInfluxdbMetricsOutput

Returns a new instance of InfluxdbMetricsOutput.



16
17
18
# File 'lib/fluent/plugin/out_influxdb_metrics.rb', line 16

def initialize
  super
end

Instance Method Details

#configure(conf) ⇒ Object



20
21
22
# File 'lib/fluent/plugin/out_influxdb_metrics.rb', line 20

def configure(conf)
  super
end

#filter_keysObject



28
29
30
31
# File 'lib/fluent/plugin/out_influxdb_metrics.rb', line 28

def filter_keys
  @filter_keys ||= @fields.split(',').map(&:strip)
  @filter_keys
end

#format(tag, time, record) ⇒ Object



33
34
35
# File 'lib/fluent/plugin/out_influxdb_metrics.rb', line 33

def format(tag, time, record)
  [tag, time, record].to_msgpack
end

#format_record(tag, time, record) ⇒ Object



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
# File 'lib/fluent/plugin/out_influxdb_metrics.rb', line 56

def format_record(tag, time, record)
  cols = ['time']
  points = [time]

  filter_keys.each do |field|
    path = field.split('.')
    rec_pos = record[path.shift]
    path.each do |p|
      rec_pos = rec_pos[p] if rec_pos
    end

    if rec_pos
      cols << field
      points << rec_pos
    end
  end

  if cols.length > 1
    return {
      name: @table,
      columns: cols,
      points: [points]
    }
  end

  nil
end

#shutdownObject



37
38
39
# File 'lib/fluent/plugin/out_influxdb_metrics.rb', line 37

def shutdown
  super
end

#startObject



24
25
26
# File 'lib/fluent/plugin/out_influxdb_metrics.rb', line 24

def start
  super
end

#write(chunk) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fluent/plugin/out_influxdb_metrics.rb', line 41

def write(chunk)
  bulk = []

  chunk.msgpack_each do |tag, time, record|
    formatted_record = format_record(tag, time, record)
    bulk << formatted_record if formatted_record
  end

  http = Net::HTTP.new(@host, @port)
  resp, _ = http.post("/db/#{@dbname}/series?u=#{@user}&p=#{@password}&time_precision=s",
                         Yajl::Encoder.encode(bulk) + "\n",
                         'Content-Type' => 'text/json')
  resp.value
end