Skip to content

Commit

Permalink
Merge pull request #251 from oleveau/ImproveLogging
Browse files Browse the repository at this point in the history
Add optional trace header logging
  • Loading branch information
iulian03 authored Apr 17, 2024
2 parents 8a79913 + f3d461c commit 514b725
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
18 changes: 16 additions & 2 deletions lib/mangopay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'benchmark'
require 'logger'
require 'time'
require 'securerandom'

# helpers
require 'mangopay/version'
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/mangopay/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module MangoPay
VERSION = '3.24.1'
VERSION = '3.25.0'
end

0 comments on commit 514b725

Please sign in to comment.