From 8d886c708c5a294d072a338052ed40c51293707a Mon Sep 17 00:00:00 2001 From: Pawel Chojnacki Date: Fri, 17 Aug 2018 16:37:40 +0200 Subject: [PATCH] Log consecutive errors once --- lib/ddtrace/transport.rb | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/ddtrace/transport.rb b/lib/ddtrace/transport.rb index 34b70719179..8e221ae559c 100644 --- a/lib/ddtrace/transport.rb +++ b/lib/ddtrace/transport.rb @@ -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 @@ -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? @@ -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 @@ -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)