Module: Contrast::Agent::Reporting::Resend

Included in:
ReporterClientUtils
Defined in:
lib/contrast/agent/reporting/reporting_utilities/resend.rb

Overview

Module to house the Resend logic, when there is no response from TS.

Defined Under Namespace

Classes: Status

Constant Summary collapse

RESCUE_ATTEMPTS =

How manny times the client will try to rescue from above error before raising an error to reflect the events.

3
TIMEOUT =
5
RETRY_ERRORS =
[
  Net::OpenTimeout, Net::ReadTimeout, EOFError, OpenSSL::SSL::SSLError, Resolv::ResolvError,
  Errno::ECONNRESET, Errno::ECONNREFUSED, Errno::ETIMEDOUT, Errno::ESHUTDOWN, Errno::EHOSTDOWN, NameError,
  Errno::EHOSTUNREACH, Errno::EISCONN, Errno::ECONNABORTED, Errno::ENETRESET, Errno::ENETUNREACH, SocketError,
  NameError, NoMethodError, Timeout::Error, RuntimeError
].cs__freeze
RESENDING_MESSAGE =
'[Reporter] Resending message...'
END_RESENDING_MESSAGE =
'[Reporter] Reporter tried to resend event and received error:'

Instance Method Summary collapse

Instance Method Details

#disable_reporting(event, error) ⇒ NilClass

Disable reporting when there is an error that cannot be handled.

Parameters:

Returns:

  • (NilClass)


107
108
109
110
# File 'lib/contrast/agent/reporting/reporting_utilities/resend.rb', line 107

def disable_reporting event, error
  logger.error('[Agent] Disabling Reporting...', error: error.message, event_type: event.cs__class&.cs__name)
  Contrast::AGENT.disable!
end

#end_of_rescue(event, error) ⇒ NilClass

End of rescue attempts.

Parameters:

Returns:

  • (NilClass)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/contrast/agent/reporting/reporting_utilities/resend.rb', line 87

def end_of_rescue event, error
  if Contrast::Agent::Reporting::ReporterClientUtils::STARTUP_EVENTS.include?(event.cs__class)
    # Agent didn't send it's startup events, There will be reporting errors, disable reporting.
    disable_reporting(event, error)
  else
    # There is an error in one of the reported messages, log that error and continue.
    logger.error(END_RESENDING_MESSAGE,
                 resend_attempts: Contrast::Agent::Reporting::Resend::RESCUE_ATTEMPTS,
                 connection_error: error,
                 client: Contrast::Agent::Reporting::ReporterClient::SERVICE_NAME,
                 event_id: event&.__id__,
                 event_type: event&.cs__class&.cs__name)
  end
end

#handle_resend(event, connection) ⇒ Net::HTTPResponse?

This method will handle the error and will decide if we need to resend the event

Parameters:

Returns:

  • (Net::HTTPResponse, nil)

    response from TS



50
51
52
53
54
55
56
57
58
59
# File 'lib/contrast/agent/reporting/reporting_utilities/resend.rb', line 50

def handle_resend event, connection
  if sleep?
    logger.debug("[Reporter] sleeping for #{ TIMEOUT } seconds before resending...")
    Thread.current.send(:sleep, timeout)
    wake_up
  end
  response = mode.status == mode.resending ? send_event(event, connection) : nil
  response_success! if response
  response
end

#handle_response_error(event, connection, error) ⇒ Object

Handles errors that occurs before the ResponseHandler do not have a response, or response code to hande. This Errors requires different handling, because most of errors here occurs when the response cannot be read, there is open ssl error, or a certain Timeout is reached.

Parameters:



71
72
73
74
75
76
77
78
79
80
# File 'lib/contrast/agent/reporting/reporting_utilities/resend.rb', line 71

def handle_response_error event, connection, error
  return end_of_rescue(event, error) if mode.resend.rescue_attempts >= RESCUE_ATTEMPTS

  if RETRY_ERRORS.include?(error.cs__class)
    mode.resend.increase_rescue_attempts
    try_resend(event, connection, error)
  else
    end_of_rescue(event, error)
  end
end

#try_resend(event, connection, error) ⇒ Net::HTTPResponse?

This method is mainly used in inside error handling and startup mesasges sending.

Parameters:

Returns:

  • (Net::HTTPResponse, nil)

    response from TS



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/contrast/agent/reporting/reporting_utilities/resend.rb', line 29

def try_resend event, connection, error
  # The timeout for agent to sleep is set here, we don't need to raise an error,
  # but also 15 min is a long time to wait for example if event is startup message.
  # Override timeout
  if Contrast::Agent::Reporting::ReporterClientUtils::STARTUP_EVENTS.include?(event.cs__class)
    response_handler.suspend_reporting(RESENDING_MESSAGE,
                                       TIMEOUT,
                                       error,
                                       event: event,
                                       log_error: false,
                                       startup: true)
  end
  mode.enter_resend_mode
  handle_resend(event, connection)
end