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
credentials['user'] = @user
credentials['password'] = @password
end
@wendelin = WendelinClient.new(@streamtool_uri, credentials)
@wendelin = WendelinClient.new(@streamtool_uri, credentials, @log)
end
def start
......
......@@ -19,18 +19,17 @@
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
......@@ -48,15 +47,15 @@ class WendelinClient
# 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
......@@ -73,23 +72,23 @@ class WendelinClient
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
......
......@@ -5,6 +5,10 @@
here = File.dirname(__FILE__)
$LOAD_PATH << File.expand_path(File.join(here, '../lib'))
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
$user = 'user'
......@@ -14,7 +18,7 @@ $password = 'password'
$erp5 = "https://example.com/erp5" # erp5 root
$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)
......
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