Module: TingYun::Agent::InstanceMethods::Connect

Includes:
HandleErrors
Included in:
TingYun::Agent::InstanceMethods
Defined in:
lib/ting_yun/agent/instance_methods/connect.rb

Instance Method Summary collapse

Methods included from HandleErrors

#handle_delay_restart, #handle_force_disconnect, #handle_force_restart, #handle_license_error, #handle_other_error, #handle_server_error, #handle_unrecoverable_agent_error, #log_error

Instance Method Details

#catch_errorsObject

  • :keep_retrying => false to only try to connect once, and return with the connection set to nil. This ensures we may try again later (default true).

  • force_reconnect => true if you want to establish a new connection to the server before running the worker loop. This means you get a separate agent run and Ting Yun sees it as a separate instance (default is false).



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 105

def catch_errors
  yield
rescue  TingYun::Support::Exception::UnKnownServerException => e
  handle_force_restart(e)
  retry
rescue TingYun::Support::Exception::ServerConnectionException => e
  handle_delay_restart(e, 60)
  retry
rescue => e
  handle_delay_restart(e, 60)
  retry
end

#connect_in_syncObject



150
151
152
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 150

def connect_in_sync
  TingYun::Agent.disable_all_tracing { connect!(:keep_retrying => false) }
end

#connect_settingsObject

Initializes the hash of settings that we send to the server. Returns a literal hash containing the options



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 70

def connect_settings
  sanitize_environment_report
  settings = {
      :pid => $$,
      :port => ::TingYun::Agent.config[:port],
      :host => local_host,
      :appName => ::TingYun::Agent.config.app_names,
      :language => 'Ruby',
      :agentVersion => ::TingYun::VERSION::STRING,
      :env => @environment_report,
      :config => ::TingYun::Agent.config.to_collector_hash
  }
  settings
end

#connect_to_serverObject

Returns connect data passed back from the server



90
91
92
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 90

def connect_to_server
  @service.connect(connect_settings)
end

#connected?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 28

def connected?
  @connect_state == :connected
end

#disconnectObject

Disconnect just sets connected to false, which prevents the agent from trying to connect again



23
24
25
26
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 23

def disconnect
  @connect_state = :disconnected
  true
end

#disconnected?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 32

def disconnected?
  @connect_state == :disconnected
end

#environment_for_connectObject

Checks whether we should send environment info, and if so, returns the snapshot from the local environment. Generating the EnvironmentReport has the potential to trigger require calls in Rails environments, so this method should only be called synchronously from on the main thread.



63
64
65
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 63

def environment_for_connect
  ::TingYun::Agent.config[:send_environment_info] ? TingYun::EnvironmentReport.new.data : {}
end

#finish_setup(config_data) ⇒ Object

Takes a hash of configuration data returned from the server and uses it to set local variables and to initialize various parts of the agent that are configured separately.



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 123

def finish_setup(config_data)
  return if config_data == nil

  if config_data['config']
    ::TingYun::Agent.logger.debug "Using config from server"
  end
  ::TingYun::Agent.logger.debug "Server provided config: #{config_data.inspect}"
  server_config = TingYun::Configuration::ServerSource.new(config_data)
  ::TingYun::Agent.config.replace_or_add_config(server_config)
  #log_connection!(config_data)
end

#generate_environment_reportObject



44
45
46
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 44

def generate_environment_report
  @environment_report = environment_for_connect
end

#local_hostObject



85
86
87
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 85

def local_host
  TingYun::Support::Hostname.get
end

#log_collector_messages(messages) ⇒ Object



144
145
146
147
148
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 144

def log_collector_messages(messages)
  messages.each do |message|
    ::TingYun::Agent.logger.send(message['level'].downcase, message['message'])
  end
end

#log_connection!(config_data) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 135

def log_connection!(config_data)
  ::TingYun::Agent.logger.debug "Connected to TingYun Service at #{@service.collector.name}"
  ::TingYun::Agent.logger.debug "Application Run       = #{@service.applicationId}."
  ::TingYun::Agent.logger.debug "Connection data = #{config_data.inspect}"
  if config_data['messages'] && config_data['messages'].any?
    log_collector_messages(config_data['messages'])
  end
end

#query_server_for_configurationObject

merge server config



95
96
97
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 95

def query_server_for_configuration
  finish_setup(connect_to_server)
end

#sanitize_environment_reportObject

We’ve seen objects in the environment report (Rails.env in particular) that can’t seralize to JSON. Cope with that here and clear out so downstream code doesn’t have to check again.



51
52
53
54
55
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 51

def sanitize_environment_report
  if !@service.valid_to_marshal?(@environment_report)
    @environment_report = {}
  end
end

#should_connect?(force = false) ⇒ Boolean

Don’t connect if we’re already connected, or if we tried to connect and were rejected with prejudice because of a license issue, unless we’re forced to by force_reconnect.

Returns:

  • (Boolean)


39
40
41
# File 'lib/ting_yun/agent/instance_methods/connect.rb', line 39

def should_connect?(force=false)
  force || (!connected? && !disconnected?)
end