Class: Fluent::HTTPOutput
- Inherits:
-
Output
- Object
- Output
- Fluent::HTTPOutput
- Defined in:
- lib/fluent/plugin/out_http.rb
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #create_request(tag, time, record) ⇒ Object
- #emit(tag, es, chain) ⇒ Object
- #format_url(tag, time, record) ⇒ Object
-
#handle_record(tag, time, record) ⇒ Object
end send_request.
- #http_opts(uri) ⇒ Object
-
#initialize ⇒ HTTPOutput
constructor
A new instance of HTTPOutput.
- #send_request(req, uri) ⇒ Object
- #set_body(req, tag, time, record) ⇒ Object
- #set_header(req, tag, time, record) ⇒ Object
- #set_json_body(req, data) ⇒ Object
- #set_text_body(req, data) ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ HTTPOutput
Returns a new instance of HTTPOutput.
4 5 6 7 8 9 |
# File 'lib/fluent/plugin/out_http.rb', line 4 def initialize super require 'net/http' require 'uri' require 'yajl' end |
Instance Method Details
#configure(conf) ⇒ Object
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 |
# File 'lib/fluent/plugin/out_http.rb', line 39 def configure(conf) super @ssl_verify_mode = if @ssl_no_verify OpenSSL::SSL::VERIFY_NONE else OpenSSL::SSL::VERIFY_PEER end @ca_file = @cacert_file serializers = [:json, :form, :text] @serializer = if serializers.include? @serializer.intern @serializer.intern else :form end http_methods = [:get, :put, :post, :delete] @http_method = if http_methods.include? @http_method.intern @http_method.intern else :post end @auth = case @authentication when 'basic' then :basic when 'bearer' then :bearer when 'jwt' then :jwt else :none end @last_request_time = nil end |
#create_request(tag, time, record) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/fluent/plugin/out_http.rb', line 112 def create_request(tag, time, record) url = format_url(tag, time, record) uri = URI.parse(url) req = Net::HTTP.const_get(@http_method.to_s.capitalize).new(uri.path) set_body(req, tag, time, record) set_header(req, tag, time, record) return req, uri end |
#emit(tag, es, chain) ⇒ Object
170 171 172 173 174 175 |
# File 'lib/fluent/plugin/out_http.rb', line 170 def emit(tag, es, chain) es.each do |time, record| handle_record(tag, time, record) end chain.next end |
#format_url(tag, time, record) ⇒ Object
83 84 85 |
# File 'lib/fluent/plugin/out_http.rb', line 83 def format_url(tag, time, record) @endpoint_url end |
#handle_record(tag, time, record) ⇒ Object
end send_request
165 166 167 168 |
# File 'lib/fluent/plugin/out_http.rb', line 165 def handle_record(tag, time, record) req, uri = create_request(tag, time, record) send_request(req, uri) end |
#http_opts(uri) ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/fluent/plugin/out_http.rb', line 121 def http_opts(uri) opts = { :use_ssl => uri.scheme == 'https' } opts[:verify_mode] = @ssl_verify_mode if opts[:use_ssl] opts[:ca_file] = File.join(@ca_file) if File.file?(@ca_file) opts end |
#send_request(req, uri) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/fluent/plugin/out_http.rb', line 130 def send_request(req, uri) is_rate_limited = (@rate_limit_msec != 0 and not @last_request_time.nil?) if is_rate_limited and ((Time.now.to_f - @last_request_time) * 1000.0 < @rate_limit_msec) $log.info('Dropped request due to rate limiting') return end res = nil begin if @auth and @auth == :basic req.basic_auth(@username, @password) elsif @auth and @auth == :bearer req['authorization'] = "bearer #{@token}" elsif @auth and @auth == :jwt req['authorization'] = "jwt #{@token}" end @last_request_time = Time.now.to_f res = Net::HTTP.start(uri.host, uri.port, **http_opts(uri)) {|http| http.request(req) } rescue => e # rescue all StandardErrors # server didn't respond $log.warn "Net::HTTP.#{req.method.capitalize} raises exception: #{e.class}, '#{e.}'" raise e if @raise_on_error else unless res and res.is_a?(Net::HTTPSuccess) res_summary = if res "#{res.code} #{res.} #{res.body}" else "res=nil" end $log.warn "failed to #{req.method} #{uri} (#{res_summary})" end #end unless end # end begin end |
#set_body(req, tag, time, record) ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/fluent/plugin/out_http.rb', line 87 def set_body(req, tag, time, record) if @serializer == :json set_json_body(req, record) elsif @serializer == :text set_text_body(req, record) else req.set_form_data(record) end req end |
#set_header(req, tag, time, record) ⇒ Object
98 99 100 |
# File 'lib/fluent/plugin/out_http.rb', line 98 def set_header(req, tag, time, record) req end |
#set_json_body(req, data) ⇒ Object
102 103 104 105 |
# File 'lib/fluent/plugin/out_http.rb', line 102 def set_json_body(req, data) req.body = Yajl.dump(data) req['Content-Type'] = 'application/json' end |
#set_text_body(req, data) ⇒ Object
107 108 109 110 |
# File 'lib/fluent/plugin/out_http.rb', line 107 def set_text_body(req, data) req.body = data["message"] req['Content-Type'] = 'text/plain' end |
#shutdown ⇒ Object
79 80 81 |
# File 'lib/fluent/plugin/out_http.rb', line 79 def shutdown super end |
#start ⇒ Object
75 76 77 |
# File 'lib/fluent/plugin/out_http.rb', line 75 def start super end |