Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debug: intermittent network issues #58

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,82 @@

puts "=" * 80
end


module KnapsackPro
module Client
module ConnectionPatch
def build_http(uri)
http = super(uri)

FileUtils.mkdir_p('tmp')
log_file = 'tmp/knapsack_http_requests.log'
http.set_debug_output(Logger.new(log_file))

#http.set_debug_output($stdout) # uncomment if you prefer to log all requests to stdout

http
end

def make_request(&block)
retries ||= 0

@http_response = block.call
@response_body = parse_response_body(http_response.body)

request_uuid = http_response.header['X-Request-Id'] || 'N/A'

logger.debug("#{action.http_method.to_s.upcase} #{endpoint_url}")
logger.debug("API request UUID: #{request_uuid}")
logger.debug("Test suite split seed: #{seed}") if has_seed?
logger.debug('API response:')
if errors?
logger.error(response_body)
else
logger.debug(response_body)
end

if server_error?
raise KnapsackPro::Client::Connection::ServerError.new(response_body)
end

response_body
rescue KnapsackPro::Client::Connection::ServerError, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EPIPE, EOFError, SocketError, Net::OpenTimeout, Net::ReadTimeout, OpenSSL::SSL::SSLError => e
logger.warn("#{action.http_method.to_s.upcase} #{endpoint_url}")
logger.warn('Request failed due to:')
logger.warn(e.inspect)
retries += 1
if retries < max_request_retries
wait = retries * KnapsackPro::Client::Connection::REQUEST_RETRY_TIMEBOX
print_every = 2 # seconds
(wait / print_every).ceil.times do |i|
if i == 0
logger.warn("Wait for #{wait}s before retrying the request to Knapsack Pro API.")
else
logger.warn("#{wait - i * print_every}s left before retry...")
end
Kernel.sleep(print_every)
end

logger.warn('The request is going to be retried.')
curl_trace_api
curl_trace_google

retry
else
response_body
end
end

def curl_trace_api
`curl --trace tmp/knapsack_curl_trace.log https://api.knapsackpro.com`
end

def curl_trace_google
`curl --trace tmp/google_curl_trace.log https://google.com`
end
end
end
end

KnapsackPro::Client::Connection.prepend(KnapsackPro::Client::ConnectionPatch)
Loading