module TingYun::Agent::InstanceMethods::Connect

Public Instance Methods

catch_errors() { || ... } click to toggle source
  • :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).

# File lib/ting_yun/agent/instance_methods/connect.rb, line 115
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, 30)
  retry
rescue => e
  handle_delay_restart(e, 30)
  retry
end
connect_in_sync() click to toggle source
# File lib/ting_yun/agent/instance_methods/connect.rb, line 162
def connect_in_sync
  TingYun::Agent.disable_all_tracing { connect!(:keep_retrying => false) }
end
connect_settings() click to toggle source

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

# File lib/ting_yun/agent/instance_methods/connect.rb, line 71
def connect_settings
  sanitize_environment_report
  settings = {
      :host => local_host,
      :port => ::TingYun::Agent.config[:port],
      :instanceName => ::TingYun::Agent.config[:'instance.name'],
      :appName => ::TingYun::Agent.config[:app_name],
      :language => 'Ruby',
      :agentVersion => TingYun::VERSION::STRING,
      :firstRun  => @connect_state==:first,
      :oneAgentUuid=> 'oneAgentUuid',
      :environment => {
          :meta => {
              :pid => $$,
              :agentVersion => ::TingYun::VERSION::STRING,
              :readonly => true
          },
          :env => @environment_report,
          :config => ::TingYun::Agent.config.to_collector_hash
      }
  }
  settings
end
connect_to_server() click to toggle source

Returns connect data passed back from the server

# File lib/ting_yun/agent/instance_methods/connect.rb, line 100
def connect_to_server
  @service.connect(connect_settings)
end
connected?() click to toggle source
# File lib/ting_yun/agent/instance_methods/connect.rb, line 29
def connected?
  @connect_state == :connected
end
disconnect() click to toggle source

Disconnect just sets connected to false, which prevents the agent from trying to connect again @connect_state has three state {:disconnected,:pending,:connected}

# File lib/ting_yun/agent/instance_methods/connect.rb, line 24
def disconnect
  @connect_state = :disconnected
  true
end
disconnected?() click to toggle source
# File lib/ting_yun/agent/instance_methods/connect.rb, line 33
def disconnected?
  @connect_state == :disconnected
end
environment_for_connect() click to toggle source

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.

# File lib/ting_yun/agent/instance_methods/connect.rb, line 64
def environment_for_connect
  ::TingYun::Agent.config[:send_environment_info] ? TingYun::EnvironmentReport.new.data : {}
end
finish_setup(config_data) click to toggle source

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.

# File lib/ting_yun/agent/instance_methods/connect.rb, line 135
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_report() click to toggle source
# File lib/ting_yun/agent/instance_methods/connect.rb, line 45
def generate_environment_report
  @environment_report = environment_for_connect
end
local_host() click to toggle source
# File lib/ting_yun/agent/instance_methods/connect.rb, line 95
def local_host
  TingYun::Support::Hostname.get
end
log_collector_messages(messages) click to toggle source
# File lib/ting_yun/agent/instance_methods/connect.rb, line 156
def log_collector_messages(messages)
  messages.each do |message|
    ::TingYun::Agent.logger.send(message['level'].downcase, message['message'])
  end
end
log_connection!(config_data) click to toggle source
# File lib/ting_yun/agent/instance_methods/connect.rb, line 147
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_configuration() click to toggle source

merge server config

# File lib/ting_yun/agent/instance_methods/connect.rb, line 105
def query_server_for_configuration
  finish_setup(connect_to_server)
end
sanitize_environment_report() click to toggle source

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.

# File lib/ting_yun/agent/instance_methods/connect.rb, line 52
def sanitize_environment_report
  if !@service.valid_to_marshal?(@environment_report)
    @environment_report = {}
  end
end
should_connect?(force=false) click to toggle source

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.

# File lib/ting_yun/agent/instance_methods/connect.rb, line 40
def should_connect?(force=false)
  force || (!connected? && !disconnected?)
end