From 6cb704ad75868f6ee7af91e3ad4276a3948b28cf Mon Sep 17 00:00:00 2001 From: Olivier Leveau Date: Tue, 16 Apr 2024 11:23:09 +0200 Subject: [PATCH 1/2] Improve logging Bump version Bump version --- CHANGELOG.md | 5 +++++ lib/mangopay.rb | 35 +++++++++++++++++++++++++++++++++-- lib/mangopay/version.rb | 2 +- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a99b436..dcdef47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## [3.25.0] - 2024-04-16 +### Added + +- Add trace header logging : Introduced the `log_trace_headers` boolean configuration key. Set it to true to enable logging of `x_mangopay_trace-id` and `IdempotencyKey` in the log of the http requests. + ## [3.24.1] - 2024-04-10 ### Fixed diff --git a/lib/mangopay.rb b/lib/mangopay.rb index f29a627..a7cdfd0 100644 --- a/lib/mangopay.rb +++ b/lib/mangopay.rb @@ -5,6 +5,7 @@ require 'benchmark' require 'logger' require 'time' +require 'securerandom' # helpers require 'mangopay/version' @@ -55,7 +56,7 @@ module MangoPay class Configuration attr_accessor :preproduction, :root_url, :client_id, :client_apiKey, - :temp_dir, :log_file, :http_timeout, + :temp_dir, :log_file, :log_trace_headers, :http_timeout, :http_max_retries, :http_open_timeout, :logger, :use_ssl, :uk_header_flag @@ -65,6 +66,7 @@ def apply_configuration config.client_id = @client_id config.client_apiKey = @client_apiKey config.log_file = @log_file + config.log_trace_headers = @log_trace_headers config.http_timeout = @http_timeout config.http_max_retries = @http_max_retries config.http_open_timeout = @http_open_timeout @@ -101,6 +103,10 @@ def use_ssl? true end + + def log_trace_headers + @log_trace_headers || false + end def uk_header_flag @uk_header_flag || false @@ -274,6 +280,9 @@ def request_headers 'Authorization' => "#{auth_token['token_type']} #{auth_token['access_token']}", 'Content-Type' => 'application/json' } + if configuration.log_trace_headers + headers.update('x_mangopay_trace-id' => SecureRandom.uuid) + end begin headers.update('x_mangopay_client_user_agent' => JSON.dump(user_agent)) rescue => e @@ -292,7 +301,12 @@ def do_request(http, req, uri) def do_request_with_log(http, req, uri) res, time = nil, nil params = FilterParameters.request(req.body) - line = "[#{Time.now.iso8601}] #{req.method.upcase} \"#{uri.to_s}\" #{params}" + if configuration.log_trace_headers + trace_headers = JSON.dump({ 'Idempotency-Key' => req['Idempotency-Key'] , 'x_mangopay_trace-id' => req['x_mangopay_trace-id'] }) + line = "[#{Time.now.iso8601}] #{req.method.upcase} \"#{uri.to_s}\" #{params} #{trace_headers}" + else + line = "[#{Time.now.iso8601}] #{req.method.upcase} \"#{uri.to_s}\" #{params}" + end begin time = Benchmark.realtime { begin @@ -348,5 +362,22 @@ def logger def logs_required? !configuration.log_file.nil? || !configuration.logger.nil? end + + def filter_headers(hash, to_filter) + hash.each do |k,v| + if v.is_a?(Hash) + filter_hash(v, to_filter) + else + hash[k] = '[FILTERED]' if to_filter.include?(k) + end + end + end + + def trace_headers_keys + @@trace_headers_keys ||= [ + 'Idempotency-Key', 'Traceparent', 'trace-id', 'X-Datadog-Trace-Id', 'X-Datadog-Parent-Id', + + ].freeze + end end end diff --git a/lib/mangopay/version.rb b/lib/mangopay/version.rb index db8b59c..079eef0 100644 --- a/lib/mangopay/version.rb +++ b/lib/mangopay/version.rb @@ -1,3 +1,3 @@ module MangoPay - VERSION = '3.24.1' + VERSION = '3.25.0' end From f3d461c9c91aa919b8f2c5370517eb44c83668f9 Mon Sep 17 00:00:00 2001 From: Olivier Leveau Date: Tue, 16 Apr 2024 16:29:36 +0200 Subject: [PATCH 2/2] Improve logging --- lib/mangopay.rb | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/lib/mangopay.rb b/lib/mangopay.rb index a7cdfd0..6a2c275 100644 --- a/lib/mangopay.rb +++ b/lib/mangopay.rb @@ -362,22 +362,5 @@ def logger def logs_required? !configuration.log_file.nil? || !configuration.logger.nil? end - - def filter_headers(hash, to_filter) - hash.each do |k,v| - if v.is_a?(Hash) - filter_hash(v, to_filter) - else - hash[k] = '[FILTERED]' if to_filter.include?(k) - end - end - end - - def trace_headers_keys - @@trace_headers_keys ||= [ - 'Idempotency-Key', 'Traceparent', 'trace-id', 'X-Datadog-Trace-Id', 'X-Datadog-Parent-Id', - - ].freeze - end end end