Skip to content

Commit

Permalink
Improving the readability of the Ruby logs.
Browse files Browse the repository at this point in the history
Change-Id: Ib9768d3f0e358575172ccc419550457e9ea63e3b
  • Loading branch information
mcloonan committed Aug 15, 2024
1 parent e42ff7f commit 4569c4b
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions lib/google/ads/google_ads/interceptors/logging_interceptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ def request_response(request:, call:, method:, metadata: {})
response = yield

@logger.info { build_summary_message(request, call, method, false) }
@logger.debug { build_request_message(metadata, request) }
@logger.debug { build_request_message(request, call, method, metadata) }
@logger.debug { build_success_response_message(response) }
if response.respond_to?(:partial_failure_error) && response.partial_failure_error
@logger.debug { build_partial_failure_message(response) }
end
response
rescue Exception
@logger.warn { build_summary_message(request, call, method, true) }
@logger.info { build_request_message(metadata, request) }
@logger.info { build_request_message(request, call, method, metadata) }
@logger.info { build_error_response_message }
raise
end
Expand All @@ -77,7 +77,7 @@ def server_streamer(request:, call:, method:, metadata: {})
responses = yield
Enumerator.new do |y|
responses.each { |response|
@logger.debug { build_request_message(metadata, request) }
@logger.debug { build_request_message(request, call, method, metadata) }
@logger.debug { build_success_response_message(response) }
if response.respond_to?(:partial_failure_error) && response.partial_failure_error
@logger.debug { build_partial_failure_message(response) }
Expand All @@ -103,7 +103,7 @@ def server_streamer(request:, call:, method:, metadata: {})

def handle_error(request, call, method, metadata)
@logger.warn { build_summary_message(request, call, method, true) }
@logger.info { build_request_message(metadata, request) }
@logger.info { build_request_message(request, call, method, metadata) }
@logger.info { build_error_response_message }
raise
end
Expand Down Expand Up @@ -155,10 +155,11 @@ def error_details(exception)
end

def build_success_response_message(response)
"Incoming response: Payload: #{sanitize_message(response).to_json}"
"\nResponse\n--------\n" \
"Response: #{sanitize_message(response).to_json}"
end

def build_request_message(metadata, request)
def build_request_message(request, call, method, metadata)
# calling #to_json on some protos (specifically those with non-UTF8
# encodable byte values) causes a segfault, however #inspect works
# so we check if the proto contains a bytevalue, and if it does
Expand All @@ -168,8 +169,11 @@ def build_request_message(metadata, request)
else
sanitize_message(request).to_json
end
"Outgoing request: Headers: #{sanitize_headers(metadata).to_json} " \
"Payload: #{request_inspect}"
"\nRequest\n-------\n" \
"Method: #{method}\n" \
"Host: #{call.instance_variable_get(:@wrapped).instance_variable_get(:@peer)}\n" \
"Headers: #{sanitize_headers(metadata).to_json}\n" \
"Request: #{request_inspect}"
end

def sanitize_headers(metadata)
Expand Down

0 comments on commit 4569c4b

Please sign in to comment.