lib/fluent/plugin/wendelin_client.rb in fluent-plugin-wendelin-0.1 vs lib/fluent/plugin/wendelin_client.rb in fluent-plugin-wendelin-0.2

- old
+ new

@@ -17,22 +17,21 @@ require 'net/http' require 'openssl' -# show request/response traces or not -$trace = false - # class representing a Wendelin client class WendelinClient # `streamtool_uri` - URI pointing to portal_input_data_stream "mountpoint" # `credentials` # {'user' => _, 'password' => _} TODO change to certificate - def initialize(streamtool_uri, credentials) + # `log` - logger to use + def initialize(streamtool_uri, credentials, log) @streamtool_uri = streamtool_uri @credentials = credentials + @log = log end # ingest `data_chunk` to a stream referenced as `reference` def ingest(reference, data_chunk) @@ -46,51 +45,59 @@ # (but then querystring ?reference=... is lost) # req.body = data_chunk # req.content_type = 'application/octet-stream' req.set_form_data('data_chunk' => data_chunk) - if $trace - puts '>>> REQUEST' - puts "method\t=> #{req.method}" - puts "path\t=> #{req.path}" - puts "uri\t=> #{req.uri}" - puts "body\t=> #{req.body}" - puts "body_stream\t=> #{req.body_stream}" - req.each {|h| puts "#{h}:\t#{req[h]}"} - puts + @log.on_trace do + @log.trace '>>> REQUEST' + @log.trace "method\t=> #{req.method}" + @log.trace "path\t=> #{req.path}" + @log.trace "uri\t=> #{req.uri}" + @log.trace "body\t=> #{req.body}" + @log.trace "body_stream\t=> #{req.body_stream}" + req.each {|h| @log.trace "#{h}:\t#{req[h]}"} + @log.trace end begin # TODO keep connection open (so that every new ingest does not do # full connect again) res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => (uri.scheme == 'https'), # NOTE = "do not check server cert" # TODO move this out to conf parameters - :verify_mode => OpenSSL::SSL::VERIFY_NONE + :verify_mode => OpenSSL::SSL::VERIFY_NONE, + + # Net::HTTP default open timeout is infinity, which results + # in thread hang forever if other side does not fully + # establish connection. Default read_timeout is 60 seconds. + # We go safe way and make sure all timeouts are defined. + :ssl_timeout => 60, + :open_timeout => 60, + :read_timeout => 60, ) do |http| http.request(req) end rescue # some http/ssl/other connection error - puts "HTTP ERROR:" + @log.warn "HTTP ERROR:" raise else - if $trace - puts '>>> RESPONSE' - res.each {|h| puts "#{h}:\t#{res[h]}"} - puts "code\t=> #{res.code}" - puts "msg\t=> #{res.message}" - puts "class\t=> #{res.class}" - puts "body:", res.body + @log.on_trace do + @log.trace '>>> RESPONSE' + res.each {|h| @log.trace "#{h}:\t#{res[h]}"} + @log.trace "code\t=> #{res.code}" + @log.trace "msg\t=> #{res.message}" + @log.trace "class\t=> #{res.class}" + @log.trace "body:", res.body end if res.kind_of?(Net::HTTPSuccess) # res.code is 2XX - #puts "ingested ok" + #@log.info "ingested ok" else - puts "FAIL:" + @log.warn "FAIL:" res.value end end end