Class: Fluent::HTTPOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_alertmanager.rb

Instance Method Summary collapse

Constructor Details

#initializeHTTPOutput

Returns a new instance of HTTPOutput.



4
5
6
7
8
9
# File 'lib/fluent/plugin/out_alertmanager.rb', line 4

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

Instance Method Details

#configure(_) ⇒ Object



14
15
16
17
# File 'lib/fluent/plugin/out_alertmanager.rb', line 14

def configure(_)
  super
  @uri = URI.parse(@endpoint_url)
end

#create_request(record) ⇒ Object



27
28
29
30
31
32
# File 'lib/fluent/plugin/out_alertmanager.rb', line 27

def create_request(record)
  req = Net::HTTP::Post.new(@uri.path)
  req.body = Yajl.dump([record])
  req['Content-Type'] = 'application/json'
  return req
end

#emit(_, es, chain) ⇒ Object



56
57
58
59
60
61
# File 'lib/fluent/plugin/out_alertmanager.rb', line 56

def emit(_, es, chain)
  es.each do |_, record|
    send_alert(record)
  end
  chain.next
end

#send_alert(record) ⇒ Object



52
53
54
# File 'lib/fluent/plugin/out_alertmanager.rb', line 52

def send_alert(record)
  send_request(create_request(record))
end

#send_request(req) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fluent/plugin/out_alertmanager.rb', line 34

def send_request(req)
  res = nil
  begin
    res = Net::HTTP.start(@uri.host, @uri.port) {|http| http.request(req) }
  rescue => e
    $log.error "Net::HTTP::Post raises exception: #{e.class}, '#{e.message}'"
  else
    unless res and res.is_a?(Net::HTTPSuccess)
      res_summary = if res
                      "#{res.code} #{res.message} #{res.body}"
                    else
                      "res=nil"
                    end
      $log.error "failed to #{req.method} #{@uri} (#{res_summary})"
    end
  end
end

#shutdownObject



23
24
25
# File 'lib/fluent/plugin/out_alertmanager.rb', line 23

def shutdown
  super
end

#startObject



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

def start
  super
end