Skip to content

Commit

Permalink
Merge pull request #515 from DataDog/fix/post_data_log_connection_as_…
Browse files Browse the repository at this point in the history
…debug

Log consecutive errors in Transport only once
  • Loading branch information
delner authored Aug 21, 2018
2 parents 42dc839 + 8d886c7 commit d9691a1
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions lib/ddtrace/transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def initialize(hostname, port, options = {})
@count_client_error = 0
@count_server_error = 0
@count_internal_error = 0
@count_consecutive_errors = 0
end

# route the send to the right endpoint
Expand Down Expand Up @@ -109,7 +110,7 @@ def post(url, data, count = nil)
response = Net::HTTP.start(@hostname, @port, read_timeout: TIMEOUT) { |http| http.request(request) }
handle_response(response)
rescue StandardError => e
Datadog::Tracer.log.error(e.message)
log_error_once(e.message)
500
end.tap do
yield(response) if block_given?
Expand Down Expand Up @@ -170,21 +171,22 @@ def handle_response(response)

if success?(status_code)
Datadog::Tracer.log.debug('Payload correctly sent to the trace agent.')
@mutex.synchronize { @count_consecutive_errors = 0 }
@mutex.synchronize { @count_success += 1 }
elsif downgrade?(status_code)
Datadog::Tracer.log.debug("calling the endpoint but received #{status_code}; downgrading the API")
elsif client_error?(status_code)
Datadog::Tracer.log.error("Client error: #{response.message}")
log_error_once("Client error: #{response.message}")
@mutex.synchronize { @count_client_error += 1 }
elsif server_error?(status_code)
Datadog::Tracer.log.error("Server error: #{response.message}")
@mutex.synchronize { @count_server_error += 1 }
log_error_once("Server error: #{response.message}")
end

status_code
rescue StandardError => e
Datadog::Tracer.log.error(e.message)
log_error_once(e.message)
@mutex.synchronize { @count_internal_error += 1 }

500
end

Expand All @@ -201,6 +203,16 @@ def stats

private

def log_error_once(*args)
if @count_consecutive_errors > 0
Datadog::Tracer.log.debug(*args)
else
Datadog::Tracer.log.error(*args)
end

@mutex.synchronize { @count_consecutive_errors += 1 }
end

def process_callback(action, response)
return unless @response_callback && @response_callback.respond_to?(:call)

Expand Down

0 comments on commit d9691a1

Please sign in to comment.