From 1cf7cd996de27d244e121189615685aaa76d4b73 Mon Sep 17 00:00:00 2001 From: "Jon R. Humphrey" Date: Thu, 13 Feb 2025 14:03:25 +0000 Subject: [PATCH] refactor: Remove logging and clean up response handling - Removed custom logger for Faraday responses. - Simplified the `instrument_response` method by removing unused parameters. - Updated logging fields to streamline request time calculations. - Cleaned up commented-out code for better readability. --- lib/data_services_api/service.rb | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/lib/data_services_api/service.rb b/lib/data_services_api/service.rb index d60fe8d..5b1d6c6 100644 --- a/lib/data_services_api/service.rb +++ b/lib/data_services_api/service.rb @@ -146,8 +146,6 @@ def create_http_connection(http_url, auth = false) # instrument the request to log the time it takes to complete faraday.request :instrumentation, name: 'requests.api' - # faraday.response :logger, FaradayCustomLogger, { headers: true, bodies: true, errors: true } - # set the basic auth if required faraday.basic_auth(api_user, api_pw) if auth @@ -189,7 +187,7 @@ def report_json_failure(json) throw msg end - def instrument_response(response, start_time, request_status) # rubocop:disable Metrics/MethodLength + def instrument_response(response, start_time, _request_status) # immediately log the time the response was received in microseconds end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond) # calculate the elapsed time in milliseconds by dividing the difference in time by 1000 @@ -242,7 +240,7 @@ def in_rails? defined?(Rails) end - # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity, Metrics/ParameterLists, Metrics/AbcSize, Layout/LineLength + # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity, Metrics/AbcSize, Layout/LineLength # Log the provided properties with the appropriate log level # @param [Hash] log_fields - The fields to log # @param [String] log_fields.message - The message to log @@ -258,19 +256,13 @@ def log_message(log_fields, log_type = 'info') # immediately log the receipt time of the response in miroseconds end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond) + # calculate the elapsed time in milliseconds by dividing the difference in time by 1000 + duration = (end_time - log_fields[:start_time]) / 1000 # parse out the optional parameters and set defaults log_fields[:message] ||= log_fields[:response]&.body - # log_fields[:request_url] ||= log_fields[:response]&.env.url.to_s + log_fields[:request_time] ||= duration log_fields[:request_status] ||= 'completed' if log_fields[:status] == 200 - log_fields[:start_time] ||= 0 log_fields[:status] ||= 200 - # calculate the elapsed time in milliseconds by dividing the difference in time by 1000 - duration = (end_time - log_fields[:start_time]) / 1000 - elapsed_time = format('%.0f ms', duration) - # add elapsed time to the message if the api request is completed - # if log_fields[:request_status].present? - # log_fields[:message] ||= "#{log_fields[:request_status]} Data Services API request in #{elapsed_time}" - # end # Log the API responses at the appropriate level requested case log_type @@ -285,6 +277,6 @@ def log_message(log_fields, log_type = 'info') end logger.flush if logger.respond_to?(:flush) end - # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity, Metrics/ParameterLists, Metrics/AbcSize, Layout/LineLength + # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity, Metrics/AbcSize, Layout/LineLength end end