Commit 11c22159 authored by Kirill Smelkov's avatar Kirill Smelkov

Use fluentd infrastructure for tracing

Previously WendelinClient's tracing was controlled by global $trace
variable and was not correlated to current Fluentd log level, or current
plugin log level and thus it was harder for users to use.

Improve it and rework the tracing to use fluentd logging infrastructure.

This way tracing is disabled by default, but can be either globally
enabled with

    fluentd -v -v

or only for wendelin_out plugin via adding

    log_level   trace

to <match ...> section which routes messages to wendelin_out.

/cc @klaus
parent 70098a73
...@@ -47,7 +47,7 @@ module Fluent ...@@ -47,7 +47,7 @@ module Fluent
credentials['user'] = @user credentials['user'] = @user
credentials['password'] = @password credentials['password'] = @password
end end
@wendelin = WendelinClient.new(@streamtool_uri, credentials) @wendelin = WendelinClient.new(@streamtool_uri, credentials, @log)
end end
def start def start
......
...@@ -19,18 +19,17 @@ ...@@ -19,18 +19,17 @@
require 'net/http' require 'net/http'
require 'openssl' require 'openssl'
# show request/response traces or not
$trace = false
# class representing a Wendelin client # class representing a Wendelin client
class WendelinClient class WendelinClient
# `streamtool_uri` - URI pointing to portal_input_data_stream "mountpoint" # `streamtool_uri` - URI pointing to portal_input_data_stream "mountpoint"
# `credentials` # {'user' => _, 'password' => _} TODO change to certificate # `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 @streamtool_uri = streamtool_uri
@credentials = credentials @credentials = credentials
@log = log
end end
...@@ -48,15 +47,15 @@ class WendelinClient ...@@ -48,15 +47,15 @@ class WendelinClient
# req.content_type = 'application/octet-stream' # req.content_type = 'application/octet-stream'
req.set_form_data('data_chunk' => data_chunk) req.set_form_data('data_chunk' => data_chunk)
if $trace @log.on_trace do
puts '>>> REQUEST' @log.trace '>>> REQUEST'
puts "method\t=> #{req.method}" @log.trace "method\t=> #{req.method}"
puts "path\t=> #{req.path}" @log.trace "path\t=> #{req.path}"
puts "uri\t=> #{req.uri}" @log.trace "uri\t=> #{req.uri}"
puts "body\t=> #{req.body}" @log.trace "body\t=> #{req.body}"
puts "body_stream\t=> #{req.body_stream}" @log.trace "body_stream\t=> #{req.body_stream}"
req.each {|h| puts "#{h}:\t#{req[h]}"} req.each {|h| @log.trace "#{h}:\t#{req[h]}"}
puts @log.trace
end end
begin begin
...@@ -73,23 +72,23 @@ class WendelinClient ...@@ -73,23 +72,23 @@ class WendelinClient
rescue rescue
# some http/ssl/other connection error # some http/ssl/other connection error
puts "HTTP ERROR:" @log.warn "HTTP ERROR:"
raise raise
else else
if $trace @log.on_trace do
puts '>>> RESPONSE' @log.trace '>>> RESPONSE'
res.each {|h| puts "#{h}:\t#{res[h]}"} res.each {|h| @log.trace "#{h}:\t#{res[h]}"}
puts "code\t=> #{res.code}" @log.trace "code\t=> #{res.code}"
puts "msg\t=> #{res.message}" @log.trace "msg\t=> #{res.message}"
puts "class\t=> #{res.class}" @log.trace "class\t=> #{res.class}"
puts "body:", res.body @log.trace "body:", res.body
end end
if res.kind_of?(Net::HTTPSuccess) # res.code is 2XX if res.kind_of?(Net::HTTPSuccess) # res.code is 2XX
#puts "ingested ok" #@log.info "ingested ok"
else else
puts "FAIL:" @log.warn "FAIL:"
res.value res.value
end end
end end
......
...@@ -5,6 +5,10 @@ ...@@ -5,6 +5,10 @@
here = File.dirname(__FILE__) here = File.dirname(__FILE__)
$LOAD_PATH << File.expand_path(File.join(here, '../lib')) $LOAD_PATH << File.expand_path(File.join(here, '../lib'))
require 'fluent/plugin/wendelin_client' require 'fluent/plugin/wendelin_client'
require 'fluent/log'
require 'fluent/engine' # for log
log = Fluent::Log.new(out=STDERR, level=Fluent::Log::LEVEL_TRACE)
# FIXME hardcoded # FIXME hardcoded
$user = 'user' $user = 'user'
...@@ -14,7 +18,7 @@ $password = 'password' ...@@ -14,7 +18,7 @@ $password = 'password'
$erp5 = "https://example.com/erp5" # erp5 root $erp5 = "https://example.com/erp5" # erp5 root
$streamtool = "#{$erp5}/portal_input_data_streams" # where Input Stream Tool is mounted $streamtool = "#{$erp5}/portal_input_data_streams" # where Input Stream Tool is mounted
$wendelin = WendelinClient.new($streamtool, {'user' => $user, 'password' => $password}) $wendelin = WendelinClient.new($streamtool, {'user' => $user, 'password' => $password}, log)
def ingest(input_stream_ref, data_chunk) def ingest(input_stream_ref, data_chunk)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment