Skip to content

Commit

Permalink
Merge pull request #17635 from Fryguy/refactor_loggers_instrument
Browse files Browse the repository at this point in the history
Extract Vmdb::Loggers::Instrument to its own file
  • Loading branch information
carbonin authored Jun 25, 2018
2 parents e7248a6 + a59843a commit 33d994a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 66 deletions.
67 changes: 1 addition & 66 deletions lib/vmdb/loggers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,71 +11,6 @@ def self.rails_logger
end

module Loggers
module Instrument
# To be used as Excon's request logger, the logger must respond to
# #instrument as in ActiveSupport::Notifications.
# Implementation derived from Excon::StandardInstrumentor
def instrument(name, params = {})
method, message =
case name
when "excon.request" then [:debug, message_for_excon_request(params)]
when "excon.response" then [:debug, message_for_excon_response(params)]
when "excon.error" then [:debug, message_for_excon_error(params)]
else [:debug, message_for_other(params)]
end

send(method, "#{name.ljust(14)} #{message}")
yield if block_given?
end

private

def message_for_excon_request(params)
uri_parts = params.values_at(:scheme, nil, :host, :port, nil, :path, nil, nil, nil)
uri_parts[3] = uri_parts[3].to_i if uri_parts[3] # port
uri = {:uri => URI::Generic.build(uri_parts).to_s}
log_params(uri.merge!(params.slice(:query, :method, :headers, :body).delete_nils))
end

def message_for_excon_response(params)
log_params(params.slice(:status, :headers, :body))
end

def message_for_excon_error(params)
params[:error].pretty_inspect
end

def message_for_other(params)
log_params(params.except(:instrumentor, :instrumentor_name, :connection, :stack, :middlewares))
end

def log_params(params)
sanitized = sanitize_params(params)
sanitized[:body] = parse_body(sanitized[:body])
"\n#{sanitized.pretty_inspect}"
end

def parse_body(body)
JSON.parse(body) if body
rescue JSON::ParserError
body
end

def sanitize_params(params)
if params.key?(:headers) && params[:headers].key?('Authorization')
params[:headers] = params[:headers].dup
params[:headers]['Authorization'] = "********"
end
if params.key?(:password)
params[:password] = "********"
end
if params.key?(:body)
params[:body] = params[:body].to_s.gsub(/"password":".+?"\}/, '"password":"********"}')
end
params
end
end

def self.init
return if @initialized
create_loggers
Expand Down Expand Up @@ -152,7 +87,6 @@ def self.configure_external_loggers
end
private_class_method :configure_external_loggers


def self.apply_config_value(config, logger, key)
old_level = logger.level
new_level_name = (config[key] || "INFO").to_s.upcase
Expand All @@ -166,4 +100,5 @@ def self.apply_config_value(config, logger, key)
end
end

require_relative "loggers/instrument"
Dir.glob(File.join(File.dirname(__FILE__), "loggers", "*")).each { |f| require f }
68 changes: 68 additions & 0 deletions lib/vmdb/loggers/instrument.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module Vmdb
module Loggers
module Instrument
# To be used as Excon's request logger, the logger must respond to
# #instrument as in ActiveSupport::Notifications.
# Implementation derived from Excon::StandardInstrumentor
def instrument(name, params = {})
method, message =
case name
when "excon.request" then [:debug, message_for_excon_request(params)]
when "excon.response" then [:debug, message_for_excon_response(params)]
when "excon.error" then [:debug, message_for_excon_error(params)]
else [:debug, message_for_other(params)]
end

send(method, "#{name.ljust(14)} #{message}")
yield if block_given?
end

private

def message_for_excon_request(params)
uri_parts = params.values_at(:scheme, nil, :host, :port, nil, :path, nil, nil, nil)
uri_parts[3] = uri_parts[3].to_i if uri_parts[3] # port
uri = {:uri => URI::Generic.build(uri_parts).to_s}
log_params(uri.merge!(params.slice(:query, :method, :headers, :body).delete_nils))
end

def message_for_excon_response(params)
log_params(params.slice(:status, :headers, :body))
end

def message_for_excon_error(params)
params[:error].pretty_inspect
end

def message_for_other(params)
log_params(params.except(:instrumentor, :instrumentor_name, :connection, :stack, :middlewares))
end

def log_params(params)
sanitized = sanitize_params(params)
sanitized[:body] = parse_body(sanitized[:body])
"\n#{sanitized.pretty_inspect}"
end

def parse_body(body)
JSON.parse(body) if body
rescue JSON::ParserError
body
end

def sanitize_params(params)
if params.key?(:headers) && params[:headers].key?('Authorization')
params[:headers] = params[:headers].dup
params[:headers]['Authorization'] = "********"
end
if params.key?(:password)
params[:password] = "********"
end
if params.key?(:body)
params[:body] = params[:body].to_s.gsub(/"password":".+?"\}/, '"password":"********"}')
end
params
end
end
end
end

0 comments on commit 33d994a

Please sign in to comment.