Module: NewRelic::Agent::Agent::InstanceMethods::Connect

Included in:
NewRelic::Agent::Agent::InstanceMethods
Defined in:
lib/new_relic/agent/agent.rb

Overview

This module is an artifact of a refactoring of the connect method - all of its methods are used in that context, so it can be refactored at will. It should be fully tested

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connect_attemptsObject

number of attempts we’ve made to contact the server



617
618
619
# File 'lib/new_relic/agent/agent.rb', line 617

def connect_attempts
  @connect_attempts
end

#connect_retry_periodObject

the frequency with which we should try to connect to the server at the moment.



615
616
617
# File 'lib/new_relic/agent/agent.rb', line 615

def connect_retry_period
  @connect_retry_period
end

Instance Method Details

#apdex_fObject

apdex_f is always 4 times the apdex_t



740
741
742
# File 'lib/new_relic/agent/agent.rb', line 740

def apdex_f
  (4 * Agent.config[:apdex_t]).to_f
end

#connect_settingsObject

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



718
719
720
721
722
723
724
725
726
727
728
729
# File 'lib/new_relic/agent/agent.rb', line 718

def connect_settings
  {
    :pid => $$,
    :host => @local_host,
    :app_name => Agent.config.app_names,
    :language => 'ruby',
    :agent_version => NewRelic::VERSION::STRING,
    :environment => environment_for_connect,
    :settings => Agent.config.flattened_config,
    :validate => validate_settings
  }
end

#connect_to_serverObject

Does some simple logging to make sure that our seed and token for verification are correct, then returns the connect data passed back from the server



734
735
736
737
# File 'lib/new_relic/agent/agent.rb', line 734

def connect_to_server
  log_seed_token
  @service.connect(connect_settings)
end

#disconnectObject

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



621
622
623
624
# File 'lib/new_relic/agent/agent.rb', line 621

def disconnect
  @connected = false
  true
end

#environment_for_connectObject

Checks whether we should send environment info, and if so, returns the snapshot from the local environment



701
702
703
# File 'lib/new_relic/agent/agent.rb', line 701

def environment_for_connect
  Agent.config[:send_environment_info] ? Control.instance.local_env.snapshot : []
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.

Can accommodate most arbitrary data - anything extra is ignored unless we say to do something with it here.



757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
# File 'lib/new_relic/agent/agent.rb', line 757

def finish_setup(config_data)
  return if config_data == nil

  @service.agent_id = config_data['agent_run_id'] if @service

  if config_data['agent_config']
    log.info "Using config from server"
  end

  log.debug "Server provided config: #{config_data.inspect}"
  server_config = NewRelic::Agent::Configuration::ServerSource.new(config_data)
  Agent.config.apply_config(server_config, 1)
  log_connection!(config_data) if @service

  @beacon_configuration = BeaconConfiguration.new
end

#get_retry_periodObject

Retry period is a minute for each failed attempt that we’ve made. This should probably do some sort of sane TCP backoff to prevent hammering the server, but a minute for each attempt seems to work reasonably well.



643
644
645
646
# File 'lib/new_relic/agent/agent.rb', line 643

def get_retry_period
  return 600 if self.connect_attempts > 6
  connect_attempts * 60
end

#handle_license_error(error) ⇒ Object

When the server sends us an error with the license key, we want to tell the user that something went wrong, and let them know where to go to get a valid license key

After this runs, it disconnects the agent so that it will no longer try to connect to the server, saving the application and the server load



684
685
686
687
688
# File 'lib/new_relic/agent/agent.rb', line 684

def handle_license_error(error)
  log.error error.message
  log.info "Visit NewRelic.com to obtain a valid license key, or to upgrade your account."
  disconnect
end

#increment_retry_period!Object

:nodoc:



648
649
650
# File 'lib/new_relic/agent/agent.rb', line 648

def increment_retry_period! #:nodoc:
  self.connect_retry_period=(get_retry_period)
end

#log_collector_messages(messages) ⇒ Object



785
786
787
788
789
# File 'lib/new_relic/agent/agent.rb', line 785

def log_collector_messages(messages)
  messages.each do |message|
    log.send(message['level'].downcase.to_sym, message['message'])
  end
end

#log_connection!(config_data) ⇒ Object

Logs when we connect to the server, for debugging purposes

  • makes sure we know if an agent has not connected



776
777
778
779
780
781
782
783
# File 'lib/new_relic/agent/agent.rb', line 776

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

#log_error(error) ⇒ Object

When we have a problem connecting to the server, we need to tell the user what happened, since this is not an error we can handle gracefully.



672
673
674
675
# File 'lib/new_relic/agent/agent.rb', line 672

def log_error(error)
  log.error "Error establishing connection with New Relic Service at #{control.server}: #{error.message}"
  log.debug error.backtrace.join("\n")
end

#log_seed_tokenObject

If we are using a seed and token to validate the agent, we should debug log that fact so that debug logs include a clue that token authentication is what will be used



693
694
695
696
697
# File 'lib/new_relic/agent/agent.rb', line 693

def log_seed_token
  if Agent.config[:validate_seed]
    log.debug "Connecting with validation seed/token: #{Agent.config[:validate_seed]}/#{Agent.config[:validate_token]}"
  end
end

#query_server_for_configurationObject

Sets the collector host and connects to the server, then invokes the final configuration with the returned data



746
747
748
# File 'lib/new_relic/agent/agent.rb', line 746

def query_server_for_configuration
  finish_setup(connect_to_server)
end

#should_keep_retrying?(options) ⇒ Boolean

We keep trying by default, but you can disable it with the :keep_retrying option set to false

Returns:

  • (Boolean)


635
636
637
# File 'lib/new_relic/agent/agent.rb', line 635

def should_keep_retrying?(options)
  @keep_retrying = (options[:keep_retrying].nil? || options[:keep_retrying])
end

#should_retry?Boolean

We should only retry when there has not been a more serious condition that would prevent it. We increment the connect attempts and the retry period, to prevent constant connection attempts, and tell the user what we’re doing by logging.

Returns:

  • (Boolean)


657
658
659
660
661
662
663
664
665
666
667
# File 'lib/new_relic/agent/agent.rb', line 657

def should_retry?
  if @keep_retrying
    self.connect_attempts=(connect_attempts + 1)
    increment_retry_period!
    log.info "Will re-attempt in #{connect_retry_period} seconds"
    true
  else
    disconnect
    false
  end
end

#tried_to_connect?(options) ⇒ Boolean

We’ve tried to connect if @connected is not nil, or if we are forcing reconnection (i.e. in the case of an after_fork with long running processes)

Returns:

  • (Boolean)


629
630
631
# File 'lib/new_relic/agent/agent.rb', line 629

def tried_to_connect?(options)
  !(@connected.nil? || options[:force_reconnect])
end

#validate_settingsObject

These validation settings are used for cases where a dynamic server is spun up for clients - partners can include a seed and token to indicate that the host is allowed to connect, rather than setting a unique hostname



709
710
711
712
713
714
# File 'lib/new_relic/agent/agent.rb', line 709

def validate_settings
  {
    :seed => Agent.config[:validate_seed],
    :token => Agent.config[:validate_token]
  }
end