Skip to content

Commit

Permalink
refactor: Remove logging and clean up response handling
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
jonrandahl committed Feb 13, 2025
1 parent 290f852 commit 1cf7cd9
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions lib/data_services_api/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 1cf7cd9

Please sign in to comment.