Class: Fluent::Nata2Output

Inherits:
Output
  • Object
show all
Includes:
HandleTagNameMixin, Mixin::RewriteTagName
Defined in:
lib/fluent/plugin/out_nata2.rb

Instance Method Summary collapse

Constructor Details

#initializeNata2Output

Returns a new instance of Nata2Output.



12
13
14
15
16
# File 'lib/fluent/plugin/out_nata2.rb', line 12

def initialize
  super
  require 'net/http'
  require 'uri'
end

Instance Method Details

#configure(conf) ⇒ Object



18
19
20
21
# File 'lib/fluent/plugin/out_nata2.rb', line 18

def configure(conf)
  super
  @url = 'http://' + @server + ':' + @port.to_s
end

#emit(tag, es, chain) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/fluent/plugin/out_nata2.rb', line 23

def emit(tag, es, chain)
  es.each do |time, record|
    emit_tag = tag.clone
    filter_record(emit_tag, time, record)
    service_name, host_name, database_name = prepare_data_to_post(emit_tag, record)
    post_to_nata2(service_name, host_name, database_name, record)
  end
  chain.next
end

#post_to_nata2(service_name, host_name, database_name, record) ⇒ Object



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

def post_to_nata2(service_name, host_name, database_name, record)
  api = URI.parse(@url + %Q{/api/1/#{service_name}/#{host_name}/#{database_name}})
  begin
    request = Net::HTTP::Post.new(api.path)
    request.set_form_data(record)
    http = Net::HTTP.new(api.host, api.port)
    response = http.start.request(request)
  rescue IOError, EOFError, SystemCallError => e
    log.warn %Q{net/http POST raises exception: #{e.class}, '#{e.message}'}
  end
  if !response || !response.is_a?(Net::HTTPSuccess)
    log.warn %Q{failed to post to nata2: #{api}, sql: #{record[:sql]}, code: #{response && response.code}}
  end
end

#prepare_data_to_post(tag, record) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/fluent/plugin/out_nata2.rb', line 33

def prepare_data_to_post(tag, record)
  tag = tag.split('.')
  service_name = tag.shift
  host_name = tag.join('.')
  database_name = record[:database] || record['database']
  [ service_name, host_name, database_name ]
end